private void textEdit1_Leave(object sender, EventArgs e)
        {
     
            if (dtPrint.Rows.Count > 0 && company != textEdit1.Text.Trim())
            {
                if (MessageBox.Show("客户名称不一样,是否取消打印之前录入的运单?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    dtPrint.Clear();
                    PrintBtn.Visible = false;
                    print_totalamt = 0.0;
                }
            }
        }private void textEdit1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)//回车
                textEdit3.Focus();
        }为什么回车会出现两次对话框,如何解决?

解决方案 »

  1.   

    textEdit3.Focus(); 
    这句后面加一句
    e.Handled = true;
      

  2.   

     leave 是在控件失去焦点的时候触发.
    当你按回车的时候,KeyDown和leave都捕捉到了,所以依此触发,就是2次.
      

  3.   

    我做了个简单的winform没有发现lz的问题: private void textBox2_Leave(object sender, EventArgs e)
            {
                if (MessageBox.Show("adfasfdasfd", "adasfd", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    textBox2.Clear();
                    textBox2.Text = "111";
                }
            }        private void textBox2_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyValue == 13)
                {
                    textBox3.Focus();
                }        }
      

  4.   

    我测试了下,没发现你的问题.
    当原来的焦点在textBox1上按回车时,才有一个框出来.
    namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void textBox1_Leave(object sender, EventArgs e)
            {
                MessageBox.Show("leave");
            }        private void textBox1_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyValue == 13)//回车 
                    textBox2.Focus(); 
            }
        }
    }
      

  5.   


    很明显,你的textEdit3.Focus();又触发了textEdit1_Leave事件~
      

  6.   

    楼主检查一下
    this.textEdit1.Leave += new System.EventHandler(this.textEdit1_Leave);
    注册了几次~
      

  7.   

    在textEdit3.Focus(); 后面把 textEdit1_Leave()  卸掉
      

  8.   

    这里发言,表示您接受了CSDN社区的用户行为准则。 
    请对您的言行负责,并遵守中华人民共和国有关法律法规,尊重网上道德。 
    转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。 
      

  9.   

    问题是在textEdit3这个控件问题,但不知是什么原因,把这个换成textBox就可以,不用第三方控件。