使用VB.NET开发复合控件
2012-12-30 17:01
633 查看
使用VB.NET开发复合控件
界面:
控件类型 名称 文本
ListBox lstSource
ListBox lstTargeg
Button btnAdd Add >
Button btnAddAll Add All >>
Button btnRemove < Remove
Button btnClear << Clear
代码如下:
测试中,将控件拖入WINDOW窗体,在Form_Load事件中写入
SelectCombo1.Add("1")
SelectCombo1.Add("2")
SelectCombo1.Add("3")
SelectCombo1.Add("4")
SelectCombo1.Add("5")
界面:
控件类型 名称 文本
ListBox lstSource
ListBox lstTargeg
Button btnAdd Add >
Button btnAddAll Add All >>
Button btnRemove < Remove
Button btnClear << Clear
代码如下:
Public Class SelectCombo Inherits System.Windows.Forms.UserControl ' Make the width of the area for the buttons 100 twips Dim mnButtonAreaWidth As Integer = 100 ' Set minimum height and width for the control Dim mnMinControlWidth As Integer = 200 Dim mnMinControlHeight As Integer = 200 #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'UserControl1 overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents lstSource As System.Windows.Forms.ListBox Friend WithEvents btnAdd As System.Windows.Forms.Button Friend WithEvents btnAddAll As System.Windows.Forms.Button Friend WithEvents lstTarget As System.Windows.Forms.ListBox Friend WithEvents btnRemove As System.Windows.Forms.Button Friend WithEvents btnClear As System.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.lstSource = New System.Windows.Forms.ListBox() Me.btnAdd = New System.Windows.Forms.Button() Me.btnAddAll = New System.Windows.Forms.Button() Me.btnRemove = New System.Windows.Forms.Button() Me.btnClear = New System.Windows.Forms.Button() Me.lstTarget = New System.Windows.Forms.ListBox() Me.SuspendLayout() ' 'lstSource ' Me.lstSource.Dock = System.Windows.Forms.DockStyle.Left Me.lstSource.ItemHeight = 12 Me.lstSource.Location = New System.Drawing.Point(0, 0) Me.lstSource.Name = "lstSource" Me.lstSource.Size = New System.Drawing.Size(120, 136) Me.lstSource.TabIndex = 0 ' 'btnAdd ' Me.btnAdd.Location = New System.Drawing.Point(136, 8) Me.btnAdd.Name = "btnAdd" Me.btnAdd.Size = New System.Drawing.Size(80, 24) Me.btnAdd.TabIndex = 1 Me.btnAdd.Text = "Add >" ' 'btnAddAll ' Me.btnAddAll.Location = New System.Drawing.Point(136, 40) Me.btnAddAll.Name = "btnAddAll" Me.btnAddAll.Size = New System.Drawing.Size(80, 24) Me.btnAddAll.TabIndex = 2 Me.btnAddAll.Text = "Add All >>" ' 'btnRemove ' Me.btnRemove.Location = New System.Drawing.Point(136, 72) Me.btnRemove.Name = "btnRemove" Me.btnRemove.Size = New System.Drawing.Size(80, 24) Me.btnRemove.TabIndex = 3 Me.btnRemove.Text = "< Remove" ' 'btnClear ' Me.btnClear.Location = New System.Drawing.Point(136, 104) Me.btnClear.Name = "btnClear" Me.btnClear.Size = New System.Drawing.Size(80, 24) Me.btnClear.TabIndex = 4 Me.btnClear.Text = "<< Clear" ' 'lstTarget ' Me.lstTarget.Dock = System.Windows.Forms.DockStyle.Right Me.lstTarget.ItemHeight = 12 Me.lstTarget.Location = New System.Drawing.Point(232, 0) Me.lstTarget.Name = "lstTarget" Me.lstTarget.Size = New System.Drawing.Size(120, 136) Me.lstTarget.TabIndex = 5 ' 'SelectCombo ' Me.Controls.Add(Me.lstTarget) Me.Controls.Add(Me.btnClear) Me.Controls.Add(Me.btnRemove) Me.Controls.Add(Me.btnAddAll) Me.Controls.Add(Me.btnAdd) Me.Controls.Add(Me.lstSource) Me.Name = "SelectCombo" Me.Size = New System.Drawing.Size(352, 136) Me.ResumeLayout(False) End Sub #End Region Private Sub SelectCombo_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize ' Check for minimum width and height. ' Throw exception if new width or height too small Dim sError As String sError = "Attempted to make SelectCombo user control too small." If MyBase.Size.Width < mnMinControlWidth Then Dim eComboException As New ApplicationException(sError) eComboException.Source = Me.ToString End If If MyBase.Size.Height < mnMinControlHeight Then Dim eComboException As New ApplicationException(sError) eComboException.Source = Me.ToString End If 'Set source and target list boxes to appropriate width. Note that 'docking the list boxes makes their height the right size automatically. Dim nListboxWidth As Integer nListboxWidth = CInt(0.5 * (Me.Size.Width - mnButtonAreaWidth)) lstSource.Size = New Size(nListboxWidth, lstSource.Size.Height) lstTarget.Size = New Size(nListboxWidth, lstSource.Size.Height) 'Now position the buttons between the list boxes. Dim nLeftButtonPosition As Integer nLeftButtonPosition = nListboxWidth + _ ((mnButtonAreaWidth - btnAdd.Size.Width) \ 2) btnAdd.Location = New Point(nLeftButtonPosition, btnAdd.Location.Y) btnAddAll.Location = New Point(nLeftButtonPosition, btnAddAll.Location.Y) btnRemove.Location = New Point(nLeftButtonPosition, btnRemove.Location.Y) btnClear.Location = New Point(nLeftButtonPosition, btnClear.Location.Y) End Sub Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click Dim objItem As Object For Each objItem In lstSource.SelectedItems lstTarget.Items.Add(objItem) Next objItem End Sub Private Sub btnAddAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddAll.Click Dim objItem As Object For Each objItem In lstSource.Items lstTarget.Items.Add(objItem) Next objItem End Sub Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click lstTarget.Items.Clear() End Sub Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click ' Have to go through the collection in reverse ' because we are removing items. Dim nIndex As Integer For nIndex = lstTarget.SelectedItems.Count - 1 To 0 Step -1 lstTarget.Items.Remove(lstTarget.SelectedItems(nIndex)) Next nIndex End Sub Public ReadOnly Property SelectedItem(ByVal iIndex As Integer) As Object Get Return lstTarget.Items(iIndex) End Get End Property Public ReadOnly Property SelectedCount() As Integer Get Return lstTarget.Items.Count End Get End Property Public ReadOnly Property AvailableCount() As Integer Get Return lstSource.Items.Count End Get End Property Public Sub Add(ByVal objItem As Object) lstSource.Items.Add(objItem) End Sub Public ReadOnly Property AvailableItem(ByVal iIndex As Integer) As Object Get Return lstSource.Items(iIndex) End Get End Property Public Sub Clear() lstSource.Items.Clear() lstTarget.Items.Clear() End Sub End Class
测试中,将控件拖入WINDOW窗体,在Form_Load事件中写入
SelectCombo1.Add("1")
SelectCombo1.Add("2")
SelectCombo1.Add("3")
SelectCombo1.Add("4")
SelectCombo1.Add("5")
相关文章推荐
- VisualStudio移动开发(C#、VB.NET)Smobiler开发平台——SliderView控件的使用方式
- VS移动开发(C#、VB.NET)Smobiler开发平台——SliderView控件的使用方式 4000
- VisualStudio移动开发(C#、VB.NET)Smobiler开发平台——AlbumView相册控件的使用方式
- 使用VB.NET开发定制控件
- VisualStudio移动开发(C#、VB.NET)Smobiler开发平台——GifView控件的使用方式
- 4000 移动开发(C#、VB.NET)Smobiler开发平台——GifView控件的使用方式
- VisualStudio移动开发(C#、VB.NET)Smobiler开发平台——BarcodeView控件的使用方式,.Net移动开发
- VisualStudio移动开发(C#、VB.NET)Smobiler开发平台——VoiceRecorder控件的使用方式.Net移动开发
- VisualStudio移动开发(C#、VB.NET)Smobiler开发平台——ImageTabBar控件的使用方式
- VisualStudio移动开发(C#、VB.NET)Smobiler开发平台——VoiceRecorder控件的使用方式.Net移动开发
- asp.net控件开发基础(4) ----------明白使用RenderContent方法呈现自定义控件
- swift开发笔记16 - 使用xib自定义一个控件(复合控件)
- ASP.NET 控件开发速成教程:生成复合控件
- Asp.net 2.0 自定义控件开发专题[详细探讨页面状态(视图状态和控件状态)机制及其使用场景](示例代码下载)
- Winform下的地图开发控件(GMap.NET)使用心得
- GMap.NET地图开发控件使用心得
- vb.net WebBrowser控件使用
- [VB.NET]如何在第二个窗体里使用第一个窗体的控件方法
- Asp.net 2.0 自定义控件开发专题[详细探讨页面状态(视图状态和控件状态)机制及其使用场景](示例代码下载)