I'm creating a login form, and like what the title says, is there a way to make my textbox (textbox for the username in this case) catch whatever the user is typing? I want to copy what the Facebook homepage does. If you're not logged in, and you go to FB, and try typing in, whatever it is that you typed, it appears to the textbox labeled "Email or Phone." Also, I'm new to C#.
Edit: I want the textbox to catch the text that the user typed in, even if the user did not select the said textbox. I'm not really good in English so I don't know how to properly phrase what I want to say. Sorry.
Edit 2 (from my comment below): I want to apologize again for my English. I'm gonna try again. For example, I opened the login form, and then it appeared. No control is selected or is in focus. Now, when the user type something, I want the textbox for the user to be in focus, and whatever was typed by the user be also typed there.
So that's basically the intention of a event. You should use the TextChanged()
event of your TextBox
. This event will be invoked each time the content of the property textBox1.Text
changes.
private void textBox1_TextChanged(object sender, EventArgs e)
{
targetTextBox.Text = textBox1.Text;
}
You could store the text value as well in a string
property for further global use. Instead of a plain string
property, you could use a Dictionary<TKey,TValue>
as well. As key you could store the specific user logged in as a string
and it's value could be a List<string>
of all the inputs this specific user has written.
Edit according to the new needs of OP:
So at first you should set the focus to your desired textBox
. If I understand you right, you want this to happen right after the login form loaded. So add this to the end of your constructor of the login form.
this.ActiveControl = textBox1; /* name of the textBox you want to set focus to*/
Then subscribe to the TextChanged
event for the textBox
that gets the focus and that's it. That could be done the easiest way in the properties
window that shows once you look at the design of your form.
Edit according to the new provided code: This is about setting the focus to textBox1
referring to the code environment of OP.
private void button1_Click(object sender, EventArgs e)
{
Hide();
Form2 form2 = new Form2();
form2.ShowDialog();
Show();
//simply setting the focus here
this.ActiveControl = textBox1;
}
Or you could e.g. subscribe to the event Form1_VisibleChanged()
. Then you don't even need to set the focus in the constructor, just in this event.
private void Form1_VisibleChanged(object sender, EventArgs e)
{
this.ActiveControl = textBox1;
}
This event gets every time fired when the visibility of Form1
gets changed, so even on the first initialisation or after every invoke of Show()
.