C#多行计算 [英] C# multiline calculation

查看:184
本文介绍了C#多行计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在也开始对C#。我在解决一个问题我的讲师要求我做了一个问题。下面是GUI。

I'm starting on C# now. I had a problem in tackling a question my lecturer asked me to do. Below is the GUI.

HTTP://i.share。 pho.to/daa36a24_c.png

这是我做的代码,但我没能部分代码如下

This is the code i did but i didn't manage to code part the i mentioned below

      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Linq;
      using System.Text;
      using System.Windows.Forms;

      namespace WindowsFormsApplication1
       {
          public partial class Form1 : Form
          {
              public Form1()
              {
                  InitializeComponent();
              }

              private void button1_Click(object sender, EventArgs e)
              {
                  double num1;
                  double num2;
                  double answer;

                  num1 = double.Parse(textBox1.Text);
                  num2 = double.Parse(textBox2.Text);


                  textBox4.Text = Convert.ToString(answer);
              }
          }
      }



我将需要添加/减去/多个/分的第一和第二数目,这样它会产生 - >(第一号+操作+第二数量=答案)

I will need to add/subtract/multiple/divide first and second number so that it will produce --> (first number + the operation + second number = the answer).

问题是我需要通过点击+,选择操作 - 在文本框中,*,/符号。我可以通过使用单选按钮或等容易做,但我的讲师坚持这种格式。请不要助攻操作选项的编码。谢谢你。

The problem is i need to select the operation by clicking on the + , - , * , / symbols on the textbox. I could do it easily by using radio button or etc but my lecturer insist on this format. Please do assist for the coding of "operation" selection. Thank you.

推荐答案

您使用列表框的 OnIndexChanged 事件知道选中了哪个运营商。

You use the OnIndexChanged event of a listbox to know which operator was selected.

这将允许你来计算列表框中的每一次点击。

This will allow you to calculate on each click of the list box.

operatorListBox1_SelectedIndexChanged 事件方法的通知,您可以使用发件人(被点击的对象)找到的SelectedItem 。投射此为一个字符串(它在列表框中的对象)和你的星座就会出现。 (没有双关语意)

Notice in the operatorListBox1_SelectedIndexChanged event method, you use sender (the object that was clicked on) to find the SelectedItem. Cast this to a string (its an object in the listbox) and your sign will appear. (no pun intended)

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private int firstNum = 2;
        private int secondNum = 4;
        private int answer;

        public Form2()
        {
            InitializeComponent();
            operatorListBox1.Items.Add("+");
            operatorListBox1.Items.Add("-");
            operatorListBox1.Items.Add("*");
            operatorListBox1.Items.Add("/");
            //this next line would go in your designer.cs file.  I put it here for completeness
            this.operatorListBox1.SelectedIndexChanged += new System.EventHandler(this.operatorListBox1_SelectedIndexChanged);
        }

        private void operatorListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            calculateAnswer(((ListBox)sender).SelectedItem.ToString());
        }

        private void calculateAnswer(string sign)
        {
            switch (sign)
            {
                case "+":
                    answer = firstNum + secondNum;
                    break;
                case "-":
                    answer = firstNum - secondNum;
                    break;
                case "*":
                    answer = firstNum * secondNum;
                    break;
                case "/":
                    answer = firstNum / secondNum;
                    break;
            }
            textBox4.Text = firstNum + " " + sign + " " + secondNum + " = " + answer;
        }
    }
}

这篇关于C#多行计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆