2016-11-01 48 views
1

我想編程文本框,所以他們在文本框離開後執行他們的指令。任何想法如何做到這一點?我試圖與水木清華KEYUP,但真的不知道它是如何工作C#文本編程

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

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

    private void button1_Click(object sender, EventArgs e) 
    { 

     var LoopResult = new List<int>(); 
     var LoopParameters = new List<int>(); 
     LoopParameters = TakeInput(); 
     LoopResult = Looping(LoopParameters); 
     Result(LoopResult); 
     FinalResult(LoopResult); 
    } 
    private List<int> TakeInput() 
    { 
     var Loop = new List<int>(); 
     Loop.Add(int.Parse(textBox1.Text)); 
     Loop.Add(int.Parse(textBox2.Text)); 
     Loop.Add(int.Parse(textBox3.Text)); 
     Loop.Add(int.Parse(textBox4.Text)); 
     Loop.Add(int.Parse(textBox5.Text)); 
     Loop.Add(int.Parse(textBox6.Text)); 
     return Loop; 
    }//Take the input 
    private List<int> Looping(List<int> LoopParam) 
    { 
     var LoopSum = new List<int>(); 
     LoopSum.Add(0); 
     LoopSum.Add(0); 
     for (int i = LoopParam[0]; i <= LoopParam[1]; i += LoopParam[2]) 
     { 
      LoopSum[0]+=i; 
     } 
     for(int i = LoopParam[3]; i <= LoopParam[4]; i += LoopParam[5]) 
     { 
      LoopSum[1]+=i; 
     } 
     return LoopSum; 
    }//Do the loop and summarize 
    void Result (List<int> LoopResult) 
    { 
     resultA.Text = ""+LoopResult[0]; 
     resultB.Text = ""+LoopResult[1]; 
    }//Give result 
    void FinalResult(List<int> LoopResult) 
    { 
     if(LoopResult[0]>LoopResult[1]) 
     { 
      WhoWins.Text = "Player A wins"; 
     } 
     else if(LoopResult[1]>LoopResult[0]) 
     { 
      WhoWins.Text = "Player B wins"; 
     } 
     else 
     { 
      WhoWins.Text = "It's a draw"; 
     } 
    }//Give final result 
    private void textBox1_TextChanged(object sender, EventArgs e) 
    { 
     int i = int.Parse(textBox1.Text); 
     if (i < 1 || i > 6) 
      label3.Text = "Wrong number"; 
    }//Texbox evaluation 

    private void textBox2_TextChanged(object sender, EventArgs e) 
    { 
     int i = int.Parse(textBox2.Text); 
     if (i < 7 || i > 18) 
      label4.Text = "Wrong number"; 
    } 

    private void textBox4_TextChanged(object sender, EventArgs e) 
    { 
     int i = int.Parse(textBox4.Text); 
     if (i < 1 || i > 6) 
      label7.Text = "Wrong number"; 
    }//Textbox evaluation 

    private void textBox5_TextChanged(object sender, EventArgs e) 
    { 
     int i = int.Parse(textBox5.Text); 
     if (i < 7 || i > 18) 
      label8.Text = "Wrong number"; 
    } 
} 
} 

The form itself

它告訴我,我的帖子主要是代碼,所以我需要添加更多的細節

+0

聽起來你正在尋找['LostFocus'](https://msdn.microsoft.com/en-us/library/system.windows.forms.control.lostfocus(v = vs。 110).aspx)事件。 –

回答

0

有引發LostFocus文本框事件可能對您有用在這種情況下:

private void textBox1_LostFocus(object sender, System.EventArgs e) 
{ 
    //Do something 
} 

而且不要忘了在你的設計師文件

註冊事件
textBox1.LostFocus += new EventHandler(textBox1_LostFocus); 
+0

我應該在哪裏註冊該活動? – alex3wielki

+0

在你的Form1.Designer.cs文件中的InitializeComponent()方法下this.textBox1聲明 – Roman

+0

那麼我需要4個不同的Initialize組件?原因我有4個文本框 – alex3wielki