How could a TextBox's cursor move to the beginning of the text after calling SelectAll()?

强颜欢笑 提交于 2020-01-04 09:10:22

问题


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

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.

来源:https://stackoverflow.com/questions/24790704/how-could-a-textboxs-cursor-move-to-the-beginning-of-the-text-after-calling-sel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!