gpt4 book ai didi

c# - 不能加整数

转载 作者:太空宇宙 更新时间:2023-11-03 17:13:25 26 4
gpt4 key购买 nike

好的,我是 C# 的新手,我想添加两个用户输入到文本框中的值。这是我的代码,我收到“val1 + val2”的错误。如何添加这些值?如果之前有人问过这个问题,我真的很抱歉,但我已经搜索过答案,但我就是找不到。谢谢 :)。

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 Csharp_Calculator
{

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonAdd_Click(object sender, EventArgs e)
{
int val1 = Convert.ToInt32(textBox1.Text);
int val2 = Convert.ToInt32(textBox2.Text);
textBoxAns.Text = val1 + val2;
}

private void buttonSub_Click(object sender, EventArgs e)
{
int val1 = Convert.ToInt32(textBox1.Text);
int val2 = Convert.ToInt32(textBox2.Text);
textBoxAns.Text = val1 + val2;
}

private void buttonDivide_Click(object sender, EventArgs e)
{
int val1 = Convert.ToInt32(textBox1.Text);
int val2 = Convert.ToInt32(textBox2.Text);
textBoxAns.Text = val1 + val2;
}

private void buttonRefresh_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
textBoxAns.Text = "";
}
}
}

最佳答案

尝试这样的事情:

textBoxAns.Text = (val1 + val2).ToString();

关于c# - 不能加整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21539187/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com