1
Is it possible to pick a string from a textbox when pressing the enter key? If so, how? I’d like you to leave examples of code because I’m pretty new to this world of Winforms...
1
Is it possible to pick a string from a textbox when pressing the enter key? If so, how? I’d like you to leave examples of code because I’m pretty new to this world of Winforms...
1
You can associate the code that will use the content of Textbox with an event that is triggered when a key is pressed while the Textbox has the focus.
You can use the event Keydown of the component Textbox. To do so, select the component in the form and in the window Properties, click on the button pointed to see the list of events, locate the event Keydown and double-click on it:
In the archive Nomedoform.Cs the visual studio will generate a code more or less like this:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
}
And will also associate this method to the event Keydown of the component Textbox (in this case, the name of my Textbox is textBox1).
See in the file Nomedoform.Designer.Cs how Visual Studio did it:
private void InitializeComponent()
{
...
this.textBox1.KeyDown +=
new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
...
}
}
Finally, in the body of the method that Visual Studio generated in Nomedoform.Cs, enter the processing code for the Textbox text. Example:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Enter)
{
MessageBox.Show(textBox1.Text);
}
}
Note then that it is not enough for you to write the above code, this method needs to be associated with the event of the textbox component.
Study how Winforms programming is event oriented, try to understand the code generated by Visual Studio.
Thanks for the example, the problem is that I am using Xamarin Studio. There is how to do it for him?
I was able to associate the event with the textbox. But it doesn’t work...
@Davidamaral Then it must be a compiler bug. These C# compilers are very unstable ;-) Let’s hope someone with the solution to a problem described as "not working".
I am using Xamarin which apparently does not have the Keydown event. Only Keypress and Keyrelease. Some way to do it by Keypress?
Browser other questions tagged c# winforms
You are not signed in. Login or sign up in order to post.
Explain better what it is to "catch a
string
of aTextBox
". Pick up where? Do what with it? The question is very abstract. Put an example of what you are doing and what is your problem.– Maniero
I want the textbox input to be picked up when the enter key is pressed.
– David Amaral
This did not add any new information, you just repeated what you had already written. Do what I said.
– Maniero
It’s simple. I’m trying to create a textbox that, when pressed enter inside the textbox, the input the user wrote in the textbox. That’s it, it’s not so complex...
– David Amaral
But you don’t know what you want to do with it. The answer given was a kick and you considered it appropriate because you didn’t want to do anything useful with this information.
– Maniero
It’s a simple matter. If you don’t understand, it’s not my fault.
– David Amaral
If it’s simple, why don’t you describe it correctly? Information is missing so that a correct answer can be given. What they said to him is useless.
– Maniero