2012-10-30 112 views
0

我有幾種形式的解決方案,每個可能都有TextBox的/控件和一個按鈕來顯示SIP(底部欄隱藏)。防止按鈕獲得焦點

當用戶點擊我的SIP按鈕時,SIP已啓用,但焦點現在是按鈕。我希望用戶點擊按鈕 - 要顯示的SIP,但在用戶單擊該按鈕之前焦點仍然保留在具有焦點的控件上。有誰知道如何做到這一點?謝謝。

+0

也許做$(輸入).click(函數)並將元素id存儲爲last_id。然後爲按鈕做一個onblur並重新調整last_id。那就是如果你有javascript/jquery可用的話。 –

回答

0

nathan的解決方案也適用於Compact Framework或原生Windows Mobile應用程序。在文本框中的GotFocus設置一個全局變量和使用中的按鈕單擊事件將焦點設置到最後一個活動的文本框:

//global var 
    TextBox currentTB = null; 
    private void button1_Click(object sender, EventArgs e) 
    { 
     inputPanel1.Enabled = !inputPanel1.Enabled; 
     if(currentTB!=null) 
      currentTB.Focus(); 
    } 

    private void textBox1_GotFocus(object sender, EventArgs e) 
    { 
     currentTB = (TextBox)sender; 
    } 

問候

約瑟夫

編輯:解的子類TextBox:

class TextBoxIM: TextBox{ 
    public static TextBox tb; 
    protected override void OnGotFocus (EventArgs e) 
    { 
     tb=this; 
     base.OnGotFocus (e); 
    } 
} 
... 
private void btnOK_Click (object sender, System.EventArgs e) 
{  
    string sName=""; 
    foreach(Control c in this.Controls){ 
     if (c.GetType()==typeof(TextBoxIM)){ 
      sName=c.Name; 
      break; //we only need one instance to get the value 
     } 
    } 
    MessageBox.Show("Last textbox='"+sName+"'"); 
    } 

然後,而不是將TextBox使用TextBoxIM。

+0

我想盡管如此,但如果有其他文本框,他們也需要獲得焦點,其他形式與其他文本框相乘,這是很多代碼來跟蹤。我想知道是否有更簡單的方法。我記得在C++中有一個預翻譯的消息,所以我可以監控所有控件的所有消息 - 有什麼類似的C#我可以掛鉤? – JLWarlow

+0

我不確定,如何在MFC應用程序中使用PreTranslateMessage來解決這個問題。 - 如何創建一個TextBox的子類與靜態變量「公共靜態文本框tb;」這是在subclassed TextBox的覆蓋OnGotFocus()中設置的。 – josef

1

除了使用標準按鈕,還可以通過從Control類派生並重寫OnPaint方法來創建自定義按鈕。在處理Click事件(在VS2008 netcf 2.0上測試)時,以此方式創建的控件在默認情況下不會聲明焦點。

public partial class MyCustomButton : Control 
{ 
    public MyCustomButton() 
    { 
     InitializeComponent(); 
    } 

    protected override void OnPaint(PaintEventArgs pe) 
    { 
     pe.Graphics.DrawString("Show SIP", Font, new SolidBrush(ForeColor), 0, 0); 
     // Calling the base class OnPaint 
     base.OnPaint(pe); 
    } 
} 
+0

只有通過點擊標籤進行導航才能停止獲取焦點。單擊該按鈕仍然會將焦點對準按鈕。 – JLWarlow

+0

@JLWarlow看我的編輯。 – yms