开发者

Suppress Automatic Textbox Selection on Windows Forms Form.Show()

开发者 https://www.devze.com 2022-12-20 06:21 出处:网络
I have an application where I am trying to mimic the \"soft description\" textboxes like those found for the tags and title locations on this site.

I have an application where I am trying to mimic the "soft description" textboxes like those found for the tags and title locations on this site.

The way I've done this is essentially to create my textbox, and depending on what happens when the mouse pointer enters or leaves the control, it updates the content of the textbox to get the effect.

The problem is what when my form is first shown, the mouse cursor immediately jumps into the first textbox, which removes the title t开发者_如何转开发elling the user what the textbox is for.

If I turn off AcceptTab on the textbox, then everything works as expected, but the user loses the ability to tab into the textbox.

Is there a way to turn off this automatic selection of the textbox?


Could you this.Focus() on the form itself, or on some label control?


Bit late but a perfect solution is to select the form on load of form.

Adding this line to the constructor will give the expecting result.

this.Select();

But while using multi thread controls like OpenFileDialog if u want to unfocus/deselect text-box this.Select() was not working so I selected a button in the form using.

button1.Select();


The TabIndex property controls what order things will tab in, and on load, focus goes to the first control (ordered by TabIndex) that has AcceptTab as true. You can change the ordering so that the control that you want the user focus to start in is lowest (and have tabs work cycle through controls as you'd expect).

Alternatively, as Jason suggested, you could simply call Focus() on whatever control or the form itself in the FormLoad event.


I used a variant on Jason's technique. First, I created a dummy textbox with tabindex 0. That way, when the form is shown, that textbox will be selected. Next, I made the dummy textbox have zero width, so that it has no visible component.

However, once the form is loaded, I don't want the user to be able to tab over to the "nonexistant" textbox. Therefore, I added these two bits:

    //These functions prevent the textboxes from being implicitly selected.
    private void dummyBox_Leave(object sender, EventArgs e)
    {
        dummyBox.TabStop = false;
    }

    private void Main_Enter(object sender, EventArgs e)
    {
        dummyBox.TabStop = true;
        dummyBox.Select();
    }

Where Main is the name of my form.

Hope this helps someone.

Billy3

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号