Click here to Skip to main content
13,043,154 members (55,558 online)
Click here to Skip to main content
Add your own
alternative version


20 bookmarked
Posted 22 May 2008

Read Only ComboBox Provider

, 22 May 2008
Rate this:
Please Sign up or sign in to vote.
How to create a read only combobox user IExtenderProvider.


I've seen a lot of creative ways to make a combo box read only. The best way I have found to do it is is to create an ExtenderProvider for the combo box inside which you change the dropdownstyle to simple and then trap all the keystrokes.

I've found it useful to set a combobox to readonly for the same reasons I like to use readonly on a textbox.

Namely in order to:

  • Keep the background color white instead of disabled gray
  • Allow my user to copy text out of the combo box (disabling doesn't allow a copy)
  • Keep an associated tooltip enabled (disabling a combo box will also disable the associated tooltip)


This code is based on the IExtenderProvider interface. You may want to Google it! The article also assumes you already know something about trapping keystrokes and the use of hashtables.

Points of Interest

The IExtenderProvider can be used in all sorts of situations, like, trapping keystrokes in a textbox control. Look for future articles where I'll show an example of that technique.

Using the Code

You'll need to rebuild the project after you add the ReadOnlyComboProvider class. Then, simply drag one of these from the toolbox onto your form and set the properties via a property window or via code.

To set the readonly property via code, you would do something like:

Me.ReadOnlyComboProvider1.SetReadOnly(myComboBox, True)

Here's the code for the ExtenderProvider:

Option Explicit On 
Option Strict On 
Option Compare Text 
Imports System.ComponentModel 
Imports System.Windows.Forms 
<Drawing.ToolboxBitmap(GetType(System.Windows.Forms.TextBox)), _ 
System.ComponentModel.DesignerCategoryAttribute("Code"), _ 
ProvideProperty("ReadOnly", GetType(Control)), _ 
ProvideProperty("OrigDropDownStyle", GetType(Control))> _ 
Public Class ReadOnlyComboProvider 

Inherits System.ComponentModel.Component 

Implements IExtenderProvider 
''This hash table stores all the controls extended by this extender provider 
Friend htProvidedProperties As New Hashtable 
Public Function CanExtend(ByVal extendee As Object) As Boolean _
       Implements System.ComponentModel.IExtenderProvider.CanExtend 
    If TypeOf extendee Is ComboBox Then 
        Return True 
        Return False 
    End If 
End Function 
Private Class ComboBoxProperties 
    Public IsReadOnly As Boolean = False 
    Public OrigComboBoxStyle As ComboBoxStyle = ComboBoxStyle.DropDown 
End Class 
<Category("Read Only Combobox Provider")> _ 
Sub SetReadOnly(ByVal ctrl As Control, ByVal value As Boolean) 
    Dim cbo As ComboBox = CType(ctrl, ComboBox) 
    If value = True Then 
        cbo.DropDownStyle = ComboBoxStyle.Simple 
        cbo.DropDownStyle = GetControlFromHashtable(ctrl).OrigComboBoxStyle 
    End If 
    GetControlFromHashtable(ctrl).IsReadOnly = value 
End Sub 
<Category("Read Only Combobox Provider")> _ 
Function GetReadOnly(ByVal ctrl As Control) As Boolean 
    Return GetControlFromHashtable(ctrl).IsReadOnly 
End Function 
<Category("Read Only Combobox Provider")> _ 
Sub SetOrigDropDownStyle(ByVal ctrl As Control, ByVal value As ComboBoxStyle) 
    GetControlFromHashtable(ctrl).OrigComboBoxStyle = value 
End Sub 
<Category("Read Only Combobox Provider")> _ 
Function GetOrigDropDownStyle(ByVal ctrl As Control) As ComboBoxStyle 
    Return GetControlFromHashtable(ctrl).OrigComboBoxStyle 
End Function 
Private Function GetControlFromHashtable(ByVal ctrl As Control) As ComboBoxProperties 
    If htProvidedProperties.Contains(ctrl) Then 
        Return DirectCast(htProvidedProperties(ctrl), ComboBoxProperties) 
        Dim ProvidedProperties As New ComboBoxProperties

        ''Add A KeyPress Event Handler as the control is added to hash table 
        AddHandler ctrl.KeyPress, AddressOf KeyPressHandler 
        htProvidedProperties.Add(ctrl, ProvidedProperties) 
        Return ProvidedProperties 
    End If 
End Function 
Private Sub KeyPressHandler(ByVal sender As Object, ByVal e As KeyPressEventArgs) 
    Dim cboSender As ComboBox = CType(sender, ComboBox) 
    If GetControlFromHashtable(cboSender).IsReadOnly = True Then 
        e.Handled = True 
        e.Handled = False 
    End If 
End Sub 
End Class


  • 5/22/08 - First version posted.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

You may also be interested in...

Comments and Discussions

QuestionHi i am not able to add this readonly combo provider Pin
memberPRATHAMESH CHAVAN25-Jan-14 6:38 
QuestionReadonly Combobox not quite read only? Pin
djyule23-Jan-09 14:05
memberdjyule23-Jan-09 14:05 
GeneralUp/Down arrows Pin
Roefes28-May-08 23:26
memberRoefes28-May-08 23:26 
GeneralRe: Up/Down arrows Pin
AndrusM23-Jul-08 5:24
memberAndrusM23-Jul-08 5:24 
General[Message Removed] Pin
Mojtaba Vali25-May-08 0:49
memberMojtaba Vali25-May-08 0:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170713.1 | Last Updated 22 May 2008
Article Copyright 2008 by RanceDowner1234
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid