0

当用户在键盘上按 Enter 键时,我已经有代码,它返回选项卡并“跳转”到下一个字段,它工作得很好,它可能使它用于 2 或 3 个文本框,当需要在多个文本框上制作时出现问题,例如每个文本框 20 个形式,它只是不起作用。

见代码:

// Detect if Enter key is pressed on each text box, mute sound enter "ding"  sound and replace Enter for tab (problem that have make it for each textbox)
        private void txtAltura_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                e.Handled = true; //Silenciar Enter
                SendKeys.Send("{TAB}");
            }
        }

        private void txtLargura_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                e.Handled = true; //Silenciar Enter
                SendKeys.Send("{TAB}");
            }
        }

        private void txtProfundidade_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                e.Handled = true; //Silenciar Enter
                SendKeys.Send("{TAB}");
            }
        }

//execute keypress command when enter is typed on textbox 
    private void txtProfundidade_TextChanged(object sender, EventArgs e)
            {
                if (txtProfundidade.Text != "") { foreach (char c in txtProfundidade.Text.ToCharArray()) txtProfundidade_KeyPress(sender, new KeyPressEventArgs(c)); }

            }

            private void txtLargura_TextChanged(object sender, EventArgs e)
            {
                if (txtLargura.Text != "") { foreach (char c in txtLargura.Text.ToCharArray()) txtLargura_KeyPress(sender, new KeyPressEventArgs(c)); }
            }

            private void txtAltura_TextChanged(object sender, EventArgs e)
            {
                if (txtAltura.Text != ""){foreach (char c in txtAltura.Text.ToCharArray()) txtAltura_KeyPress(sender, new KeyPressEventArgs(c));}
            }

希望让它变得更好。

提前致谢..

4

3 回答 3

1

据我了解,您正试图找出一种将事件处理程序分配给多个文本框控件的方法,并且不想为每个控件编写一个处理程序。如果是这种情况,试试这个:

private void Form1_Load(object sender, EventArgs e)
{
    foreach (TextBox textBox in this.Controls.OfType<TextBox>())
    {
        textBox.KeyDown += new KeyEventHandler(textBox_KeyDown);
    }
}

void textBox_KeyDown(object sender, KeyEventArgs e)
{
   if (e.KeyCode == Keys.Enter)
   {
      e.Handled = true;
      SendKeys.Send("{TAB}");
   }
}

这将为表单上的每个文本框控件分配一个处理程序。

于 2013-04-12T04:17:35.647 回答
1

如果它的 windows 窗体应用程序可以使用它,这将用 Enter 键替换 tab 键

protected override bool ProcessKeyPreview(ref Message m)
            {
                if (m.Msg == 0x0100 && (int)m.WParam == 13)
                {
                    this.ProcessTabKey(true);
                }
                return base.ProcessKeyPreview(ref m);
            }
于 2013-04-12T04:21:09.677 回答
0

那效果很好。

我还必须在每个文本框上重复此操作,以检查是否所有代码都已填写并更新状态栏:

        private void txtAltura_TextChanged(object sender, EventArgs e)
        {
            testePreenchidoecalculo();
        }

        private void txtLargura_TextChanged(object sender, EventArgs e)
        {
            testePreenchidoecalculo();
        }

        private void txtProfundidade_TextChanged(object sender, EventArgs e)
        {
            testePreenchidoecalculo();
        }

有可能让它变得更好吗?

于 2013-04-12T18:14:38.423 回答