使用VB.NET开发复合控件

简介: 使用VB.NET开发复合控件 界面: 控件类型   名称               文本  ListBox     lstSource ListBox     lstTargeg Button     btnAdd           Add > Button     btnAdd...

使用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")

相关文章
|
10月前
|
人工智能 芯片
D1net阅闻|OpenAI员工疯狂暗示,内部已成功开发ASI?被曝训出GPT-5但雪藏
D1net阅闻|OpenAI员工疯狂暗示,内部已成功开发ASI?被曝训出GPT-5但雪藏
|
8月前
|
SQL 小程序 API
如何运用C#.NET技术快速开发一套掌上医院系统?
本方案基于C#.NET技术快速构建掌上医院系统,结合模块化开发理念与医院信息化需求。核心功能涵盖用户端的预约挂号、在线问诊、报告查询等,以及管理端的排班管理和数据统计。采用.NET Core Web API与uni-app实现前后端分离,支持跨平台小程序开发。数据库选用SQL Server 2012,并通过读写分离与索引优化提升性能。部署方案包括Windows Server与负载均衡设计,确保高可用性。同时针对API差异、数据库老化及高并发等问题制定应对措施,保障系统稳定运行。推荐使用Postman、Redgate等工具辅助开发,提升效率与质量。
316 0
|
12月前
|
缓存 算法 安全
精选10款C#/.NET开发必备类库(含使用教程),工作效率提升利器!
精选10款C#/.NET开发必备类库(含使用教程),工作效率提升利器!
395 12
|
12月前
|
Linux API C#
基于 .NET 开发的多功能流媒体管理控制平台
基于 .NET 开发的多功能流媒体管理控制平台
208 9
|
12月前
|
Web App开发 前端开发 调度
一款基于 .NET + Blazor 开发的智能访客管理系统
一款基于 .NET + Blazor 开发的智能访客管理系统
177 8
|
12月前
|
前端开发 JavaScript C#
基于.NET8+Vue3开发的权限管理&个人博客系统
基于.NET8+Vue3开发的权限管理&个人博客系统
186 7
|
12月前
|
网络协议 C#
基于.NET WinForm开发的一款硬件及协议通讯工具
基于.NET WinForm开发的一款硬件及协议通讯工具
137 7
|
12月前
|
监控 前端开发 API
一款基于 .NET MVC 框架开发、功能全面的MES系统
一款基于 .NET MVC 框架开发、功能全面的MES系统
354 5
|
机器学习/深度学习 人工智能 物联网
.NET 技术:引领未来开发潮流
.NET 技术以其跨平台兼容性、高效的开发体验、强大的性能表现和安全可靠的架构,成为引领未来开发潮流的重要力量。本文深入探讨了 .NET 的核心优势与特点,及其在企业级应用、移动开发、云计算、人工智能等领域的广泛应用,展示了其卓越的应用价值和未来发展前景。
222 5
|
传感器 人工智能 供应链
.NET开发技术在数字化时代的创新作用,从高效的开发环境、强大的性能表现、丰富的库和框架资源等方面揭示了其关键优势。
本文深入探讨了.NET开发技术在数字化时代的创新作用,从高效的开发环境、强大的性能表现、丰富的库和框架资源等方面揭示了其关键优势。通过企业级应用、Web应用及移动应用的创新案例,展示了.NET在各领域的广泛应用和巨大潜力。展望未来,.NET将与新兴技术深度融合,拓展跨平台开发,推动云原生应用发展,持续创新。
167 4