Jamesbraders
Jamesbraders

Reputation: 143

Winforms Checkbox combobox single click instead of double click

I am using the Checkbox combobox control found at: http://www.codeproject.com/KB/combobox/extending_combobox.aspx,

and have a problem where the checkbox combobox requires two clicks to select an item at first, but once the first click has occurred, it only requires one. I need the box to only require one click regardless.

Has anybody else had this problem and managed to solve it? The same question was asked of another user on the code project site listed above, but with no answer.

Thanks

James

Upvotes: 2

Views: 3513

Answers (2)

Steve Stokes
Steve Stokes

Reputation: 1230

The above solution is correct to fix the first issue, where it required two clicks to enter the list of checkboxes, however, this introduces a new issue when you click the control to exit it, it retains focus and you must double click to go to another control. I was able to fix this with the following code:

In CheckBoxComboBox.cs add the following override:

    protected override void OnClick(EventArgs e)
    {
        base.OnClick(e);
        this.Parent.Focus();
    }

With the answer from Rob P. and this answer, it will not hold focus on either click event.

Upvotes: 3

Rob P.
Rob P.

Reputation: 15071

It sounds like a focus issue (the first click is activating the control and the second click is checking the box).

Did you try the solution below?

/// <summary>
/// Processes Windows messages.
/// </summary>
/// <param name="m">The Windows <see cref="T:System.Windows.Forms.Message" /> to process.</param>
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
protected override void WndProc(ref Message m) {
    if (m.Msg == (NativeMethods.WM_COMMAND + NativeMethods.WM_REFLECT) && NativeMethods.HIWORD(m.WParam) == NativeMethods.CBN_DROPDOWN) {
        // Wout: changed this to use BeginInvoke instead of calling ShowDropDown directly.
        // When calling directly, the Control doesn't receive focus.
        BeginInvoke(new MethodInvoker(ShowDropDown));
        return;
    }
    base.WndProc(ref m);
}

I can't take credit for the code - but the comment seems to match up with my gut feeling - it wasn't getting focus.

Upvotes: 2

Related Questions