Read Only Combobox

  • Thread starter Jason Simard via .NET 247
  • Start date
J

Jason Simard via .NET 247

Hi,

Does anyone know a way to make a combobox "read-only", similar to the read-only property of a textbox?

In essence, make a combobox readonly...making the combobox text not gray out but be disabled. There is no readonly property for a combobox like there is for a text box.

Thx,

Jason
 
N

norton

Hi

try set the dropdown style to "dropdownlist"

Hope it helps

Regards,
Norton

Jason Simard via .NET 247 said:
Hi,

Does anyone know a way to make a combobox "read-only", similar to the
read-only property of a textbox?
In essence, make a combobox readonly...making the combobox text not gray
out but be disabled. There is no readonly property for a combobox like
there is for a text box.
 
H

Herfried K. Wagner [MVP]

* Jason Simard via .NET 247 said:
Does anyone know a way to make a combobox "read-only", similar to the read-only property of a textbox?

Quick and dirty:

\\\
Private Declare Auto Function GetWindow Lib "user32.dll" ( _
ByVal hwnd As IntPtr, _
ByVal wCmd As Int32 _
) As IntPtr

Private Declare Auto Function SendMessage Lib "user32.dll" ( _
ByVal hwnd As IntPtr, _
ByVal wMsg As Int32, _
ByVal wParam As Boolean, _
ByVal lParam As Int32 _
) As Int32

Private Const EM_SETREADONLY As Int32 = &HCF

Private Const GW_CHILD As Int32 = 5

Private Function SetComboBoxReadOnly(ByVal ComboBox As ComboBox, ByVal Value As Boolean)
SendMessage( _
GetWindow( _
ComboBox.Handle, _
GW_CHILD _
), _
EM_SETREADONLY, _
Value, _
0 _
)
ComboBox.Refresh()
End Function

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
SetComboBoxReadOnly(Me.ComboBox1, Me.CheckBox1.Checked)
End Sub
///
 
C

Claes Bergefall

You'll also need to prevent the button from being pressed
Handle MouseDown event and don't call base if the coordinates
are within the button. Use CB_GETCOMBOBOXINFO to get its
position (that will also get you the handle to the edit control)
And override ProcessCmdMsg to prevent Alt+Down to drop
down the list

/claes

read-only property of a textbox?
Quick and dirty:

\\\
Private Declare Auto Function GetWindow Lib "user32.dll" ( _
ByVal hwnd As IntPtr, _
ByVal wCmd As Int32 _
) As IntPtr

Private Declare Auto Function SendMessage Lib "user32.dll" ( _
ByVal hwnd As IntPtr, _
ByVal wMsg As Int32, _
ByVal wParam As Boolean, _
ByVal lParam As Int32 _
) As Int32

Private Const EM_SETREADONLY As Int32 = &HCF

Private Const GW_CHILD As Int32 = 5

Private Function SetComboBoxReadOnly(ByVal ComboBox As ComboBox, ByVal Value As Boolean)
SendMessage( _
GetWindow( _
ComboBox.Handle, _
GW_CHILD _
), _
EM_SETREADONLY, _
Value, _
0 _
)
ComboBox.Refresh()
End Function

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles CheckBox1.CheckedChanged
 
H

Herfried K. Wagner [MVP]

* "Claes Bergefall said:
You'll also need to prevent the button from being pressed
Handle MouseDown event and don't call base if the coordinates
are within the button. Use CB_GETCOMBOBOXINFO to get its
position (that will also get you the handle to the edit control)
And override ProcessCmdMsg to prevent Alt+Down to drop
down the list

I don't think that this is a good idea. Yesterday I tried to implement
a readonly combobox. I didn't want to prevent the user from taking a
look at the dropdown part of the control. That worked fine by drawing
the items in an ownerdrawn mode that doesn't show the selection if the
control is readonly. Nevertheless, I was not able to prevent the user
from selecting an item. I subclassed the combobox's dropdown part and
played around with it (eating some messages etc.), and I tried to
disable the dropdown part, but this prevented the dropdown from closing
properly and/or the dropdown listbox's scrollbar to be disabled.

:-(
 
C

Claes Bergefall

Herfried K. Wagner said:
I don't think that this is a good idea. Yesterday I tried to implement
a readonly combobox. I didn't want to prevent the user from taking a
look at the dropdown part of the control. That worked fine by drawing
the items in an ownerdrawn mode that doesn't show the selection if the
control is readonly. Nevertheless, I was not able to prevent the user
from selecting an item. I subclassed the combobox's dropdown part and
played around with it (eating some messages etc.), and I tried to
disable the dropdown part, but this prevented the dropdown from closing
properly and/or the dropdown listbox's scrollbar to be disabled.

:-(

Well, if you still want to be able to see the drop-down list (but not select
anything) I guess it gets a bit trickier :). The OP should have enough
to get him started though

/claes
 
C

Cor Ligthert

Hi Herfried,

The answer is already given in the first answer in this thread, however when
you do not catch an raised event, there is no problem at all, what are you
searching for ?

I think that the OP only wants to make the text(box) part of the combobox
readonly and that answer is given by norton.

:)

Cor
 
H

Herfried K. Wagner [MVP]

* "Cor Ligthert said:
The answer is already given in the first answer in this thread, however when
you do not catch an raised event, there is no problem at all, what are you
searching for ?

I think that the OP only wants to make the text(box) part of the combobox
readonly and that answer is given by norton.

Mhm... Not sure what you are referring to. I wanted the user to be
able to dropdown the list but prevent him from selecting another item.
It's very simple to remove the item selection rectangle in the listbox
part of the combobox, but it's not as simple to make this part selection
insensitive.
 
H

Herfried K. Wagner [MVP]

* "Claes Bergefall said:
Well, if you still want to be able to see the drop-down list (but not select
anything) I guess it gets a bit trickier :).

Well, do you have an idea how to accomplish that?

;-)
The OP should have enough to get him started though

ACK.
 

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