简体   繁体   中英

How to detect a key press within the textbox?

I am new to C#.NET. I am trying to develop a remoting application where, whatever characters i type in textbox(windows forms) i want to send it to remoting machine where same character will appear( character is received). Is it possible to send each key i pressed within the textbox? Can i make use of keybd_event() or SendKeys.Send() ?

What are the namespaces? Sorry I have less knowledge in this field..!

If you click on the textbox, go into properties then click the lightning bolt. These are your events. Double click on KeyDown and insert your code for whatever you would like to do with each character that is inserted into the text box

in textbox event option there are some events KeyPress , KeyUp , KeyDown . you can add your custom sending code in this event

 private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
 {
     // your code  is here 
 }

You can use the TextBox.KeyDown event to detect the key events in your textbox. You can register the event either in the window designer or in code. As explained by @Jacooobley, you can register the event handler in the designer, else you can register the event handler in the Load() or form constructor. You can take a look at Consuming Events for understanding the control flow. The said events are available in the System.Windows.Forms namespace

public MyForm()
{
    InitializeComponent();

    this.myTextBox.KeyDown += myTextBox_KeyDown;
}

void myTextBox_KeyDown(object sender, KeyEventArgs e)
{
    // Send the message to the remote machine asynchronously
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM