I have the following code:
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.N)
{
richTextBox1.Select(1, 3);
}
}
When I press the N key , the selected text is replaced with "n". I read this Selecting text in RichTexbox in C# deletes the text ,but it had no effects.
I am using Windows Forms.
Likely, you will need e.Handled = true; in this to stop the event.
http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.handled.aspx
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.N)
{
richTextBox1.Select(1, 3);
e.Handled = true;
}
}
Try it yourself:
Open up the editor, type some text, mark some of this text and press N. What happens? The marked text is replaced with n.
The same thing happens in your RichTextBox. Important to understand here is, that with the event you set up, you only add some functionality and leave the default event handling (handled by the OS) intact.
So with your code, on a key press you just do
richTextBox1.Select(1, 3);
which selects some characters and afterwards the default event handling kicks in. Thus there is some marked text which gets replaced with N.
So, you simply have to mark the event as handled by yourself. Not using the Handled-property, but with the SuppressKeyPress-property.
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.N)
{
richTextBox1.Select(1, 3);
e.SuppressKeyPress = true;
}
}
The documentation of Handled clearly states:
If you set Handled to true on a TextBox, that control will
not pass the key press events to the underlying Win32 text
box control, but it will still display the characters that the user typed.
Here is the official documentation of SuppressKeyPress.
Related
I want to make a wpf application in c# that displays some text on screen, and where the user is supposed to write a response and press enter to submit the response. I don't want to use a textbox, since there is only one line for the text input in the window, and I don't want the user to have to click to select the textbox. I want the application to be mouse-free.
My question is: How do I make it so that when the user has written their answer, they can submit the response simply by pressing enter?
I have tried the following snippet of code which I found on a microsoft help website:
private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
doSomething();
}
}
I suppose I have to add some code elsewhere, but I'm not sure where or what I need to add.
If you want to make sure your window process every Enter key press without care what control is focused you can use PreviewKeyDown event:
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
//Process user input
e.Handled = true;
}
}
Of course if you are doing mvvm you can create a behavior to encapsulate the event handler:
public class WindowBehavior : Behavior<Window>
{
protected override void OnAttached()
{
AssociatedObject.PreviewKeyDown += AssociatedObject_PreviewKeyDown;
}
private void AssociatedObject_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
MessageBox.Show("Enter from Window");
e.Handled = true;
}
}
protected override void OnDetaching()
{
AssociatedObject.PreviewKeyDown -= AssociatedObject_PreviewKeyDown;
}
I suggest you to read this article about bubble, tunneling and direct events basic for WPF events.
If you have a button that you're using for submit, you can easily set it as the default by using the IsDefault=true (wrote a tip about doing this and the cancel for the escape here.)
Other than that, you'll have to have somewhere to write it (yet you don't want a textbox? you can select it by default, or tab into it if you don't have the focus there), and you can handle the keydown to "catch" the Enter otherwise.
I want to prevent the Enter key from making a new line inside the multiple line text box using the KeyDown event. Despite clearing the textBox using textBox.Text = "" or textBox.Clear(), it behaves like this: http://i.imgur.com/3sknAbO.png
private void textBox2_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
listBox1.Items.Add(textBox2.Text);
textBox2.Text = "";
}
}
So, basically you want to define a text box as multi-line and only accept one long line from it?
Seems as though you only want a single line text box. Define your text box as single line and set it up at design time to look like a multi-line text box.
Another option would be to leave the box as-is and allow the user to enter it with the newline characters in it. Then, when you need the data, simply remove all newline characters after the fact.
I was experimenting a bit and it seems like the new line from the Enter key is added at the KeyUp event. So this should work (note that I use KeyUp instead of KeyDown):
private void textBox2_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
listBox1.Items.Add(textBox2.Text);
textBox2.Text = "";
e.Handled = true;
}
}
Or you could set the cursor position with
textBox2.Select(0, 0);
instead of using the line e.Handled = true;
I have Windows Form Application with TextBox and Label and I want to type something in the textbox and then press Enter to let's say show what I've typed in Label.
Example with button:
private void button1_Click(object sender, EventArgs e)
{
this.Label1.Text = this.TextBox1.Text;
}
I need to do exactly the same but with pressing Enter not button.
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Label1.Text = TextBox1.Text;
}
}
I tried the code from Jan Anderssen and it works correctly. The error "Operator '==' can not be applied to operands of type 'char' and 'System.Windows.Forms.Keys" is because you are matching a character to Keys.Enter, make sure that the syntax is correct e. KeyCode.
E.KeyCode is used because in the event handler send a parameter with the value of e "KeyEventArgs e" variable and here is the key pressed.
private void txtText_KeyDown (object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
this.lblText.Text = this.txtText.Text;
}
}
Did you copy and paste the code?
If so, try doing it with the events of the properties box.
Click in the textbox -> Events ---> key down ---> double click and put the code there.
Do you have more than one form? This code may change.
you can use the text change event and check when enter pressed.
when you identify enter, then you can do what ever you want
I have a listbox which displays the contents of an array. The array is populated with a list of results when my "go" button is pressed.
The go button is set as the AcceptButton on the form properties so pressing the Enter key anywhere in the focus of the form re-runs the go button process.
Double clicking on a result from the array within the listbox works fine using below:
void ListBox1_DoubleClick(object sender, EventArgs e) {}
I would like to be able to use my arrow keys and enter keys to select and run an event without having to double click on the line within the listbox. (however go button runs each time instead)
Basically open the form, type search string, press enter to run go button, use up and down arrows then press enter on selection to run same event as double click above. Will need to change focus after each bit.
You can handle the KeyDown events for the controls you want to override. For example,
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
//execute go button method
GoButtonMethod();
//or if it's an event handler (should be a method)
GoButton_Click(null,null);
}
}
That will perform the search. You can then focus your listbox
myListBox.Focus();
//you might need to select one value to allow arrow keys
myListBox.SelectedIndex = 0;
You can handle the Enter button in the ListBox the same way as the TextBox above and call the DoubleClick event.
This problem is similar to -
Pressing Enter Key will Add the Selected Item From ListBox to RichTextBox
Certain controls do not recognize some keys when they are pressed in Control::KeyDown event. For e.g. list box does not recognize if the key pressed is Enter key.
See the remarks section of the Control::KeyDown event reference.
One way to resolve your problem might be writing a method for the Control::PreviewKeyDown event for your list box control:
private void listBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up && this.listBox1.SelectedIndex - 1 > -1)
{
//listBox1.SelectedIndex--;
}
if (e.KeyCode == Keys.Down && this.listBox1.SelectedIndex + 1 < this.listBox1.Items.Count)
{
//listBox1.SelectedIndex++;
}
if (e.KeyCode == Keys.Enter)
{
//Do your task here :)
}
}
private void listBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Enter:
e.IsInputKey = true;
break;
}
}
I have a multiline textbox which shouldn't accept alphabets, numbers, newline(enter key) and backspace. In short, the textbox.Text shouldn't be editable. But I do want the textbox to accept two shortcut keys - control and control+R. One way I can make the textbox un-editable is by making it read-only. But then the textbox wont accept any keystroke at all. In short, my shortcuts ( control and control+R) wont work( Control + R) with read-only method.
Can anyone help in this regard. That's all I have to do.
One thing I could do here is not to make the textbox read-only and then restrict the characters(alphabets and digits) that could be inputted in the textbox. With this code:
private void txtResult_KeyPress(object sender, KeyPressEventArgs e)
{
// only modifier keys, escape, enter, backspace etc is accepted now
e.Handled = !char.IsControl(e.KeyChar);
}
private void txtResult_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control == true)
{
if (e.KeyCode == Keys.R)
{
// do something
}
else
{
//do something
}
}
if (e.KeyCode == Keys.Escape)
{
//do something
}
}
With this technique I can get the shortcuts(control and control+R) working. But the trouble with this method is that Enter and Backspace keys work as well making it possible to edit the text of textbox. How can I specifically restrict Enter and Backspace key being registered from the textbox, but let Control and Escape??
did you try SuppressKeyPress = true ?
private void textBox2_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control == true)
{
if (e.KeyCode == Keys.R)
{
// do something
}
else
{
//do something
}
}
else
e.SuppressKeyPress = true;
}
Since you are handling the keys in the KeyDown event handler, why not have your KeyPress handler return that all keystrokes are handled?
So just set e.Handled = true no matter what. I believe the backspace and enter would be interpreted as control characters, also.
The Enter and Backspace keys won't work if the textbox is set to ReadOnly, as you suggested early on in the question that you had done. Make sure the property is still set to true. You can either set it in the Properties window, or through code like so:
myTextBox.ReadOnly = true;
Then, you need to handle the KeyDown event for the textbox control, and watch for the specific keys that you're interested in. Something like this:
private void myTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control)
{
if (e.KeyCode == Keys.R)
{
MessageBox.Show("Pressed Ctrl+R");
}
else
{
MessageBox.Show("Pressed Ctrl");
}
}
else if (e.KeyCode == Keys.Escape)
{
MessageBox.Show("Pressed Esc");
}
}
This works exactly as expected, as long as the textbox is set to read-only. No other keys are recognized, and the user cannot change or modify any of the text in the textbox. You don't need to suppress the keypresses, as the control is already doing that when you set it to read-only. You also don't need to handle both the KeyDown and KeyPress events. KeyPress won't work for you anyway, as it doesn't let you handle control characters.