zoukankan      html  css  js  c++  java
  • 只读组合框提供程序

    介绍 我已经见过许多

      

    使组合框只读的创造性方法。我发现最好的方法是为组合框创建一个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

  • 相关阅读:
    mybatis 动态sql
    linux shell 之 crontab(定时任务)详解
    FTP定时批量下载文件(SHELL脚本及使用方法 )
    腾讯云数据库团队:MySQL5.7 JSON实现简单介绍
    Chisel Tutorial(七)——模块
    大数问题解决模板
    可靠的功能測试--Espresso和Dagger2
    hdoj 1698 Just a Hook 【线段树 区间更新】
    平衡二叉树
    WPF中DependencyObject与DependencyProperty的源代码简单剖析
  • 原文地址:https://www.cnblogs.com/Dincat/p/13431226.html
Copyright © 2011-2022 走看看