65.9K
CodeProject 正在变化。 阅读更多。
Home

只读 ComboBox 提供程序

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.22/5 (6投票s)

2008 年 5 月 22 日

CPOL

1分钟阅读

viewsIcon

36556

downloadIcon

295

如何创建一个只读 ComboBox 用户 IExtenderProvider。

引言

我见过很多使 ComboBox 只读的方法。我发现的最佳方法是为 ComboBox 创建一个 ExtenderProvider,在其中将 dropdownstyle 更改为 simple,然后捕获所有按键。

我发现将 ComboBox 设置为只读很有用,原因与我喜欢在 TextBox 上使用只读模式的原因相同。

主要是为了

  • 保持背景颜色为白色,而不是禁用的灰色
  • 允许我的用户从 ComboBox 中复制文本(禁用不允许复制)
  • 保持关联的工具提示启用(禁用 ComboBox 也会禁用关联的工具提示)

背景

这段代码基于 IExtenderProvider 接口。你可能需要 Google 搜索一下!文章还假定你已经了解一些关于捕获按键和使用哈希表的内容。

关注点

IExtenderProvider 可以用于各种情况,例如捕获 TextBox 控件中的按键。请关注我未来发布的文章,我将在其中展示该技术的示例。

Using the Code

在添加 ReadOnlyComboProvider 类后,你需要重新构建项目。然后,只需将其中一个从工具箱拖到你的表单上,并通过属性窗口或通过代码设置属性。

要通过代码设置只读属性,你可以这样做

Me.ReadOnlyComboProvider1.SetReadOnly(myComboBox, True)

这是 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

历史

  • 2008/5/22 - 发布第一个版本。
© . All rights reserved.