Form1_keydown事件在分隔符和返回键时不起作用 [英] Form1_keydown event doesn't work right when it comes to separator and return keys

查看:61
本文介绍了Form1_keydown事件在分隔符和返回键时不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用简单的计算器来处理键盘事件。到目前为止一切顺利,除了Keys.Separator和Keys.Return。 =按钮工作正常,这两个keydown事件也与按钮具有相同的代码。它的工作方式很奇怪,就像我说我要总计50和2,当我按下返回或numpad进入查看结果时,它只是不断向第二个变量添加1(无论什么是变量,它总是重复 1s),如果我按一次结果显示为21,如果我一直按回车或小键盘输入,则变为21111111 ...



其他KeyDown事件工作正常,我想知道如果这两个钥匙确实有一个特殊的工作方式......



以下是活动代码:



I was trying to make my simple calculator to work with keyboard events. All is well so far, except Keys.Separator and Keys.Return. = button works fine and these two keydown events also have same codes with the button. it works strangely, like lets say I want to totalize 50 and 2, when I press return or numpad enter to see the result, it just keeps adding "1"s to second variable(doesnt matter what are the variables, it always duplicates "1"s), If I press one time result is shown as 21, If I keep pressing return or numpad enter, it becomes 21111111...

Other KeyDown events work fine, I wonder If these two Keys do have a special way of work...

Here is the code of the event:

private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Separator||e.KeyCode==Keys.Return)
            {
                if (equal)
                {
                    sayi1 = sonuc;
                }
                else
                {
                    sayi2 = Convert.ToDouble(label1.Text);
                }
                if (topla)
                {
                    sonuc = sayi1 + sayi2;
                    label1.Text = (sonuc).ToString();
                }
                else if (cikar)
                {
                    sonuc = sayi1 - sayi2;
                    label1.Text = (sonuc).ToString();
                }
                else if (carp)
                {
                    sonuc = sayi1 * sayi2;
                    label1.Text = (sonuc).ToString();
                }
                else
                {
                    sonuc = sayi1 / sayi2;
                    label1.Text = sonuc.ToString();
                }
                sayi = sonuc.ToString();
                equal = true;
            }







代码的其他部分如何工作:






How other parts of the code work:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            KeyPreview = true;
        }

        public string sayi;//this variable is probably useless, i will work on it later
        public double sayi1, sayi2, sonuc;//var1,var2,result
        public bool topla, cikar, carp, bol, equal;//+,-,*,/







        private void button1_Click(object sender, EventArgs e)//1 button
        {
            if ((topla || cikar || carp || bol) && sayi == "")
                label1.Text = "";
            sayi += "1";
            label1.Text += "1";

        }

        private void button17_Click(object sender, EventArgs e)//c clear button
        {

            sayi = "";
            label1.Text = sayi;
            equal = false;
        }

        
        private void button14_Click(object sender, EventArgs e)//- process
        {
            sayi1 = double.Parse(label1.Text);
            sayi = "";
            equal = false;
            cikar = true;
            topla = false; carp = false; bol = false;
        }

        
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Separator||e.KeyCode==Keys.Return)
            {
                if (equal)
                {
                    sayi1 = sonuc;
                }
                else
                {
                    sayi2 = Convert.ToDouble(label1.Text);
                }
                if (topla)
                {
                    sonuc = sayi1 + sayi2;
                    label1.Text = (sonuc).ToString();
                }
                else if (cikar)
                {
                    sonuc = sayi1 - sayi2;
                    label1.Text = (sonuc).ToString();
                }
                else if (carp)
                {
                    sonuc = sayi1 * sayi2;
                    label1.Text = (sonuc).ToString();
                }
                else
                {
                    sonuc = sayi1 / sayi2;
                    label1.Text = sonuc.ToString();
                }
                sayi = sonuc.ToString();
                equal = true;
            }
            

            else if(e.KeyCode == Keys.D1 || e.KeyCode == Keys.NumPad1)
            {
                if ((topla || cikar || carp || bol) && sayi == "")
                    label1.Text = "";
                sayi += "1";
                label1.Text += "1";
            }

        
        private void button12_Click(object sender, EventArgs e)// = button
        {
            if (equal)
            {
                sayi1 = sonuc;
            }
            else
            {
                sayi2 = Convert.ToDouble(label1.Text);
            }
            if (topla)
            {
                sonuc = sayi1 + sayi2;
                label1.Text = (sonuc).ToString();
            }
            else if (cikar)
            {
                sonuc = sayi1 - sayi2;
                label1.Text = (sonuc).ToString();
            }
            else if (carp)
            {
                sonuc = sayi1 * sayi2;
                label1.Text = (sonuc).ToString();
            }
            else
            {
                sonuc = sayi1 / sayi2;
                label1.Text = sonuc.ToString();
            }
            sayi = sonuc.ToString();
            equal = true;
        }

        
    }





我的尝试:



我分别对这两个事件进行了编码,但没有什么区别。



What I have tried:

I coded these two events separately but didn't make a difference.

推荐答案

Enter是一个特殊键,用于激活按钮,而不是默认情况下通过KeyDown事件处理程序传递。

为了证明这一点,在Form1_keyDown处理程序中添加一个断点,你会明白我的意思 - 它永远不会受到打击。事实上,如果你的表单上有一个输入控件,如文本框或按钮,除非Form.KeyPreview属性设置为true,否则甚至不会为表单生成事件。

要捕获在表单中输入键,最好将=按钮指定为表单的AcceptButton,因为这将导致ENTER单击按钮。
Enter is a "special key" which activates a button, rather than being passed through the KeyDown event handler by default.
To prove this, add a breakpoint to your Form1_keyDown handler and you will see what I mean - it never gets hit. In fact, if you have an input control such as a textbox or button on your form KeyDown events are not even generated for the form unless the Form.KeyPreview property is set to true.
To "catch" the Enter key in a form, you would be best off assigning the "=" button as the AcceptButton for the form as that will cause ENTER to click the button.


它似乎是关于按钮的焦点方法。我将所有按钮的TabIndex属性设置为False,并在每个单击事件中添加了label1.Focus()方法。这些解决了这个问题。将=声明为主窗体的AcceptButton,导致TabIndex = false不能用于=按钮,所以我不得不使用KeyDown事件进入enter和numpad再次输入。
It seems it was about buttons' focus method. I set all of the buttons' TabIndex properties as False, and added label1.Focus() method in each click event. These solved the problem. Declaring "=" as the AcceptButton of the main form, caused to TabIndex=false didnt work for "=" button, so I had to use KeyDown event for enter and numpad enter again.


这篇关于Form1_keydown事件在分隔符和返回键时不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆