我有幾種形式的解決方案,每個可能都有TextBox的/控件和一個按鈕來顯示SIP(底部欄隱藏)。防止按鈕獲得焦點
當用戶點擊我的SIP按鈕時,SIP已啓用,但焦點現在是按鈕。我希望用戶點擊按鈕 - 要顯示的SIP,但在用戶單擊該按鈕之前焦點仍然保留在具有焦點的控件上。有誰知道如何做到這一點?謝謝。
我有幾種形式的解決方案,每個可能都有TextBox的/控件和一個按鈕來顯示SIP(底部欄隱藏)。防止按鈕獲得焦點
當用戶點擊我的SIP按鈕時,SIP已啓用,但焦點現在是按鈕。我希望用戶點擊按鈕 - 要顯示的SIP,但在用戶單擊該按鈕之前焦點仍然保留在具有焦點的控件上。有誰知道如何做到這一點?謝謝。
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。
除了使用標準按鈕,還可以通過從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);
}
}
也許做$(輸入).click(函數)並將元素id存儲爲last_id。然後爲按鈕做一個onblur並重新調整last_id。那就是如果你有javascript/jquery可用的話。 –