简体   繁体   English

如何检测文本框中的按键?

[英]How to detect a key press within the textbox?

I am new to C#.NET. 我是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). 我正在尝试开发一个远程处理应用程序,无论我在文本框(Windows窗体)中键入什么字符,我都希望将其发送到将出现相同字符(接收到字符)的远程处理机。 Is it possible to send each key i pressed within the textbox? 是否可以发送我在文本框中按下的每个键? Can i make use of keybd_event() or SendKeys.Send() ? 我可以使用keybd_event()或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 双击KeyDown并插入您想要对插入文本框中的每个字符执行的操作的代码

in textbox event option there are some events KeyPress , KeyUp , KeyDown . 在文本框事件选项中,有一些事件KeyPressKeyUpKeyDown 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. 您可以使用TextBox.KeyDown事件来检测文本框中的按键事件。 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. 正如@Jacooobley所解释的,您可以在设计器中注册事件处理程序,否则可以在Load()或表单构造函数中注册事件处理程序。 You can take a look at Consuming Events for understanding the control flow. 您可以看一下消耗事件,以了解控制流。 The said events are available in the System.Windows.Forms namespace 所述事件在System.Windows.Forms命名空间中可用

public MyForm()
{
    InitializeComponent();

    this.myTextBox.KeyDown += myTextBox_KeyDown;
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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