Labyrinth
Labyrinth

Reputation: 31

Is it possible to focus a control in a form that is unfocusable via WS_CHILD style?

I've created a drop-down control similar to a ComboBox, where on the click of a button a 'dropdown' form will appear below the button and the user will be able to select an option from a ListBox. The dropdown form itself is unfocusable, so the parent form never loses focus. It all works perfectly as intended, but my problem is that I would like to be use any type of control in this dropdown form. Any control that requires focus to function (e.g. a TextBox) won't work because it will be unable to gain focus since the dropdown form is unfocusable.

I am using this code here for the dropdown form:

protected override CreateParams CreateParams
{
    get
    {
        CreateParams ret = base.CreateParams;
        ret.Style = (int)Flags.WindowStyles.WS_CHILD;
        ret.ExStyle |= (int)Flags.WindowStyles.WS_EX_NOACTIVATE | (int)Flags.WindowStyles.WS_EX_TOOLWINDOW;
        ret.X = this.Location.X;
        ret.Y = this.Location.Y;
        return ret;
    }
}

Which I sourced from here: https://www.codeproject.com/Articles/71808/Creating-a-Form-That-Doesn-t-Take-Focus

I tried manually setting a dropdown control's parent to the root form, but this causes it not to be drawn.

I'd like to know if there's some kind of solution to allow selection of these controls contained in the dropdown despite the form itself being unselectable. Perhaps a windows message that I can process to circumvent focus logic?

Upvotes: 0

Views: 97

Answers (1)

Labyrinth
Labyrinth

Reputation: 31

I ended up not using the CreateParams override, and instead simply used protected override bool ShowWithoutActivation => true; with a borderless form.

Seems to work well enough. Some of the benefits of not focusing controls such as selection borders and highlights had to be addressed case by case, but otherwise the looks the same with added functionality.

Upvotes: 1

Related Questions