我是这个网站的新手,对编程也很陌生。
我正在用 C# 制作一个计算器,我想知道在按下按钮后如何让数字出现在文本框中。
谢谢!
您需要向按钮事件处理程序添加代码:
public void Button1_Click(Object sender, EventArgs e)
{
TextBox1.Text = "1";
}
这会将string
“1”显示到文本框中。
您还可以在网页上有多个文本框并使用:
public void Button1_Click(Object sender, EventArgs e)
{
string input1 = txtInput.Text;
string input2 = txtInput2.Text;
int userInput;
int userInput2;
int result;
Int32.TryParse(input1, out userInput);
Int32.TryParse(input2, out userInput2);
result = userInput + userInput2;
txtAnswer.Text = "The answer is: " result.ToString();
}
我已经包含了一个TryParse
示例,该示例演示了从字符串到整数的转换是否成功。