如何从窗体级别检测文本框中的按键事件 [英] how to detect a keypress event in textbox from the form level
本文介绍了如何从窗体级别检测文本框中的按键事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
到目前为止,我已经尝试过:
[DllImport(" ]
内部 静态 外部 私有控件GetFocusedControl()
{
Control focusControl = 空;
IntPtr focusedHandle = GetFocus();
如果(focusedHandle!= IntPtr .零)
focusControl = Control.FromHandle(focusedHandle);
返回 focusedControl;
}
私有 无效 Form1_KeyPress(对象发件人,KeyPressEventArgs e)
{
如果(GetFocusedControl().GetType().ToString()== " )
{
// 我的代码...
}
}
但是当活动控件是文本框form1_keypress事件未触发时??
任何帮助将不胜感激....
解决方案
set
此.KeyPreview= true ;
并使用此代码
私有 void Form1_KeyPress(对象发件人,KeyPressEventArgs e) { foreach (控制项 in this . ) { 如果(例如,是文本框) { 如果(重点关注) { // 为聚焦"文本框做些事情 con.Text = " ; } } } }
或使用以下代码:
私有 void Form1_KeyPress(对象发件人,KeyPressEventArgs e) { Control con = 此 .ActiveControl; 如果(例如,是文本框) { // 对焦点"文本框或写"事件进行处理 con.Text = " ; } }
public Form1() { InitializeComponent(); CreateKeyPressEvent(此 .Controls); } 无效 CreateKeyPressEvent(System.Windows.Forms.Control.ControlCollection ctrls) { foreach (Ctrls ctrls中的控件) { 如果(c 是文本框) { c.KeyPress + = 新 KeyPressEventHandler(c_KeyPress); } 其他 CreateKeyPressEvent(c.Controls); } } 无效 c_KeyPress(对象发送者,KeyPressEventArgs e) { TextBox tb =(TextBox)发送器; // 使用此"tb"做任何您想做的事 }
^ ]
只需在this 中设置.KeyPreview= true ;构造函数您将能够接收所有KeyPress,KeyDown和KeyUp事件
hi
i have about 50 textboxes on a single windows form. i dont want to write keypress event for each textbox. is there any parent form level event which detect that there is keypress on any of the textbox.
i have tried so far:
[DllImport("user32.dll"]
internal static extern IntPtr GetFocus();
private Control GetFocusedControl()
{
Control focusedControl = null;
IntPtr focusedHandle = GetFocus();
if (focusedHandle != IntPtr.Zero)
focusedControl = Control.FromHandle(focusedHandle);
return focusedControl;
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (GetFocusedControl().GetType().ToString() == "System.Windows.Forms.TextBox")
{
//my code...
}
}
but when the active control is a textbox form1_keypress event not being fired??
any help will be highly appreciated....
解决方案
set
this.KeyPreview = true;
and use this code
private void Form1_KeyPress(object sender, KeyPressEventArgs e) { foreach (Control con in this.Controls) { if (con is TextBox) { if (con.Focused) { //Do somthing for Focused text box con.Text = "s"; } } } }
or use this code:
private void Form1_KeyPress(object sender, KeyPressEventArgs e) { Control con = this.ActiveControl; if (con is TextBox) { //Do somthing for Focused text box or Write event con.Text = "s"; } }
public Form1() { InitializeComponent(); CreateKeyPressEvent(this.Controls); } void CreateKeyPressEvent(System.Windows.Forms.Control.ControlCollection ctrls) { foreach (Control c in ctrls) { if (c is TextBox) { c.KeyPress += new KeyPressEventHandler(c_KeyPress); } else CreateKeyPressEvent(c.Controls); } } void c_KeyPress(object sender, KeyPressEventArgs e) { TextBox tb = (TextBox)sender; // Do whatever you want with this "tb" }
Form.KeyPreview Property[^]
Just setthis.KeyPreview = true;in the constructor & you will able to receive all KeyPress, KeyDown, and KeyUp events
这篇关于如何从窗体级别检测文本框中的按键事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文