键盘输入。 [英] Keyboard Input.

查看:86
本文介绍了键盘输入。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道它应该如此简单。我知道有关于此的数百个主题。我把它们都读了。我无法做对。我正在winforms中做一个计算器,并尝试从按钮和输入键盘输入。这是我的计算器代码:

I know it's supposed to be so simple. I know there's hundreds of threads about this. I read them all. I just can't get it right. I'm doing a calculator in winforms and trying to make input from buttons AND input keyboard. this is my code for the calculator:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Calculator__
{
	public partial class Calculator : Form
	{

		string num1 = "", num2 = "";
		string sign = "";


		public Calculator()
		{
			InitializeComponent();
			OutputBar.SelectionAlignment = HorizontalAlignment.Center;
		}




		private void ZeroButtonClick(object sender, EventArgs e)
		{
			if(sign != "")
			{
				num2 = num2 + "0";
			}
			OutputBar.AppendText("0");
		}
		private void OneButtonClick(object sender, EventArgs e)
		{
			if (sign != "")
			{
				num2 = num2 + "1";
			}
			OutputBar.AppendText("1");
		}
		private void TwoButtonClick(object sender, EventArgs e)
		{
			if (sign != "")
			{
				num2 = num2 + "2";
			}
			OutputBar.AppendText("2");
		}
		private void ThreeButtonClick(object sender, EventArgs e)
		{
			if (sign != "")
			{
				num2 = num2 + "3";
			}
			OutputBar.AppendText("3");
		}
		private void FourButtonClick(object sender, EventArgs e)
		{
			if (sign != "")
			{
				num2 = num2 + "4";
			}
			OutputBar.AppendText("4");
		}
		private void FiveButtonClick(object sender, EventArgs e)
		{
			if (sign != "")
			{
				num2 = num2 + "5";
			}
			OutputBar.AppendText("5");
		}
		private void SixButtonClick(object sender, EventArgs e)
		{
			if (sign != "")
			{
				num2 = num2 + "6";
			}
			OutputBar.AppendText("6");
		}
		private void SevenButtonClick(object sender, EventArgs e)
		{
			if (sign != "")
			{
				num2 = num2 + "7";
			}
			OutputBar.AppendText("7");
		}
		private void EightButtonClick(object sender, EventArgs e)
		{
			if (sign != "")
			{
				num2 = num2 + "8";
			}
			OutputBar.AppendText("8");
		}
		private void NineButtonClick(object sender, EventArgs e)
		{
			if (sign != "")
			{
				num2 = num2 + "9";
			}
			OutputBar.AppendText("9");
		}


		private void DotButtonClick(object sender, EventArgs e)
		{
			OutputBar.AppendText(".");
		}

		private void ClearButtonClick(object sender, EventArgs e)
		{
			OutputBar.Text = "";
			num1 = "";
			num2 = "";
			sign = "";
		}


		private void PlusButtonClick(object sender, EventArgs e)
		{
			if (sign == "")
			{
				num1 = OutputBar.Text;
				OutputBar.AppendText("+");
				sign = "+";
			}	
		}
		private void MinusButtonClick(object sender, EventArgs e)
		{
			if (sign == "")
			{
				num1 = OutputBar.Text;
				OutputBar.AppendText("-");
				sign = "-";
			}
		}
		private void MultiplyButtonClick(object sender, EventArgs e)
		{
			if (sign == "")
			{
				num1 = OutputBar.Text;
				OutputBar.AppendText("*");
				sign = "*";
			}
		}
		private void DivideButtonClick(object sender, EventArgs e)
		{
			if (sign == "")
			{
				num1 = OutputBar.Text;
				OutputBar.AppendText("/");
				sign = "/";
			}
		}

		private void EqualButtonClick(object sender, EventArgs e)
		{
			
			if(num1 != "" && num2 != "")
			{

				if(sign == "+")
				{
					num1 = (double.Parse(num1) + double.Parse(num2)).ToString();
					OutputBar.Text = num1;
					num2 = "";
					sign = "";
				}
				if (sign == "-")
				{
					num1 = (double.Parse(num1) - double.Parse(num2)).ToString();
					OutputBar.Text = num1;
					num2 = "";
					sign = "";
				}
				if (sign == "*")
				{
					num1 = (double.Parse(num1) * double.Parse(num2)).ToString();
					OutputBar.Text = num1;
					num2 = "";
					sign = "";
				}
				if (sign == "/" && double.Parse(num2) != 0)
				{
					num1 = (double.Parse(num1) / double.Parse(num2)).ToString();
					OutputBar.Text = num1;
					num2 = "";
					sign = "";
				}
				if(sign == "/" && double.Parse(num2) == 0)
				{
					OutputBar.Text = "";
					num1 = "";
					num2 = "";
					sign = "";
				}

			}

		}

	}

}

它工作正常(除了我做0.5 * 0.5它得到2.5。更容易修复)。所以我找到了答案。他们告诉我这样做:

It works fine (except I did 0.5*0.5 it got 2.5. Easier fix tho). So I found the answer. They're telling me to do this:

if(ModifierKeys == Keys.NumPad1)
{

}

问题是我在哪里放这个。因为如果我把它放在公共计算器内它检查一次然后停止检查。如果我把它放在循环中,应用程序将无法工作(它将保持在循环中)。那我该怎么做呢?现在,我注意到还有其他的
方法,当我按下键(OnKeyPress)时会出现一个void。听起来很不错,但对我来说不起作用。

Question is where do I put this. Cuz if I put it Inside public Calculator it Checks once then stops checking. If I put it in a loop the Application won't work (it will just stay in the loop). So how do I make this work? Now, I noticed there are also other ways to do it with a void that starts when I press the key (OnKeyPress). Sounds good, but didn't work for me.

我一直在所有论坛上,有人可以修改我的脚本以使其正常工作吗?

I've been on all the forums, can someone please modify my script to make it work?

提前致谢。

推荐答案

Hi Soogbad,

Hi Soogbad,

>>除了我做了0.5 * 0.5它得到2.5

>>except I did 0.5*0.5 it got 2.5

这是因为当您输入"0.5"时,您没有成功添加"。"。在第二次,所以当你输入"0.5 * 0.5"时,程序得到"0.5 * 05"。实际上,原因是结果是"2.5"。

That is because you did not add '.' successful when you input the "0.5" at the second time, so when you input "0.5*0.5", the program got "0.5*05" actually, of cause the result is "2.5".

请将DotButtonClick中的代码更改为:

Please change the code within DotButtonClick to:

        private void DotButtonClick(object sender, EventArgs e)
        {
            if (sign != "")
            {
                num2 = num2 + ".";
            }
            OutputBar.AppendText(".");
        }

希望这会有所帮助!

最好的问候,

Stanly


这篇关于键盘输入。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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