I would like to ask you for help. How i am supposed to remove whole text when i press a button, (for instance 1). Is there a way to do that?
The problem is on c# console application
With Console.Clear() you can remove every Text from the console.
You can use
Console.Clear();
to clear the console window. This removes the whole text from the console window.
Or send a delete character to move the cursor backwards:
Console.Write("\b");
The you can overwrite it with another character.
char c = Console. ReadKey().KeyChar ;
Capture the key and overwrite it with blank
Related
I have a C# application which handles key presses and will break the loop when the Enter key is pressed. The problem I am encountering is that when a multiline paste event occurs, the console is interpreting the newlines as Enter key presses.
Example that doesn't cause Enter keypress:
select something \n from somewhere \n where condition
Example that does cause Enter keypress:
select something
from somewhere
where condition
Does anyone know why this would be? And is there any way to stop this without digging into the WndProc stuff for intercepting a Paste event?
On Windows, new lines are \r\n. When you press the Enter key, these two characters are input. So if you put a \r before each of your \n they should be treated as newlines.
Hi it looks like someone used Console.ReadLine() to trigger loop break.
https://msdn.microsoft.com/pl-pl/library/system.console.readline(v=vs.110).aspx
Effectively loop breaks when you start new line not only press enter Key.
The Enter key press can't be resolved back into \r\n as far as I can tell, so I don't think we can process based on those characters - but I was able to get a hint (for my situation anyway) if the user was pasting multiline text by interrogating Console.KeyAvailable.
So now, I look to see if (Key == ConsoleKey.Enter && Console.KeyAvailable == false) to determine whether to process it as "genuine" Enter key press or not (as there usually aren't keys available in my situation when Enter is pressed).
See this commit for more info
I'm using MessageBox.Show() in a console application for a purpose. But, the header text displayed in the MessageBox is trimmed as it displays ending characters as "...".
Can I set the MessageBox window width to handle this behavior.
Please help me in this regard.
Thanks in advance.
No, in the default MessageBox its not possible to change the size of it. But if you want it, you can create your own message box. Just create a new form, put some buttons inside, in icon if you wanto and some text. And when you want it to show, just simply call ut, like you open the form
See
here
C# formatting a MessageBox
It can't be done. You have to create your own Form mimicking that behavior.
You can try to use the Win32 call SetWindowPos As suggested here.
I am writing a simple C# program in which when I press "ctrl+g" I want my program to automatically delete the character directly left of the cursor in any program (ex: chrome browser, word document, powerpoint document, etc...).
I have installed a global hook for "ctrl+g" and it works fine. I am using a keyboard simulator that I found from codeproject: http://www.codeproject.com/Articles/28064/Global-Mouse-and-Keyboard-Library
My issue is that when I simulate a backspace like so:
KeyboardSimulator.KeyPress(Keys.Back);
the entire word is deleted instead of just the character to the left of the cursor. For example, if I am in a Microsoft Word document with the following line of text:
"Happy new year"
if my cursor is at the end of "year" and I press ctrl+g, my program deletes "year" and puts the cursor just to the right of "new" instead of just deleting the letter "r" of "year".
I have tried other simulators as well with the same result. Does anyone have a solution or know what I am doing incorrectly?
Thanks.
Apparently, Ctrl+Backspace commonly means delete the entire word in Windows. When you press Ctrl+g, and your hook simulates the back space key, the program sees Ctrl+Backspace because the Ctrl is down.
How do you use the ASCII value of backspace 0x08 programmatically in a WinForms TextArea? I tried in a console program:
Console.WriteLine("Hello" + (char)0x8 + "World");
And it shows as I expected:
HellWorld
Where as I tried in a WinForms TextArea to fill the value:
txt.Text += "Hello" + (char)0x8 + "World";
It shows like this:
What to make backspace event to trigger other than using Keyboard's event to make the display as I expect by using the value of 0x8 alone not accessing with any text field events. Through programmatically to print "HellWorld" in text field.
Characters and keypress are not related directly. You would have to get a Backspace keypress into the textbox in order to erase a character.
If you want to simulate someone typing in the textbox, then you might do so using SendKeys.Send(), e.g:
txt.Select();
SendKeys.Send("hello");
SendKeys.Send(((char)0x8).ToString()); // send backspace using 0x08
SendKeys.Send("{BS}"); // send backspace using predefined code
SendKeys.Send("world");
But pre-processing the strings seems much simpler (and safer), e.g:
var str = "hello";
txt.Text += str.Substring(0, str.Length-1);
Note: SendKeys.Send sends keystrokes to the active window. So if another window gets the focus while the above code is running, then the keystrokes might end in a different window.
The backspace character was introduced for thin clients that send input to a server character by character, to allow end users to fix a typo that was already sent. Modern applications usually have no use for such a character, as client applications now usually just wait until the user has finished entering data before sending it to the server.
That is why the TextBox control does not have support for it, and probably why people are wondering why you need this.
Anyway, you'll need to pre-process the data yourself before setting the TextBox's Text. You could for instance use regular expressions to simulate this behavior.
I need write a console application like as hiren boot screen:
alt text http://xahoithongtin.com.vn/Images/diembao/2006_10/Hiren2.jpg
User can input a arrow key or a number for choosing. When a menu item is selected, I will fill a background for the selected menu item.
Please give me some guideline or example. Thanks.
The console class has all the core functionality you need.
To set the cursor to any desired position you can use the Console.CursorLeft or Console.CursorTop properties. A little example is already posted here.
For the colors you can use the Console.BackgroundColor and Console.ForegroundColor.
With these properties you should be able to write all this stuff onto the screen. Afterwards you need to check the user input (KeyUp, KeyDown pressed). This can be done by checking the result of Console.ReadKey() method. By setting the boolean paramter to true you can prevent that the pressed character is displayed on the screen itself.
With this base functionality you should be able to write your own helper class to make all this stuff a little more comfortable.
There are several .NET based NCurses libraries around that make creation of console based interfaces easier:
Curses#
MonoCurses
CursesSharp