Console.Clear(); doesn't clean up the whole console - c#

I have a part in my application that generates more output than in a console window fits. I'm satisfied with scrolling through the generated lines but by clearing the console window only the last part disappears. Everything above the height of the console window is still there when I'm scrolling up.
Now, is there a way to wipe the whole console content even the scrollback buffer? Or is the only solution to develop a pager?
Thanks for your replies!
EDIT:
I wrote the following code as POC:
for (int i = 0; i < 100; i++)
{
Console.WriteLine(i);
}
Console.Clear();
Console.ReadKey();
When I'm running that code only the lines 71 to 100 will be deleted.

Thanks for all your replies!
Finally I solved it with the following escape sequence:
Console.Clear();
Console.WriteLine("\x1b[3J");
This sequence removes the whole content of the console. (But it only works reliable if the clear command is called first)
After these two lines the console is empty and the scrollbars are removed or disabled. The cursor is on the second line (and there could be a few chars from the last input). To prevent that I called the clear command again.
I hope this will help someone with the same problem.

Related

c# form display timing

I have a forms program that displays two forms. Essentially form1 hides form2, rebuilds it's contents by creating a checkerboard, then shows form2. If I go thru it a step at a time, everything displays fine. I want to run several sequences of hide-rebuild-show with a short pause between each display of form2 so I can verify it is working as expected. If I put a msgbox after each show I see form2 drawn correctly. If I put a 2 second pause (loop doing nothing for 2 seconds) after the show I see the outline of the form but an open space where the checkerboard should be. I think it's some sort of timing issue but don't know how to fix it. What's the correct way to do a short pause?
private void Do1Bot()
{
SetStart();
for (int i = 1; i <= numsess; i++)
{
NextSess();
Do1Sess();
//MessageBox.Show("After do1 sess"); // Checkerboard appears in the middle of the form this way.
//Wait(2); // This way I get the form borders but a blank space in place of a checkerboard.
}
}
private void Wait(int secs)
{
DateTime Tend = DateTime.Now.AddSeconds(secs);
do { }
while (DateTime.Now < Tend);
}
Adding Application.DoEvents() to the wait loop (thanks to Sam Axe) solved the problem.

Enter in text editor set cursor at begin of line instead of tabs

I have a strange behaviour with VS2015.
In C# code (the file is .cs) when I hit the enter, the cursor will always begin on position 0 of the next line. No tabs will be inserted to push the cursor to the same position of the previous line where code started.
public class Fake
{
public void Test() (here I hit enter)
{ <-- no tabs are automatic inserted before {
}
I have done: repair VS2015, Reset user Environment settings VS.
Someone an idea to reset the behaviour to default C# text editor?
Find the parameter - /ResetUserData - which Removes all user settings and makes you set them again. From http://msdn.microsoft.com/en-us/library/bb245788%28v=vs.80%29.aspx#vs05idetips_topic6
I got a similar issue here and it was about files.autoSaveDelay setting.
Issue:
After breaking line my cursor does go to the right indentation but after 1 sec goes straight to the beginning of the current line.
Cause:
So it happens because VSC (Visual Studio Code) erases any tab or spaces within a line if there is only these type of chars.
Solution
What you can do in order to solve it is either set a higher delay to files.autoSaveDelay or disable files.autoSave.

Only clear part of console window

My console window looks like this:
YES
YES
NO
YES
NO
NO
YES
DOING THE ACTION
ACTION HAS BEEN DONE
ACTION HAS BEEN DONE
The yes and no's are random and change every X seconds. I currently just use Console.Clear(); for clearing the console but this clears the entire console. I only want to clear the yes and no's and let the rest of the lines be. Is there a way to do this?
Edit: I forgot to add that every time it refreshes there are maybe a few yes and no's more or less.
You can set the position where you want to write your text and overwrite those values with spaces. For example:
Console.SetCursorPosition(5, 6);
Console.Write(" ");

RichTextBox replacing input with "random" unicode character

I have an application that allows users to browse through data. I have menu items controlling navigation and a RichTextBox displaying the data. Very straightforward.
tl;dr version
It mostly works except for one strange problem. There are instances where the RichTextControl will replace the first character typed with a random unicode character. Feel free to download this sample app and see for yourself:
http://www.technitivity.com/stackoverflow/RichTextFocusTest.zip
Full Explanation
The issue happens when navigating between rows. It's best described with a few use cases:
Use Case 1
Navigate anywhere into the dataset.
Press back.
Press next.
Type any letter, say, "F".
Result: "F" appears in the RichTextBox as expected.
Use Case 2
Navigate anywhere into the dataset.
Press back twice.
Press next twice.
Type the letter "F".
Result: instead of "F", "ᅲ" appears in the RichTextBox.
Use Case 3
Navigate anywhere into the dataset.
Press next twice.
Press back twice.
Type the letter "F".
Result: instead of "F", "᧴" appears in the RichTextBox.
The navigation process entails nothing more than:
// either forward
i++;
// or backward
i--;
// then update
RichTextBox1.Text = MyData[i];
Procedurally speaking:
// This works
RichTextBox1.Text = MyData[3];
// This works
RichTextBox1.Text = MyData[3];
RichTextBox1.Text = MyData[2];
RichTextBox1.Text = MyData[3];
// This doesn't work
RichTextBox1.Text = MyData[3];
RichTextBox1.Text = MyData[2];
RichTextBox1.Text = MyData[1];
RichTextBox1.Text = MyData[2];
RichTextBox1.Text = MyData[3];
Granted, that's not what's actually happening, but it is what's effectively happening.
It's important to note that this doesn't happen if the RichTextBox loses focus between updates. It only happens if the RichTextBox retains focus while its Text attribute is updated in accordance with the above description.
I'm at a complete loss about what's causing this, how to fix it, or why I can't seem to find anyone else with this problem.
I've reproduced it on 64-bit Windows 7 and 32-bit Windows Vista. This is on .NET Framework 4 though I was also able to reproduce on a .NET Framework 2 project.
Here's hoping someone else has run across this (and solved it!)
Edit:
Here's a screenshot:
http://www.technitivity.com/stackoverflow/RichTextBox-Screenshot1.png
As mentioned in the comments, to reproduce this in the sample app, you have to use the keyboard menu shortcuts. If you click on the menu items (or the toolbar buttons), the RichTextBox loses focus and the problem goes away. But if you navigate through the items using Alt+Left or Alt+Right (back/next) and then type, you should see something like what's shown in the above screenshot.
I hesitate to call this an "answer", but I couldn't find a "Post a Hack" button and this hack does get me by for now. I'm not thrilled about it, but sometimes you just have to move on. Here it is.
Since the problem went away when the RichTextBox lost focus, I tried an experiment:
I created a visible, 0-pixel wide textbox, called Hacktastic.
I added a KeyPress event to the RichTextBox.
On KeyPress:
Hacktastic.Focus();
Hacktastic.Text = KeyChar.ToString();
MyRichTextBox.Focus();
This worked and (at least for now) I'm sticking with this as a solution. If anyone could still try out my sample project and reproduce and/or solve this, I would love further feedback:
http://www.technitivity.com/stackoverflow/RichTextFocusTest.zip
Steps to repro in the test project:
Using Alt+Right arrow, move through the dataset to, say, the fourth record.
Using Alt+Left arrow, move back through the dataset two places, to the second record.
Using Alt+Right arrow, move back to the fourth record.
Press any KeyChar.
Observe KeyChar is replaced with a "random", large-value unicode character.
I say "random" because a specific set of navigation (back/next) keystrokes will insert the exact same unicode character. However, depending on where you start in the set or how far back you go, you'll get a different character.
Also, note that only going back one record and forward one record does not cause the problem. You have to move at least two records for this to happen.

How do I change the tab stops in a Command Prompt/Console window/DOS Box?

I have a .NET console window and some of my columns I'd like to have wider than the tab stop, without doing fancy coding, simulating my own tabs, or using an alternative console or output device.
It ought to be easy! Is there a vt100 command or something I can send? Or is my Google-fu that weak?
thanks for your help.
Could you use String.PadRight?
string str;
str = "BBQ and Slaw";
Console.Write("|");
Console.Write(str.PadRight(15));
Console.WriteLine("|"); // Displays "|BBQ and Slaw |".
Console.Write("|");
Console.Write(str.PadRight(5));
Console.WriteLine("|"); // Displays "|BBQ and Slaw|".

Categories

Resources