2

On some occasions, when focus is set to a particular textbox, the cursor parks itself before the text like this:

enter image description here

Yet, the TextBox has a GotFocus handler that explicitly selects all the text:

private void txtQty_GotFocus(object sender, EventArgs e)
{
    try
    {
        if (deviceInfo.isKeyboardShown())
        {
            SipShowIM(SIPF_OFF);
        }
        txtQty.SelectAll();
        txtQty.BackColor = Color.Yellow;
    }
    catch (Exception ex)
    {
        NRBQ.ExceptionHandler(ex, "frmEntry.txtQty.GotFocus");
    }
}

Also, the BackColor is not set to yellow. But the txtQty_GotFocus handler is never unhooked. How could it be that it is not firing? Are there certain conditions under which focus can be programmatically set to a TextBox without the GotFocus event firing?

UPDATE

This was "solved" by adding another call to txtQty.Focus();

Why that was necessary, when txtQty already had focus (with a blinking cursor) I don't know. And why the textbox getting focus/cursor didn't SelectAll() as well as yellowify the background of the textbox still has me verblueffed.

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • 1
    Be careful with setting the focus (if you already are in a GotFocus event treatment). For your case, here is the same problem and the solution: http://stackoverflow.com/questions/97459/automatically-select-all-text-on-focus-in-winforms-textbox – Nicolas R Jul 17 '14 at 15:52

0 Answers0