By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,493 Members | 1,114 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,493 IT Pros & Developers. It's quick & easy.

ComboBox lock/suppress DropDown

P: n/a

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;

Jul 12 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
ia**********@hotmail.com wrote:
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.con trols ?

Jul 12 '06 #2

P: n/a
Have you tried asking this question in
microsoft.public.dotnet.framework.windowsforms or
microsoft.public.dotnet.framework.windowsforms.con trols ?
Yes, I also posted this in
microsoft.public.dotnet.framework.windowsforms.con trols

Jul 12 '06 #3

P: n/a
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
Jul 13 '06 #4

P: n/a
Claes Bergefall wrote:
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;

Jul 13 '06 #5

P: n/a
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

<ia**********@hotmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Claes Bergefall wrote:
>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;

Jul 14 '06 #6

P: n/a
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;

Jul 19 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.