How to make a combo box read only ? N wat is the code to deactivate it ?

Dani AI

Generated

Two different scenarios are often confused as "read only" for combo boxes:

  • Windows Forms: If you want users to pick from your list but not type new text, use the non-editable list style rather than disabling the control. Disabling removes it from the tab order and harms accessibility. See ComboBox.DropDownStyle (Windows Forms) for details on the available modes and behavior.

  • ASP.NET Web Forms: The DropDownList control is already non-editable (users cannot type arbitrary text). If your goal is to display it as locked while keeping its value on postback, be careful: a disabled <select> does not submit its value. You can mirror the selection to a hidden field before disabling, then read from the hidden field on postback. The control API is documented at DropDownList Class (ASP.NET Web Forms), and the HTML rule about disabled controls not posting values is covered by MDN at Disabled form controls.

Example (VB.NET, Web Forms):

' Before rendering (e.g., Page_Load)
If Not IsPostBack Then
    HiddenField1.Value = MyDrop.SelectedValue
    MyDrop.Enabled = False  ' visually locked
End If

' Later, when you need the value
Dim currentValue As String =
    If(MyDrop.Enabled, MyDrop.SelectedValue, HiddenField1.Value)

Recommended Answers

All 7 Replies

ComboBox.Enabled = False ?

on combo box properties, set for DropDownStyle = DropDownList

commented: yes +1

ComboBox.Enabled = False ?

Fist load combobox with values wat you want and then
set combobox dropdown property to dropdownlist

combobox1.dropdownstyle=comboboxstyle.dropdownnlist

if you have noticed one can enter data into the combobox just like a textbox by typing directly into the interface. combobox.enable="tru/false"
just disables the user from entering data to the cmbox, what i think is the request is how to eensure that the user uses the provided options of which i dont know. however someone had suggested to me that changing DropDownStyle property to DropDownList migth do the trick!
:P

Thank you guys! I just browsed for the code with read only combo box, then you helped me! Unexpectedly. :-* That's why i joined your community just to say thanks and hopefully to make friends! :)

on combo box properties, set for dropdownstyle = dropdownlist

thank you :)

reneemacaraeg,

I'm glad you got it helpful. If you want to ask question, start your own thread.

Thread Closed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.