问题描述
我有一个测试计算器-它的设置带有2个文本框(主要的一个和侧面的一个)
最主要的地方是它显示用户输入的第一个数字(字符串number1-是,以后将其转换),然后当用户选择诸如Add,Subtract,divide或Multiply之类的工具时,number1会移动到较小的textbox和textBox1清除,以便用户在第一个输入停留在小文本框2中时进入主框. (如果不清楚,请提问)
基本上,当他们选择添加(或其他工具)时,number1会移动到textbox2,但textbox1不会写用户输入的number2
包括一些示例代码:
I have a test calculator - It''s setup with 2 textboxes (the main one and a side one)
the main one is where it shows the users input for the first number (string number1 - yes sring ill convert it later) and then when the users selects a tool like Add, Subtract, Divide or Multiply, number1 travels to the smaller textbox and textBox1 clears so the user enters the main box while their first input stays in the small textBox2. (ask questions if im unclear)
basically when they select Add (or another tool) the number1 travels to textbox2 but textbox1 does not write number2 which the user enters
ill include some sample code:
static public string number1 = String.Empty;
static public string number2 = String.Empty; //declare empty strings
static public string spare = String.Empty;
//this adds 6 to the string number1 or number2
private void button6_Click(object sender, EventArgs e) //add 6 to string
{
if (num1)
{
number1 = number1 + "6";
textBox1.Text = number1;
}
if (!num1)
{
number2 = number2 + "6";
textBox1.Text = number2;
}
}
//这是将数字1发送到备用&的添加"按钮.到textbox2
//然后将number2写入textBox1中,以供用户输入
中的数字
//this is the Add button which sends number1 to spare & to textbox2
//then writes number2 into textBox1 for the user to intput numbers in
private void button12_Click(object sender, EventArgs e) //add
{
add = true;
spare = number1;
number1 = String.Empty;
textBox2.Text = spare;
textBox1.Text = number2;
num1 = false;
num2 = true;
}
但是textBox1输入时不会写number2,而是保持空白
为什么?
but textBox1 doesnt write number2 when they enter it, it just stays blank
why?
推荐答案
private void button6_Click(object sender, EventArgs e) //add 6 to string
{
textBox1.Text += "6";
}
直接从TextBox1.Text复制到TextBox2.Text,而完全忽略num1,num2,number1和number2.
另外,我会做一个小改动:
每个控件都有一个Tag属性.对于每个数字按钮,将其标记为该字符.
1个按钮->标签=="1"
2按钮->标签=="2"
...
然后为所有数字按钮设置一个处理程序例程:
Copy from TextBox1.Text to TextBox2.Text directly and ignore num1, num2 number1 and number2 completely.
In addition, I would make a small change:
Every control has a Tag property. For each of your number buttons, make the Tag that character.
1 button -> Tag == "1"
2 button -> Tag == "2"
...
Then have a single handler routine for all number buttons:
private void NumberButton_Click(object sender, EventArgs e) //add 6 to string
{
Button b = sender as Button;
if( b != null)
{
textBox1.Text += (string) b.Tag;
}
}
这样,您不必在十个不同的地方更改代码即可更改按钮的处理方式.
而且,请不要使用Visual Studio的默认名称!下周返回代码时,button12
不如butAdd
明显,并且它可以节省大量时间来尝试确定哪个按钮可以做什么.
[edit]错别字-OriginalGriff [/edit]
This way, you don''t have to change the code in ten different places to alter your button handling.
And pretty please, don''t use the Visual studio default names for things! button12
is not as obvious as butAdd
when you come back to the code next week, and it can save a lot of time trying to work out which button does what.
[edit]Typos - OriginalGriff[/edit]
//add multiply or division etc click
private void button6_Click(object sender, EventArgs e) //add 6 to string
{
if(textBox1.Text != string.Empty && textBox2.Text!=string.Empty)
{
switch(((Button)sender).Text)
{
case "+":
textBox1.Text=""+(Covert.toDouble(textBox1.Text) + Covert.toDouble(textBox2.Text));
break;
case "-":
textBox1.Text=""+(Covert.toDouble(textBox1.Text) - Covert.toDouble(textBox2.Text));
break;
case "/":
textBox1.Text=""+(Covert.toDouble(textBox1.Text) / Covert.toDouble(textBox2.Text));
break;
case "X":
textBox1.Text=""+(Covert.toDouble(textBox1.Text) * Covert.toDouble(textBox2.Text));
break;
}
}
else if(textBox2.Text==string.Empty && textBox1.Text != string.Empty)
{
textBox2.Text = textBox1.Text;
}
}
您可以添加大小写以添加乘法或除法或..
问候
you can add case for add multiply or division or ..
regards,
这篇关于C#计算器测试-需要帮助(基本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!