2016-10-23 30 views
0

我必須在私人無效文本框中輸入一個金額,並將該金額應用於等待Connection.SendToServerAsync(2700,790)的地方;就是現在。所以我們說,一個用戶在texbox進入2000年,8,那麼(2700,790)必須改變,以(2000,8)C# - 任何人都可以幫我一個文本框?

namespace Application 
{ 
    public partial class Form1 : ExtensionForm 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 
     private async void button1_Click(object sender, EventArgs e) 
     { 
      int repeat = 5; 

      for (int i = 0; i <= repeat; i++) 
      { 
       await Connection.SendToServerAsync(2700, 790); 
       await Connection.SendToServerAsync(3745); 
      } 
     } 

     private void textBox1_TextChanged(object sender, EventArgs e) 
     { 

     } 
    } 
} 

我得到這個作爲一個答案:

你可以得到使用TextBox.Text的文本框值。 它來作爲一個字符串,所以你必須轉換爲int。您可以使用以下方法之一來執行此操作: Int.Parse Convert.ToInt32 使用轉換的值,您可以在單擊按鈕時使用新值調用方法。

任何人都可以告訴我它是如何通過複製我的代碼?

+0

對不起,你的問題表明,你甚至沒有嘗試過任何事情,並要求我們做你的工作。你是否嘗試調用'int.Parse'或'ConvertToInt32'?顯示你嘗試了什麼 – Fabio

回答

0

你並不需要一個textBox1_TextChanged()事件

一個骯髒的方式可能是由以下

private async void button1_Click(object sender, EventArgs e) 
    { 
     int repeat = 5; 

     for (int i = 0; i <= repeat; i++) 
     {     
      await Connection.SendToServerAsync(2700, Int32.Parse(textBox1.Text); // <--|use the integer value to which textBox1 value can be cast to 
      await Connection.SendToServerAsync(3745); 
     } 
    } 

而更可靠的方法將檢查在去之前居然textBox1的值轉換成一整數的可能性上:

private async void button1_Click(object sender, EventArgs e) 
    { 
     int repeat = 5; 
     int amount; 

     if (Int32.TryParse(textBox1.Text, out amount)) // <--| go on only if textBox1 input value can be cast into an integer 
      for (int i = 0; i <= repeat; i++) 
      {     
       await Connection.SendToServerAsync(2700, amount); // <--| use the "amount" integer value read from textBox1 
       await Connection.SendToServerAsync(3745); 
      } 
    } 
+0

@不合時宜,你有沒有通過它? – user3598756

相關問題