Click here to Skip to main content
Click here to Skip to main content
Go to top

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.

Introduction

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)

Background

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 
    Else 
        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 
    Else 
        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) 
    Else 
        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 
    Else 
        e.Handled = False 
    End If 
End Sub 
 
End Class

History

  • 5/22/08 - First version posted.

License

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

Share

About the Author


Comments and Discussions

 
QuestionHi i am not able to add this readonly combo provider PinmemberPRATHAMESH CHAVAN25-Jan-14 6:38 
QuestionReadonly Combobox not quite read only? Pinmemberdjyule23-Jan-09 14:05 
GeneralUp/Down arrows PinmemberRoefes28-May-08 23:26 
GeneralRe: Up/Down arrows PinmemberAndrusM23-Jul-08 5:24 
General[Message Removed] PinmemberMojtaba Vali25-May-08 0:49 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 22 May 2008
Article Copyright 2008 by RanceDowner1234
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid