使用Esc(27)键C#取消numericUpDown的问题

时间:2015-01-27 11:35:27

标签: c# winforms event-handling keypress numericupdown

我有一个带有访问数据库的winform应用程序。 表单有一个numericUpDown来更改值。取消更改的唯一方法是使用取消按钮或按esc。因为当用户按下enter / a confirmbutton或 更改单元格,调用UpdateCell()函数:

    dtpGridEdit.Validating += (s, e) => { UpdateCell(); };
            comboBoxGridEdit.Validating += (s, e) => { UpdateCell(); };
            textBoxGridEdit.Validating += (s, e) => { UpdateCell(); };
            numericUpDown1.Validating += (s, e) => { UpdateCell(); };

            //CancelButton: 
            buttonGridCancel.Click += (s, e) => { CancelCell(); };

textBoxGridEdit.KeyPress += (s, e) =>
            {
                if (e.KeyChar == (char)13)  // enter in update textbox
                {
                    e.Handled = true;   // eat the enter in this multilinebox
                    UpdateCell();
                }

                if (e.KeyChar == (char)27)  // cancel (Escape) in update textbox
                {
                    CancelCell();
                }
            };

           numericUpDown1.KeyPress += (s, e) =>
            {
                numericUpDown1.Focus(); 

                if (e.KeyChar == (char)13)  // enter in update numericUpDown
                {
                    e.Handled = true;
                    UpdateCell();
                }

                if (e.KeyChar == (char)27)  // cancel (Escape) in update numericUpDown
                {
                    e.Handled = true;
                    CancelCell();
                }
            };

如果用户使用numericUpDown的箭头,则可以使用。 如果用户使用了numericUpDown中的文本框,则可以使用 取消按钮但它不适用于按esc功能。

在其他情况下,例如dataGrid / ComboBox / TextBox,它可以以任何方式工作。 问题只在于NumerbicUpDown中的文本框和esc key.press函数之间的组合,但是我到处搜索 而且我找不到溶剂(我是编程中的新手)

0 个答案:

没有答案
相关问题