c# - 不能加整数

标签 c# calculator

好的,我是 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/

相关文章:

javascript - 带有分数/小数的数学输入、计算和输出 Javascript

Java 计算器堆栈

c# - 如何删除 CaSTLe 3.0 中 Contribute ComponentModel 构造中的所有属性注入(inject)?

c# - 在自定义 Visual Studio 编辑器中覆盖字体

c# - 使用 C# 在 MySQL 中更新语句

javascript - 实时更新Javascript计算器答案

java - 计算器不处理两个运算符方程

c# - 如何使用C#检查SQL查询是否成功

c# - 将所选值从下拉列表转换为 int

parsing - 带括号的简单计算器如何工作?