adikrish
adikrish

Reputation: 11

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..!

Upvotes: 0

Views: 2517

Answers (3)

Devendra D. Chavan
Devendra D. Chavan

Reputation: 9011

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
}

Upvotes: 1

Cubsoft
Cubsoft

Reputation: 1157

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

Upvotes: 3

Nikson Kanti Paul
Nikson Kanti Paul

Reputation: 3440

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 
 }

Upvotes: 2

Related Questions