2016-07-29 62 views
0

帶有按鈕,文本框和一些單選按鈕的Windows窗體。 a,b,c是根據哪些單選按鈕被檢查通過某些數學的變量(整數)。假設最後a = 15,b = 20,c = 10想法是,點擊按鈕之類的東西: 結果是:a = 15,b = 20,c = 10 必須顯示爲文本框中的文本,其中15,20和10是a,b和c的最終值。問題:Windows窗體:文本框必須顯示變量的值

  • 我該在哪裏聲明變量?
  • 我在哪裏執行數學?這很簡單的東西:

    if(radiobutton1.Checked == true) 
        a=a+5; 
    
  • 如何通過單擊按鈕獲得文本框中的結果?

回答

0

聲明您的變量爲私有的,就像您窗體中的第一件事。做一個按鈕單擊事件的計算:

public partial class Form1 : Form 
    { 
     private int a, b, c; 

     private void button1_Click(object sender, EventArgs e) 
     { 
      if (radioButton1.Checked) a = 5; else a = 0; 
      if (radioButton2.Checked) b = 5; else b = 0; 
      if (radioButton3.Checked) c = 5; else c = 0; 
     } 
    }   

您可以使用String.Format格式化輸出:

textBox1.Text = String.Format("a is {0}, b is {1}, c is {2}", a, b, c); 

如果使用VS2015字符串插值:

textBox1.Text = $"a is {a}, b is {b}, c is {c}";