-
使用VB.NET开发复合控件
使用VB.NET开发复合控件
界面:
控件类型 名称 文本
ListBox lstSource
ListBox lstTargeg
Button btnAdd Add >
Button btnAddAll Add All >>
Button btnRemove < Remove
Button btnClear << Clear
代码如下:
1 Public Class SelectCombo 2 Inherits System.Windows.Forms.UserControl 3 4 ' Make the width of the area for the buttons 100 twips 5 Dim mnButtonAreaWidth As Integer = 100 6 7 ' Set minimum height and width for the control 8 Dim mnMinControlWidth As Integer = 200 9 Dim mnMinControlHeight As Integer = 200 10 11 12 #Region " Windows Form Designer generated code " 13 14 Public Sub New() 15 MyBase.New() 16 17 'This call is required by the Windows Form Designer. 18 InitializeComponent() 19 20 'Add any initialization after the InitializeComponent() call 21 22 End Sub 23 24 'UserControl1 overrides dispose to clean up the component list. 25 Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) 26 If disposing Then 27 If Not (components Is Nothing) Then 28 components.Dispose() 29 End If 30 End If 31 MyBase.Dispose(disposing) 32 End Sub 33 34 'Required by the Windows Form Designer 35 Private components As System.ComponentModel.IContainer 36 37 'NOTE: The following procedure is required by the Windows Form Designer 38 'It can be modified using the Windows Form Designer. 39 'Do not modify it using the code editor. 40 Friend WithEvents lstSource As System.Windows.Forms.ListBox 41 Friend WithEvents btnAdd As System.Windows.Forms.Button 42 Friend WithEvents btnAddAll As System.Windows.Forms.Button 43 Friend WithEvents lstTarget As System.Windows.Forms.ListBox 44 Friend WithEvents btnRemove As System.Windows.Forms.Button 45 Friend WithEvents btnClear As System.Windows.Forms.Button 46 <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() 47 Me.lstSource = New System.Windows.Forms.ListBox() 48 Me.btnAdd = New System.Windows.Forms.Button() 49 Me.btnAddAll = New System.Windows.Forms.Button() 50 Me.btnRemove = New System.Windows.Forms.Button() 51 Me.btnClear = New System.Windows.Forms.Button() 52 Me.lstTarget = New System.Windows.Forms.ListBox() 53 Me.SuspendLayout() 54 ' 55 'lstSource 56 ' 57 Me.lstSource.Dock = System.Windows.Forms.DockStyle.Left 58 Me.lstSource.ItemHeight = 12 59 Me.lstSource.Location = New System.Drawing.Point(0, 0) 60 Me.lstSource.Name = "lstSource" 61 Me.lstSource.Size = New System.Drawing.Size(120, 136) 62 Me.lstSource.TabIndex = 0 63 ' 64 'btnAdd 65 ' 66 Me.btnAdd.Location = New System.Drawing.Point(136, 8) 67 Me.btnAdd.Name = "btnAdd" 68 Me.btnAdd.Size = New System.Drawing.Size(80, 24) 69 Me.btnAdd.TabIndex = 1 70 Me.btnAdd.Text = "Add >" 71 ' 72 'btnAddAll 73 ' 74 Me.btnAddAll.Location = New System.Drawing.Point(136, 40) 75 Me.btnAddAll.Name = "btnAddAll" 76 Me.btnAddAll.Size = New System.Drawing.Size(80, 24) 77 Me.btnAddAll.TabIndex = 2 78 Me.btnAddAll.Text = "Add All >>" 79 ' 80 'btnRemove 81 ' 82 Me.btnRemove.Location = New System.Drawing.Point(136, 72) 83 Me.btnRemove.Name = "btnRemove" 84 Me.btnRemove.Size = New System.Drawing.Size(80, 24) 85 Me.btnRemove.TabIndex = 3 86 Me.btnRemove.Text = "< Remove" 87 ' 88 'btnClear 89 ' 90 Me.btnClear.Location = New System.Drawing.Point(136, 104) 91 Me.btnClear.Name = "btnClear" 92 Me.btnClear.Size = New System.Drawing.Size(80, 24) 93 Me.btnClear.TabIndex = 4 94 Me.btnClear.Text = "<< Clear" 95 ' 96 'lstTarget 97 ' 98 Me.lstTarget.Dock = System.Windows.Forms.DockStyle.Right 99 Me.lstTarget.ItemHeight = 12 100 Me.lstTarget.Location = New System.Drawing.Point(232, 0) 101 Me.lstTarget.Name = "lstTarget" 102 Me.lstTarget.Size = New System.Drawing.Size(120, 136) 103 Me.lstTarget.TabIndex = 5 104 ' 105 'SelectCombo 106 ' 107 Me.Controls.Add(Me.lstTarget) 108 Me.Controls.Add(Me.btnClear) 109 Me.Controls.Add(Me.btnRemove) 110 Me.Controls.Add(Me.btnAddAll) 111 Me.Controls.Add(Me.btnAdd) 112 Me.Controls.Add(Me.lstSource) 113 Me.Name = "SelectCombo" 114 Me.Size = New System.Drawing.Size(352, 136) 115 Me.ResumeLayout(False) 116 117 End Sub 118 119 #End Region 120 121 Private Sub SelectCombo_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize 122 ' Check for minimum width and height. 123 ' Throw exception if new width or height too small 124 Dim sError As String 125 sError = "Attempted to make SelectCombo user control too small." 126 127 If MyBase.Size.Width < mnMinControlWidth Then 128 Dim eComboException As New ApplicationException(sError) 129 eComboException.Source = Me.ToString 130 End If 131 If MyBase.Size.Height < mnMinControlHeight Then 132 Dim eComboException As New ApplicationException(sError) 133 eComboException.Source = Me.ToString 134 End If 135 136 'Set source and target list boxes to appropriate width. Note that 137 'docking the list boxes makes their height the right size automatically. 138 Dim nListboxWidth As Integer 139 nListboxWidth = CInt(0.5 * (Me.Size.Width - mnButtonAreaWidth)) 140 lstSource.Size = New Size(nListboxWidth, lstSource.Size.Height) 141 lstTarget.Size = New Size(nListboxWidth, lstSource.Size.Height) 142 143 'Now position the buttons between the list boxes. 144 Dim nLeftButtonPosition As Integer 145 nLeftButtonPosition = nListboxWidth + _ 146 ((mnButtonAreaWidth - btnAdd.Size.Width) \ 2) 147 btnAdd.Location = New Point(nLeftButtonPosition, btnAdd.Location.Y) 148 btnAddAll.Location = New Point(nLeftButtonPosition, btnAddAll.Location.Y) 149 btnRemove.Location = New Point(nLeftButtonPosition, btnRemove.Location.Y) 150 btnClear.Location = New Point(nLeftButtonPosition, btnClear.Location.Y) 151 End Sub 152 153 Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click 154 Dim objItem As Object 155 For Each objItem In lstSource.SelectedItems 156 lstTarget.Items.Add(objItem) 157 Next objItem 158 159 End Sub 160 161 Private Sub btnAddAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddAll.Click 162 Dim objItem As Object 163 For Each objItem In lstSource.Items 164 lstTarget.Items.Add(objItem) 165 Next objItem 166 167 End Sub 168 169 Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click 170 lstTarget.Items.Clear() 171 End Sub 172 173 Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click 174 ' Have to go through the collection in reverse 175 ' because we are removing items. 176 Dim nIndex As Integer 177 For nIndex = lstTarget.SelectedItems.Count - 1 To 0 Step -1 178 lstTarget.Items.Remove(lstTarget.SelectedItems(nIndex)) 179 Next nIndex 180 181 End Sub 182 183 Public ReadOnly Property SelectedItem(ByVal iIndex As Integer) As Object 184 Get 185 Return lstTarget.Items(iIndex) 186 End Get 187 End Property 188 189 Public ReadOnly Property SelectedCount() As Integer 190 Get 191 Return lstTarget.Items.Count 192 End Get 193 End Property 194 195 Public ReadOnly Property AvailableCount() As Integer 196 Get 197 Return lstSource.Items.Count 198 End Get 199 End Property 200 201 Public Sub Add(ByVal objItem As Object) 202 lstSource.Items.Add(objItem) 203 End Sub 204 205 Public ReadOnly Property AvailableItem(ByVal iIndex As Integer) As Object 206 Get 207 Return lstSource.Items(iIndex) 208 End Get 209 End Property 210 211 Public Sub Clear() 212 lstSource.Items.Clear() 213 lstTarget.Items.Clear() 214 215 End Sub 216 217 End Class
测试中,将控件拖入WINDOW窗体,在Form_Load事件中写入
SelectCombo1.Add("1")
SelectCombo1.Add("2")
SelectCombo1.Add("3")
SelectCombo1.Add("4")
SelectCombo1.Add("5")
出处:https://www.cnblogs.com/djcsch2001/archive/2012/12/30/2839851.html
最新更新
python爬虫及其可视化
使用python爬取豆瓣电影短评评论内容
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比