表格按键事件 [英] Form keypress event
问题描述
我有MDI表单和MDI表单的子项,
当用户按ALT键时,它会在表单上显示快捷字母作为图片框的工具提示(就像我们在MS-Word中按Alt键一样) 。
i使用了以下两种形式的事件&设置KeyPreview = true: -
private void Form1_KeyDown( object sender,KeyEventArgs e)
{
if (e.Alt)
{
toolTip1.Show( U ,此,pic1.Location);
toolTip2.Show( K,这个,pictureBox2.Location);
toolTip3.Show( L,这个,pictureBox3.Location);
}
}
protected override void OnKeyPress(KeyPressEventArgs ex)
{
MessageBox.Show(ex.KeyChar.ToString());
string xo = ex.KeyChar.ToString();
if(xo ==k)//你在键盘上按了q键
{
MessageBox.Show(K pressed);
}
else if(xo ==j)//你在键盘上按了q键
{
MessageBox.Show(按下图表控件事件) ;
}
}
按下alt键时触发Form1_KeyDown事件
&显示分配的图片框的所有工具提示字母,
之后,如果我从键盘按k键,它将不会触发OnKeyPress事件,
然后再次按'k 来自键盘的键,它会触发OnKeyPress事件。
之后一切正常。
为什么MDI表格会发生这种情况。
有一种更简单的方法可以做到这一点,不需要超越'OnKeyPress。假设下面显示的KeyDown和KeyUp EventHandler连接到您的MDIChildForm,并且其KeyPreview属性设置为'true。private bool IsAltDown = false ;
private bool IsToolTipsShown = false ;
private 列表< Keys> keyList = new 列表< Keys> {Keys.K,Keys.L,Keys.U};
private void Form2_KeyDown( object sender,KeyEventArgs e)
{
IsAltDown = Control.ModifierKeys == Keys.Alt;
if (IsAltDown)
{
toolTip1.Show( U, this ,pictureBox1.Location);
toolTip2.Show( K,这个,pictureBox2.Location);
toolTip3.Show( L,这个,pictureBox3.Location);
IsToolTipsShown = true ;
}
if (IsAltDown&& keyList.Contains(e.KeyCode))
{
switch (e.KeyCode)
{
case Keys.K:
textBox1.Text = K;
break ;
case Keys.L:
textBox1.Text = L跨度>;
break ;
case Keys.U:
textBox1.Text = U跨度>;
break ;
}
textBox1.Text + = 按下Alt键< /跨度>; Alt键被释放时,
}
}
// 隐藏工具提示
私有 void Form2_KeyUp(对象发件人,KeyEventArgs e)
{
if (IsToolTipsShown&&!(Control.ModifierKeys == Keys.Alt))
{
toolTip1.Hide( this );
toolTip2.Hide( this );
toolTip3.Hide( this );
IsToolTipsShown = false ;
}
}你会注意到我在Form上的TextBox上写了一条消息,而不是显示一个MessageBox:这样做是为了避免走向的有时奇怪的副作用开发和拦截关键事件等事件时模态(暂停当前线程)。
我还使用Controls.ModifierKeys属性来确定Alt键是否关闭,或者<。blockquote>
你可以使用处理CMD来解决它
对于这个集合如下MDI表格的财产: -
MainMenustrip = menustrip1
i have MDI form and child of MDI form,
when user press ALT key,it displays shortcut letters as tooltip for picture box on form (as like when we press Alt in MS-Word).
i have used following two events of form & set KeyPreview=true:-
private void Form1_KeyDown(object sender, KeyEventArgs e) { if (e.Alt) { toolTip1.Show("U", this, pic1.Location); toolTip2.Show("K", this, pictureBox2.Location); toolTip3.Show("L", this, pictureBox3.Location); } }
protected override void OnKeyPress(KeyPressEventArgs ex) { MessageBox.Show(ex.KeyChar.ToString()); string xo = ex.KeyChar.ToString(); if (xo == "k") //You pressed "q" key on the keyboard { MessageBox.Show("K pressed"); } else if (xo == "j") //You pressed "q" key on the keyboard { MessageBox.Show("Chart control event pressed"); } }
When alt key is pressed fires Form1_KeyDown event
& shows all tooltip letters for assigned picture box,
after that if i press 'k' key from keyboard, it will not fires OnKeyPress event,
then again i press 'k' key from keyboard, it will fires OnKeyPress event.
after that all works fine.
Why this happens for MDI form.解决方案There's a much easier way to do this that doesn't require over-riding 'OnKeyPress. Assume the KeyDown and KeyUp EventHandlers shown below are wired-up to your MDIChildForm, and that its KeyPreview Property is set to 'true.private bool IsAltDown = false; private bool IsToolTipsShown = false; private List<Keys> keyList = new List<Keys> { Keys.K, Keys.L, Keys.U }; private void Form2_KeyDown(object sender, KeyEventArgs e) { IsAltDown = Control.ModifierKeys == Keys.Alt; if (IsAltDown) { toolTip1.Show("U", this, pictureBox1.Location); toolTip2.Show("K", this, pictureBox2.Location); toolTip3.Show("L", this, pictureBox3.Location); IsToolTipsShown = true; } if (IsAltDown && keyList.Contains(e.KeyCode)) { switch (e.KeyCode) { case Keys.K: textBox1.Text = "K"; break; case Keys.L: textBox1.Text = "L"; break; case Keys.U: textBox1.Text = "U"; break; } textBox1.Text += " pressed with Alt Key Down"; } } // when the Alt key is released hide the ToolTips private void Form2_KeyUp(object sender, KeyEventArgs e) { if(IsToolTipsShown && ! (Control.ModifierKeys == Keys.Alt)) { toolTip1.Hide(this); toolTip2.Hide(this); toolTip3.Hide(this); IsToolTipsShown = false; } }You'll note that I wrote a message to a TextBox on the Form rather showing a MessageBox: that's done to avoid the sometimes strange side-effects of "going modal" (suspending the current thread) when developing and intercepting events like Key Events.
I also used the Controls.ModifierKeys Property to determine whether the Alt Key is down, or up.
you can resolve it by usingProcess CMD
For this set following property of MDI Form:-
MainMenustrip=menustrip1
这篇关于表格按键事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!