2009-12-08 51 views
4

我正在寫一個C#程序,它接收一堆參數並對數據點進行一些轉換,然後將它們繪製到屏幕上。如何在所謂的事件中找到哪個控件引發了一個事件? Visual C#

在我的一個表格中,我有一堆TextBoxes,我都想執行相同的KeyPress事件。在我只做一個switch語句之前,只需將KeyPress事件發送給發件人並將其與所有文本框進行比較。這個方法對我來說似乎不是很有效,因爲現在我有20多個TextBoxes。

我想要做的是找到窗體上的哪個TextBox發送了KeyPress事件並從該TextBox獲取更多信息(IE的Text值等)。這可以節省我與發件人進行巨大切換的麻煩,以便查看它與TextBox相同的程度。但是我一直在最困難的時候這樣做。

我已經查看了System.Windows.Controls & System.Windows.Forms類來查看我是否能找到任何可以幫助我的東西。我正在尋找的東西是讓我看到哪個控制器有重點。也許這是我應該一直在尋找的?我也看了一下在KeyPress事件中我可以用發件人做些什麼,看看我能否找出TextBox引發事件,但仍然沒有運氣。

在這一點上,我覺得我更困惑自己。任何幫助將非常感激。

回答

9

發件人方式應該沒問題。你可以,但是,投:

TextBox txt = (TextBox) sender; // or "as" if you aren't sure. 
           // you can also just cast to Control for most 
           // common properties 
// perhaps look at txt.Name, txt.Text, or txt.Tag 

注意,有場景,其中發送者是不是你的想法,但是這是罕見的,主要涉及到的事件通過代碼來轉發,如:

public event EventHandler AcceptClick { 
    add { acceptButton.Click += value;} 
    remove { acceptButton.Click -= value;} 
} 

在這裏,sender將是acceptButton,你看不到。但通常這不是問題。

7

有一個約定,傳遞給事件處理函數的第一個參數是發送事件的控制,所以你可以施放它是這樣的:

void myTextBox_EventHandler(object sender, EventArgs args) 
{ 
    TextBox textBox = (TextBox)sender; 
    ... 
} 
1

您可以使用標籤控制的屬性區分多個文本框。 爲此,首先,您需要爲每個文本框分配標籤屬性,並在事件方法中,您可以訪問標籤屬性。假設你想分配多個參數,然後創建一個單獨的類,其參數爲屬性,並將該類的對象分配給文本框的標籤屬性。

希望,它有助於

1

我認爲馬克和GraemeF的答案是偉大的,但我要回答(反正)添加爲微可供選擇的方法的一個例子。這種方法反映了我的「懶散路徑」:)編程哲學。注意這個例子需要Linq。

我的第一個目標是讓一個TextBox類型的變量可用來保存當前活動的TextBox(或者如果需要從以外訪問表單,我可以通過公共屬性訪問它) :如果我能得到它,那麼我可以通過簡單的方式訪問當前活動的TextBox的當前Text:只需使用.Text屬性(不需要轉換)。

//on the Form where the TextBoxes are : make into a Public Property 
    // if you need to access this from outside the Form holding the TextBoxes 
    // a Form-level variable 
    private TextBox theActiveTB; 

然後,在表上,我會做窗體的Load事件建立文本框的集合,對我來說:並確保他們訂閱了相同的「按鍵響應和」輸入事件:

// on the Form where the TextBoxes are : a Form-level variable 
    private List<TextBox> tbList; 

    // build the list of TextBoxes and set the same 'Enter event for each of them 
    // note the assumption that every TextBox on the Form is going to be handled 
    // in exactly the same way : probably better to isolate them in a Panel in case 
    // you have other TextBoxes for other purposes ; then you'd just use the same 
    // code here to enumerate all the TextBoxes in the Panel 
    private void Form1_Load(object sender, EventArgs e) 
    { 
     tbList = 
     (
      from TextBox theTB in this.Controls.OfType<TextBox>() 
      select theTB 
     ).ToList(); 

     foreach (TextBox theTB in tbList) 
     { 
      theTB.KeyPress +=new KeyPressEventHandler(theTB_KeyPress); 
      theTB.Enter +=new EventHandler(theTB_Enter); 
     } 
    } 

    // so here's what the Enter event might look like : 
    private void theTB_Enter(object sender, EventArgs e) 
    { 
     theActiveTB = (TextBox)sender; 

     // reality check ... 
     // Console.WriteLine("entering : " + theActiveTB.Name); 
    } 

    // so here's what the KeyPress event might look like : 
    private void theTB_KeyPress(object sender, KeyPressEventArgs e) 
    { 
     // do what you need to do here 

     // access the Text in the Textbox via 'theActiveTB 

     // reality check 
     // Console.WriteLine(theActiveTB.Name + " : " + e.KeyChar); 
    } 

爲什麼還要製作List <TextBox>使用Linq?畢竟,你可以循環遍歷表單(或面板,或任何容器),並檢查每個控件的類型,然後,如果它是一個文本框,當場指派事件處理程序。我之所以選擇製作一個TextBoxes的通用列表,是因爲:當我有一個具有特殊共享標識,目的或行爲集合的集合時,通常情況下我會想要對其內容做其他事情作爲「組」或「組」。

+0

只是爲了「具體化」我在上面提到的在通用列表中收集類似元素/類/控件的優點的陳述:假設我有一個包含所有20個文本框的列表:我想禁用它們all:tbList.ForEach(tb => {tb.Enabled = false;}); ......好好照顧它。最好的,比爾 – BillW 2009-12-11 00:17:34

相關問題