locked
Why does focus work but not select in this case? RRS feed

  • Question

  • private void MainForm_Shown(object sender, EventArgs e)
    {
        toolFolderName.Focus(); // works but we're not supposed to use it
        toolFolderName.Select(); // doesn't work, control is not selected
    }
    

    The .NET docs say that control.focus() is a low-level method intended for custom control authors, and that we should ordinarily use control.select(). I have a form with a tool strip and a tool strip text box and some tool strip buttons, nothing fancy. I'd like the tool strip text box to have input focus when the form is launched. Note that I'm using the Shown event, which comes after Load and after the constructor. The form should be ready to go, yet only focus works.

    Obviously, I've solved the problem. The text box is selected when the form launches, but I'm still curious if anybody knows what's going on.

    Saturday, September 26, 2020 6:26 PM

Answers

  • Hi Robert in SF,

    Thank you for posting here.

    It seems that we cannot set 'ActiveControl' to 'toolStripTextBox'.

    As suggested in What's the difference between Control.Select() and Control.Focus()? : Select() is a higer-level method. It first looks iteratively upward in the control's parent hierarchy until it finds a container control. Then it sets that container's ActiveControl property to the called control.

    "Focus()" is different from "select()" in that it sets the focus in a lower level way.

    Hope it could be helpful.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Robert in SF Tuesday, September 29, 2020 12:45 AM
    Monday, September 28, 2020 7:41 AM