2013-09-29 43 views
0

我現在開始使用C#。在解決我的講師要求我做的一個問題時,我遇到了一個問題。下面是GUI。C#多行計算

http://i.share.pho.to/daa36a24_c.png

這是代碼我沒有,但我沒能部分代碼如下

 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); 
       } 
      } 
     } 

提到我,我將需要加/減/多/分第一和第二號所以它會產生 - >(第一個數字+操作+第二個數字=答案)。

問題是我需要通過點擊文本框上的+, - ,*,/符號來選擇操作。我可以使用單選按鈕等方式輕鬆完成,但我的講師堅持使用這種格式。請協助「操作」選擇的編碼。謝謝。

+1

顯示你的代碼,而不是像.. –

+0

我期待我怎樣才能選擇操作(+, - ,*,/),當我點擊它的文本框和程序理解我正在選擇什麼操作。 – Roshan

回答

0

你用一個列表框的OnIndexChanged事件知道選擇哪個運營商。

這將允許您計算每次點擊列表框。請使用sender(點擊的對象)找到SelectedItem。將其轉換爲一個字符串(它是列表框中的一個對象)並且您的符號將出現。 (沒有雙關語意)

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; 
     } 
    } 
} 
+1

謝謝你的男人。你做了我的一天:)現在我知道它是列表框,而不是文本框,因爲我無法在多行文本框中完成。 – Roshan

1

只要操作都在一個列表框,使用此:

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); 
       if (listBox1.SelectedIndex == 0) 
       { 
       answer = num1 + num2 
       } 
       if (listBox1.SelectedIndex == 1) 
       { 
       answer = num1 - num2 
       } 
       if (listBox1.SelectedIndex == 2) 
       { 
       answer = num1 * num2 
       } 
       if (listBox1.SelectedIndex == 3) 
       { 
       answer = num1/num2 
       } 
       textBox4.Text = Convert.ToString(answer); 
      } 
     } 
    } 
+0

感謝你的努力喬,你的方法確實奏效。 – Roshan

+0

+1以實現良好。 :) – paqogomez