2009-03-02 69 views
1

我有一個工作的WinForm,它處理我的客戶數據庫中的搜索功能。它包含以下控件:根據活動控件更改AcceptButton

  • 你在那裏輸入您的搜索字符串(A)
  • 「搜索」 按鈕(B)
  • 一個DataGridView(C),返回搜索
  • 結果的文本框
  • 「確定」 按鈕(d)和 「取消」 按鈕(E)

我試圖旁邊accieve是這樣的:

  1. 當A是活性的的AcceptButton(ENTER)應該連接到b
  2. 當按下B或輸入C應成爲活性
  3. 當C是活性的的AcceptButton應該鏈接到d

我意識到這是一個有點大的問題,所以如果你知道答案,不要猶豫,只回答一個子彈標記。

編輯:我已經解決了上述要求1和3的實施,但我仍然在尋找第二個答案。爲了說明,當搜索開始時(意思是我按下了鍵盤上的輸入或鼠標的搜索按鈕),我希望焦點轉移到DataGridView的第一行。

回答

3

當你的文本改變事件的textBox設置的AcceptButton爲「搜索」:

private void textBox1_TextChanged(object sender, EventArgs e) 
    { 
     this.AcceptButton = SearchButton; 
    } 

有可能會想更多的代碼在這裏,檢查字符串的長度等。

然後,一旦你完成搜索並填寫DataGridView,你就可以設置AcceptButton爲「OK」。

private void dataGridView1_DataMemberChanged(object sender, EventArgs e) 
    { 
     this.AcceptButton = OKButton; 
    } 

雖然你可能不想使用這個事件。

「取消」始終是CancelButton。

編輯:確定爲第2部分你想下面的代碼:

private void SearchButton_Click(object sender, EventArgs e) 
    { 
     dataGridView1.Focus(); 
    } 

這將使它儘快主動作爲搜索按鈕被按下,但你可能想這樣做時,在DataGridView已被填充 - 以防萬一搜索沒有結果。

+1

所以我想這意味着另一個dataGridView_DataMemberChanged第2部分? – Sakkle 2009-03-03 12:27:18

1

設置處理程序以跟蹤特定控件的Control.Enter事件。當輸入達到的嘗試:

this.AcceptButton = ControlThatShouldBeAcceptButton; 
1
private void WhateverControl_Enter(...) 
{ 
    this.AcceptButton = myButton; 
} 
1

您應該爲DataGridViewBindingCompleteEventHandler實現一個事件偵聽器。在這種情況下,你可以添加下面的代碼:

{ 
    if(grid.Rows.Count > 0) 
    { 
     grid.ClearSelection(); 
     grid.Rows[0].Selected = true; 
    } 
    grid.Focus(); 
}