问题描述
我现在正在学习C#,所以我正在做一些练习以习惯C#语法并学得更好。我决定制作一个看起来像普通Windows计算器的计算器。
我只创建了一个按钮 1和一个文本框。
我想让此按钮在按下时在文本框中写入1,并且还要使int变量等于课本中的数字,以便稍后进行计算。因此,我无法更改 int a的值或更改文本框中的文本,它始终显示01,因为a始终等于0。
如何制作程序,同时显示正确的数字和正确更改值?
例如,当我按下按钮两次并同时将 int a的值更改为11时,如何使程序在文本框中显示11个?
公共局部类Form1:形式
{
int a;
字符串Sa;
公共Form1()
{
InitializeComponent();
}
private void button1_Click(对象发送者,EventArgs e)
{
Sa = a.ToString()+ 1;
textBox1.Text = Sa;
}
private void textBox1_TextChanged(对象发送者,EventArgs e)
{
}
}
私有无效button1_Click(对象发送者,EventArgs e)
{
textBox1.Text + = 1;
}
private void textBox1_TextChanged(object sender,EventArgs e)
{
a = Int32.Parse(textBox1.Text);
}
调整它。每单击一次按钮,更改textBox上的文本,然后更改变量a每个文本框都更改了。
I am studying C# now, so i am making some kind of exercises to get used to C# syntax and learn better. I decided to make a calculator looking alike the normal windows calculator.
I created only one button "1" and one textbox.
I want to make this button write 1 in textbox when I press it and also make an int variable equal to the number in the textbook, to make the calculation later. So I can't either change "int a"'s value or change text in the textbox, it always shows 01, because a always equals to 0.How can I make the program, both show the correct numbers and change the value of a correctly?For example how can i make the program show eleven in the textbox when i press button twice and also change "int a"'s value to 11?
public partial class Form1 : Form
{
int a;
string Sa;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Sa = a.ToString() + "1";
textBox1.Text = Sa;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text += "1";
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
a = Int32.Parse(textBox1.Text);
}
just it.. Change text on textBox every button click, and change variable a every textBox changed.
这篇关于按下按钮输入C#计算器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!