介绍 我已经见过许多
使组合框只读的创造性方法。我发现最好的方法是为组合框创建一个ExtenderProvider,将其中的dropdownstyle更改为simple,然后捕捉所有的击键。 我发现将一个combobox设置为readonly很有用,就像我喜欢在文本框上使用readonly一样。 即为了: 让我的用户从组合框中复制文本(禁用不允许复制)让关联的工具提示启用(禁用组合框也将禁用关联的工具提示) 背景 此代码基于IExtenderProvider接口。你可能会想要谷歌!本文还假设您已经了解捕获击键和哈希表的使用。 的兴趣点 IExtenderProvider可以在各种情况下使用,比如在文本框控件中捕获击键。在以后的文章中,我将展示这种技术的示例。 使用的代码 在添加ReadOnlyComboProvider类之后,需要重新构建项目。然后,只需将其中一个从工具箱拖到窗体上,并通过属性窗口或代码设置属性。 要通过代码设置readonly属性,你可以这样做: 隐藏,复制Code
Me.ReadOnlyComboProvider1.SetReadOnly(myComboBox, True)
下面是ExtenderProvider的代码: 隐藏,收缩,复制Code
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
历史 5/22/08 -第一版发布。 本文转载于:http://www.diyabc.com/frontweb/news234.html