Click here to Skip to main content
15,881,882 members
Articles / Desktop Programming / Windows Forms
Article

Read Only ComboBox Provider

Rate me:
Please Sign up or sign in to vote.
4.22/5 (6 votes)
22 May 2008CPOL1 min read 35.8K   280   20   5
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:

VB
Me.ReadOnlyComboProvider1.SetReadOnly(myComboBox, True)

Here's the code for the ExtenderProvider:

VB
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)



Comments and Discussions

 
QuestionHi i am not able to add this readonly combo provider Pin
PRATHAMESH CHAVAN25-Jan-14 6:38
PRATHAMESH CHAVAN25-Jan-14 6:38 
QuestionReadonly Combobox not quite read only? Pin
djyule23-Jan-09 14:05
djyule23-Jan-09 14:05 
GeneralUp/Down arrows Pin
Roefes28-May-08 23:26
Roefes28-May-08 23:26 
GeneralRe: Up/Down arrows Pin
AndrusM23-Jul-08 5:24
AndrusM23-Jul-08 5:24 
General[Message Removed] Pin
Mojtaba Vali25-May-08 0:49
Mojtaba 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.