textbox with autocomplete doesn't excecute key_press event - c#

I have a textbox with the event of KeyPress to change to other textbox when Enter-key is Pressed and it was working until I add an autocomplete property from the property panel on visual studio like:
AutoCompleteCustomSource: Collection
AutoCompleteMode: SuggestAppend
AutoCompleteSource: CustomSource
Now autocomplete works but when I press enter it does not change to the other textbox.

Maybe you can use event KeyDown instead. It works fine in my test.
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
textBox2.Focus();
}
}

Related

How to change text of Label while typing in TextBox

Please I'm new to C#, I created a textBox and a label. What i am expecting is, if I type a value into the textBox, I want it to display on the label and if I change the value it should also change immediately on the label.
it work with the code below and i press enter key
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
label1.Text = textBox1.Text;
}
}
But I want it without press Enter/Return Key on keyboard.
Thanks for understanding
This works for VisualStudio
Select your TextBox in the Designer, go to the it's properties and click on the events (teh icon with the lightning). Then make a double click on the event that is called: TextChanged.
This creates a new function, that will always be called when the text of your TextBox changes. Insert following code into the function:
private void textBox1_TextChanged(object sender, EventArgs e)
{
TextBox tb = sender as TextBox;
label1.Text = tb.Text;
}
That's it.
label.DataBindings.Add("Text", textBox, "Text");
textbox KeyDown/Up/Press events may help.
For example
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
label1.Text += e.KeyData.ToString();
}

Set label to textbox content

Here's what i want to do:
I want to be able to take the contents entered into a textbox. Once the user hits the ENTER key or the enter button I made, to have that content appear in a label I've placed on my form.
And have that same label change to whatever you enter in the textbox everytime you hit enter.
I hope I explained this good enough. Thanks!
Subscribe to KeyDown event of TextBox and check if Enter is pressed
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
label1.Text = textBox1.Text;
}
add a button to your form, double click it, inside the event add the code below
label1.text=textbox1.text
and do some basic tutorials, dude
Assuming you are using winforms... First you need to enssure that your form will response to keyboard events.
So you need to set the property KeyPreview to true.
Then use the event KeyDown and write this code:
private void form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
myLabelName1.Text = myTextBoxName1.Text;
}

KeyDown event is not firing when pressing enter in an UserControl

I have a UserControl for an application and with a textbox. When pressing enter in this textbox a button should be activated (basically pressing 'Ok' via pressing Enter instead of clicking the button manually with the mouse).
So I added a KeyDown event to my textbox and implemented the following code:
private void txtSearchID_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter && !txtSearchID.Text.Equals(string.Empty))
{
button_Click(null, null);
}
}
But when I wanted to test this nothing happened when pressing Enter. Strangely enough in addition to that the event does not even get fired when pressing Enter but with every other key. Am I missing something?
This is because, when a key is pressed it will go to the control which has focus on the form, as the KeyPreview property of Form is set to False by default. You change the event like this
Change to KeyUP event,
private void txtSearchID_Keyup(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter && !txtSearchID.Text.Equals(string.Empty))
{
button_Click(null, null);
}
}

give copy access to the text of a label in a window

I am developing a application using C#.
I have a window which has a label containing some text.
I want to copy as we copy something from anywhere.
But i cant copy of the label from the window.
How can i do that to copy the text of the label???
You will not be able to do this with a label.
You could try doing this with a textbox, to simulate the label and hightlight select.
TextBox.ReadOnly Property
Use the ReadOnly property to specify whether the contents of the
TextBox control can be changed. Setting this property to true will
prevent users from entering a value or changing the existing value.
and something like
TextBox1.Text = "Hello, Select Me";
TextBox1.ReadOnly = true;
TextBox1.BorderStyle = 0;
TextBox1.BackColor = this.BackColor;
TextBox1.TabStop = false;
Add a method to the label to make the label get focus if clicked:
private void label1_Click(object sender, EventArgs e)
{
label1.Focus();
}
Set the 'KeyPreview' property of the form to 'true' so it will process keys being pressed. I also added a method to handle the keydown event:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (label1.ContainsFocus && e.Control && e.KeyCode == Keys.C)
Clipboard.SetText(label1.Text);
}
This should work even if the "KeyPreview" property is false. This property is true if the form will receive all key events; false if the currently selected control on the form receives key events. The default is false
By default winforms label control doesn't support the feature of selecting text and copying. Instead you can add click event to the label and onclick give focus to the label. And in the form key press event check if label is focused and Ctrl+C is clicked then copy it to click board.
private void label1_Click(object sender, EventArgs e)
{
label1.Focus();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (label1.ContainsFocus && e.Control && e.KeyCode == Keys.C)
Clipboard.SetText(label1.Text);
}

Autocomplete on Combobox onkeypress event eats up the Enter key

I have a ComboBox with AutoCompleteMode = suggest and handle the KeyPress event like so:
private void searchBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Return)
{
// do stuff
}
}
However, it does not catch the Enter key. It catches everything else since the autocomplete dropdown works perfectly.
I also tried the suggestion offered here : http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/2db0b540-756a-4a4f-9371-adbb92409806, set the form's KeyPreview property to true and put a breakpoint in the form's KeyPress event handler:
private void Form_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = false;
}
However, even the form's handler was not catching the enter key!
Any suggestions?
(If I disable the autocomplete, it catches the Enter key)
Difference between KeyDown and KeyPress
In your case the best you may do is use KeyDown event.
void SearchBox_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
{
// Do stuff
}
}
Another interesting thing about KeyPress event is: it even catches Enter key with autocompete on if the combobox has no items! :-)

Categories

Resources