0

I have a solution with several forms, each may have TextBox's/controls and a button to show the SIP (the bottom bar is hidden).

When the user clicks my SIP button, the SIP is enabled but the focus is now the button. I want the user to click the button - the SIP to display but the focus to remain on the control that had the focus before the user clicked the button. Does anyone know how to do this? Thanks.

John Warlow
  • 2,922
  • 1
  • 34
  • 49
  • maybe do an $(input).click(function) and store the elements id as last_id. then do an onblur for the button and refocus on the last_id. that is if you have javascript/jquery available. – nathan hayfield Oct 30 '12 at 23:44

2 Answers2

1

Instead of using an standard button, you can create a custom one by deriving from the Control class and overriding the OnPaint method. A control created this way will not claim the focus by default when treating the Click event (tested on 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);
    }
}
yms
  • 10,361
  • 3
  • 38
  • 68
  • That only stops the button getting focus if you navigate by hitting tab. Clicking the button still gives focus to the button. – John Warlow Oct 31 '12 at 14:00
0

The solution of nathan will work also for Compact Framework or native Windows Mobile applications. In the textbox GotFocus set a global var and use this in the buttons click event to set the focus back to the last active textbox:

    //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;
    }

regards

Josef

Edit: Solution with subclass of 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+"'");
    }

Then, instead of placing TextBox use TextBoxIM.

josef
  • 5,951
  • 1
  • 13
  • 24
  • I'd though about that, but if there are other text box's, they'd need the got focus too, multipy that by other forms with other text box's and that's a lot of code to track. I was wondering if there was an easier way. I recall in C++ there was a pre translate message so I could monitor all messages for all controls - is there something similar in C# I could hook into? – John Warlow Oct 31 '12 at 09:27
  • I am not sure, how you would solve that with using PreTranslateMessage in a MFC app. - What about creating a subclass of TextBox with a static var "public static TextBox tb;" that is set in overriden OnGotFocus() of the subclassed TextBox. – josef Oct 31 '12 at 16:13