Only clear part of console window - c#

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(" ");

Related

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

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.

Difference between "No" and "Cancel" in MessageBoxButtons

I was using a MessageBox to show a confirmation message when the user goes to delete an item and initially used MessageBoxButtons.YesNoCancel. Later I changed it to YesNo instead because a user pointed out that there was no real difference in "No" and "Cancel" in this case. My question is...what is the difference? Is there ever a reason to use YesNoCancel instead of YesNo?
In your case there is no difference as your question results in just one action and then finishes.
In standard usage, the Yes No Cancel usually asks a question, Yes or No will chose a different action and then proceed to do yet another action (like quitting a form), Cancel will abandon all actions.
For example: quitting Word, do you wish to save? "Yes, No, Cancel". Yes and No will continue to quit with or without saving, cancel will not save or quit.
Whatever you do, make sure Cancel does what the user expects most - I forever spam cancel if bombarded with message boxes when I have got to focus on something else. If I cancel something I don't want to lose a lot of work because I didn't have time to stop and handle it properly.
Users abuse cancel :-)
Sure there could be. For example, if there is a save dialog and you enter a filename that already exists, the dialog could ask you if you want to overwrite the file.
Yes would mean overwrite the file. No might mean append a "(1)" at the end of the file name, or prompt for a different file name. Cancel might mean don't save after-all.
You should note that Yes, No, and Cancel are all different enums and do not have the same value, so you can treat them differently.
REMEMBER TO HANDLE CANCEL ANYWAY because if the user clicks the x button in the top right corner of your dialog screen, the result of ShowDialog() is DialogResult.Cancel!
Here's an example of when YesNoCancel would be appropriate:
"Would you like to save your changes before quitting?"
Yes - Save and quit
No - Don't save and quit
Cancel - I pressed the button on accident, don't quit.
Im sure its easy enough to come up with a scenario where there would be a symantic difference between "No" as a response and "Cancel". Traditionally "Cancel" should return the program to its state before starting the current series of operations. "No" certainly doesnt have this same rule.
Example:
"Do you wish to delete file 4 of 10?"
Yes: Delete the file
No: Dont delete the file, move on to file 5 of 10
Cancel: Exit this operation and return to having not deleted any files.
I guess "Cancel" is used to abort the whole operation. If you are dealing with a huge procedure, for example, moving a set of files from one directory to another, you may want to ask something to the user about a specific file - for instance, to confirm if the user really wants to move a protected file. If the user presses "No", you ignore that item and continue the action. If the user presses "Cancel", you abort the whole action (and, maybe, rollback the previous action).
Of course, for a small procedure or a simple situation, "Cancel" and "No" have no difference.
Here's an example:
Say you are exiting an application with an unsaved file, like a word processor.
There is a confirmation when exiting that says: "Your file has changes that haven't been saved. Would you like to save them?"
In this case:
Yes = save the file and exit
No = exit and lose the changes
Cancel = abort the exit and go back to the application

C# how do I pause my program and wait for keyboard input from within a WinForm

I have a C# 2008 Winform application and I'm in the middle of a loop. I'm displaying a date to the user and I want them to tell me the day of the week that this date falls on. For example 6/22/2010 is displayed and the user needs to press t.
What I'm stuck on is how do I pause my application and wait for keyboard input? I want to respond to Esc, m, t, w, h, f, s, u and nothing else. All other key presses will be ignored.
In a console application it would be Console.ReadLine(). But how would I do this within a Winform application?
Thanks
You could perhaps use a modal dialog...
Trying to think of a better solution in terms of presentation...
Spawn a modal dialog box requesting the input.
To read a key you need to respond to the KeyDown event.
Then in the handler have something like:
if (e.KeyCode == Keys.M)
{
}
Though you'd probably want a switch statement rather than a series of ifs.
You'd have to think about how you presented the date to the user as well, if it's on the main form or in a model dialog (as others have suggested).
You've got the concept wrong, a Windows program is always in a loop, waiting.
So you'll have to think about what input to accept and what to block. You can Cancel a FormClose event for example (but please, leave the user something to get out).
To implement your scheme, use the concept of 'state', after the right input you advance to the next state.
When you need some information during a calcultion (the loop in your case), you can use a callback method to get this information.
int Calculate( Func<DateTime, string> callback )
{
var result = callback( dateTime );
}
The caller of this method has to provide a callback that returns the requested value. When the calculation is started in a form, this form could pop up a dialog to ask the user for input. And this could happen in the callback.
EDIT:
Do you know of the DateTime.DayOfWeek property? Maybe you can skip the user dialog at all.

See substring in debug watch

Is it possible to see a substring of a string when looking a string in the Watch window during a debugging session?
Yes - just watch
text.Substring(1, 15)
or whatever.
What happened when you tried this?
EDIT: "doesn't work" isn't really a description of what happened. I've just tried this myself in VS2008, and it worked fine. The value greys out when you step, but there's a little "refresh" button on the right hand side of the value box... when you click that, it will re-evaluate the expression.
If that's not happening for you, please describe what is happening for you.
I tend to use the Immediate Window for this sort of thing, but as Jon Skeet has said you can also do it directly in the Watch Window.
sure, just put in a new watch entry with the substring you want to see :-

Shape of a Winforms MessageBox

I am looking for a way to have some control over the shape of a simple MessageBox in Winforms. I would like to control where the passed in text wraps so that the dialog rect is narrower. Windows seems to want to make the dialog as wide as possible before wrapping the text. Is there an easy way to control the maximum width of the dialog without resorting to creating my own custom form?
You can embed newlines in the text to force it to wrap at a certain point. e.g.
"message text...\nmore text..."
update: I posted that thinking it was a win32 API question, but I think the principle should still apply. I assume WinForms eventually calls MessageBox().
There's really just two ways (sane ways)
1) Add line breaks to your string yourself to limit the lenghth of each line.
2) Make your own form and use it rather than messagebox.
What happens if you throw your own newlines in the string message you pass it? I'm pretty sure that will work if I recall correctly.
This, or alternatively create your own form and use that.
The \n newline chars will give you enough flexibility, then do this. I use this a lot. Eg. if I'm giving a warning, the first line will give the warning, and the next line will give the internal error message or further information as appropriate. If you don't do this, you end up with a very wide message box with very little height!
MessageBox only has limited variability - eg. the button types and icon. If you need more, then create your own. You could then do all sorts of things like add URLs, a Help button ,etc.

Categories

Resources