简体   繁体   English

如何在 C# 中不使用单击事件(按钮)来计算总数?

[英]How to calculate a total without using a click event (button) in C#?

I'm doing a simple calculator using C# and I already made it work when I press a button, like this.我正在使用 C# 做一个简单的计算器,当我按下一个按钮时,我已经让它工作了,就像这样。

private void button1_Click(object sender, EventArgs e)
       {
           int a = int.Parse(textBox1.Text);
           int b = int.Parse(textBox2.Text);

           if (comboBox1.Text == "+")
               textBox3.Text = (a + b).ToString();

           if (comboBox1.Text == "-") 
               textBox3.Text = (a - b).ToString();

           if (comboBox1.Text == "*")
               textBox3.Text = (a * b).ToString();

           if (comboBox1.Text == "/") 
               textBox3.Text = (a / b).ToString();
       }

What I'm trying to do is for the result in textBox3 to change as I choose the operator from the comboBox without having to press a button.我想要做的是改变 textBox3 中的结果,因为我从组合框中选择运算符而无需按下按钮。 I've tried using the Leave event with no success.我试过使用 Leave 事件但没有成功。 Any help will be appreciated.任何帮助将不胜感激。

您可以连接到 SelectedIndexChange 事件

private void combobox1_textChanged(object sender, EventArgs e)
{
   int a = int.Parse(textBox1.Text);
       int b = int.Parse(textBox2.Text);

       if (comboBox1.Text == "+")
           textBox3.Text = (a + b).ToString();

       if (comboBox1.Text == "-") 
           textBox3.Text = (a - b).ToString();

       if (comboBox1.Text == "*")
           textBox3.Text = (a * b).ToString();

       if (comboBox1.Text == "/") 
           textBox3.Text = (a / b).ToString();
}

The above is pseduo code, but just add the "textChanged" event and put the code inside like shown above.以上是伪代码,但只需添加“textChanged”事件并将代码放入其中,如上所示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不使用点击事件C#计算总数 - Calculating a total without using a click event C# c#中如何使用Normal方法调用按钮点击事件? - How to call button click event using Normal method in c#? 是否可以在不使用 asp.net C# 中的 jquery 的情况下在按钮单击事件中打开弹出窗口? - Is it possible to open popup window in button click event without using jquery in asp.net C#? 如何使用C#计算DataTable列的总和(总计) - how to calculate sum (Total) of DataTable Columns using C# 如何在c#中按Enter键触发按钮点击事件 - How to trigger button click event on pressing enter button in c# 如何通过单击另一页面的链接按钮来调用按钮的单击事件,而无需在c#ASP.NET中回发 - How to call button's click event from click of linkbutton of another page without post back in c# ASP.NET 使用C#在Webkit浏览器中的动态按钮单击事件 - Dynamic button click event in webkit browser using C# Itemcommand 不会在使用 c# 的数据列表中触发按钮单击事件 - Itemcommand not firing on button click event in a datalist using c# 使用c#中的组合键来触发按钮单击事件 - Fire button click event using a key combination in c# 如何使用Selenium C#单击此按钮? - How to click this button using Selenium C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM