ComboBox lock/suppress DropDown

I

ian_jacobsen

I have a control that is derived from the standard ComboBox. I want to
lock or suppress the DropDown when the arrow button is clicked without
disabling the control.

This is related to another post that I have out there for a custom
OnPaint event. I want to provide custom colors for ComboBox that work
even when the Enabled is set to false (by default it is set to Gray).
I have provided a new implementation for the Enabled property to handle
it myself. When the control is in my disabled state I am supressing
KeyDowns so that it appears as if it is disabled. The only problem is
that you can still drop down the list, and choose an item. If I can
lock this, I'm good to go.

Oh, and I'd rather not have to use SendMessage in WndProc to accomplish
this. I don't want to have a dependency on user32.dll.

--Ian;
 
B

Bruce Wood

I have a control that is derived from the standard ComboBox. I want to
lock or suppress the DropDown when the arrow button is clicked without
disabling the control.

This is related to another post that I have out there for a custom
OnPaint event. I want to provide custom colors for ComboBox that work
even when the Enabled is set to false (by default it is set to Gray).
I have provided a new implementation for the Enabled property to handle
it myself. When the control is in my disabled state I am supressing
KeyDowns so that it appears as if it is disabled. The only problem is
that you can still drop down the list, and choose an item. If I can
lock this, I'm good to go.

Oh, and I'd rather not have to use SendMessage in WndProc to accomplish
this. I don't want to have a dependency on user32.dll.

Have you tried asking this question in
microsoft.public.dotnet.framework.windowsforms or
microsoft.public.dotnet.framework.windowsforms.controls ?
 
I

ian_jacobsen

Have you tried asking this question in
microsoft.public.dotnet.framework.windowsforms or
microsoft.public.dotnet.framework.windowsforms.controls ?

Yes, I also posted this in
microsoft.public.dotnet.framework.windowsforms.controls
 
C

Claes Bergefall

I have a control that is derived from the standard ComboBox. I want to
lock or suppress the DropDown when the arrow button is clicked without
disabling the control.

This is related to another post that I have out there for a custom
OnPaint event. I want to provide custom colors for ComboBox that work
even when the Enabled is set to false (by default it is set to Gray).
I have provided a new implementation for the Enabled property to handle
it myself. When the control is in my disabled state I am supressing
KeyDowns so that it appears as if it is disabled. The only problem is
that you can still drop down the list, and choose an item. If I can
lock this, I'm good to go.

Put another control (override OnPaint and call ControlPaint.DrawComboButton)
ontop of it. You can get the size and position of the combo button by
P/Invoking GetComboBoxInfo (or you can calculate it with a little use of
SystemInformation.CaptionButtonSize)

Oh, and I'd rather not have to use SendMessage in WndProc to accomplish
this. I don't want to have a dependency on user32.dll.

Why not? It's not like user32.dll won't be there.


/claes
 
I

ian_jacobsen

Claes said:
Put another control (override OnPaint and call ControlPaint.DrawComboButton)
ontop of it.

This is simply a paint routine, not an new control (I didn't know about
ControlPaint, so that's a cool find, thanks). However I did try adding
a real button control (new Button()) and tried to place it on top of
the combobox button. I can't get it to show up, and all the clicks end
up going to the combo box so the list is still dropped down.

-- Ian;
 
C

Claes Bergefall

I meant that you should create a new control and override its OnPaint. It
needs to be in order to trap the mouse clicks.

Inherit Control like this (make it an internal class in your ComboBox
class):
internal class ReadOnlyButton : Control
{
public ReadOnlyButton()
{
this.SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.DoubleBuffer | ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.Selectable, false);
}

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
ControlPaint.DrawScrollButton(e.Graphics, this.ClientRectangle,
ScrollButton.Down, ButtonState.Inactive);
}
}

In the constructor of your ComboBox class:
....
m_readOnlyButton = new ReadOnlyButton();
m_readOnlyButton.Visible = false;
this.Controls.Add(m_readOnlyButton);
....

When you want your combo to be read only:
....
m_readOnlyButton.Visible = true;
m_readOnlyButton.BringToFront();
....

To position it (you might need to experiment a bit here, or use
GetComboBoxInfo):
....
Size size;
size.Height = this.Height - 4
size.Width = SystemInformation.CaptionButtonSize.Width - 2
m_readOnlyButton.Size = size
m_readOnlyButton.Location = new Point(this.Width -
SystemInformation.CaptionButtonSize.Width - 2, 0);
....

/claes
 
I

ian_jacobsen

Awesome, thanks Claes.

I ended up using an override of WndProc and providing my own custom
draw method on the WM_PAINT message in the derived ComboBox class. The
ComboBox has a really messed up OnPaint event, that doesn't allow for
painting your own custom text box area.

--Ian;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top