2012-10-30 102 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