功能需求是回车键和tab键的功能要求一样,但是回车键按下后,keypress事件可以进去,但是tab键按下,keypress事件进不去,为什么?
private void TextBoxKeypressBit14(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar == 13) || (e.KeyChar == (char)Keys.Tab))
{
e.Handled = true;
TextBox tb = (TextBox)sender;
string tbname = tb.Name;
string txt = tb.Text;
int x = Convert.ToInt32(tbname.Substring(7, 3));
string xuhao = "";
for (int i = 0; i < alldevices.Count / 10; i++)
{
if (alldevices[i * 10] == label49.Text)
{
xuhao = alldevices[i * 10 + 8];
}
}
//if (txt == "") { txt=;}
try
{
string strMsg = "update Bit14 " + xuhao + " 数据" + (x - 140).ToString() + " " + txt;
byte[] buffer = new byte[2048];
buffer = Encoding.Default.GetBytes(strMsg);
int receive = Program.socketSend.Send(buffer);
}
catch { }
Receive();
if (x == 180)
{
textBox141.Focus();
}
else { this.groupBox5.Controls["textBox" + (x + 1).ToString()].Focus(); }
for (int i = 0; i < 40; i++)
{
yuan[i] = this.groupBox5.Controls["textBox" + (i + 141).ToString()].Text;
}
}
if(e.KeyChar==27)
{
for (int i = 0; i < 40; i++)
{
this.groupBox5.Controls["textBox" + (i + 141).ToString()].Text = yuan[i];
}
}
}
tab键本身自带就是换标签元素的功能吧
一些按键,比如TAB,RETURN,ESC和箭头键不被认为是输入按键被忽略.
通过处理PreviewKeyDown控件事件可以将这些键设置为输入键.
private void textBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if(e.KeyData == Keys.Tab)
{
MessageBox.Show("Tab");
e.IsInputKey = true;
}
if (e.KeyData == (Keys.Tab | Keys.Shift))
{
MessageBox.Show("Shift + Tab");
e.IsInputKey = true;
}
}