[VB.NET]开发用户自定义控件

前端之家收集整理的这篇文章主要介绍了[VB.NET]开发用户自定义控件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
开发用户自定义控件

实例说明

在本实例中将自行制作一个用户自定义控件并在另外的一个应用程序中加以引用。程序运行后,可以更改其中的内容,然后单击save即可。程序运行结果如图58-1所示。

图58-1 运行结果

技术要点

l 定制用户控件界面

l 为用户控件添加属性和过程

l 编译用户控件

l 引用用户控件

实现过程

■ 新建项目

打开Visual Studio.NET,选择"新建项目",在项目类型窗口中选择"Visual Basic项目",在模板窗口中选择"Windows控件库",在名称域中输入"CustomControl",然后选择保存路径。单击"确认"。

添加控件

向当前用户控件UserControl1上添加五个Label控件和五个TexbBox控件。

■ 设置属性

用户控件UserControl1上的控件设置属性,如表58-1所示。

表58-1 各控件的属性

窗体/控件 属性

UserControl BackColor &H00C00000

Text5 Text

ScrollBars 2-Vertical

MultiLine True

Label1 Text ID

Text1 Text

其余Label、TextBox控件 Text 跟界面一致

添加组件类

通过菜单"项目|添加新项",在弹出的对话框中选择"组件类",在名称栏中输入Customer,单击"确定"。

添加代码

在组件类的代码窗口下输入下列代码

Imports System

Imports System.Windows.Forms

Imports System.Drawing

Namespace Microsoft.Samples.WinForms.VB.CustomerControl

Public Class CustomerControl

Inherits System.Windows.Forms.UserControl

Private customer1 As Customer

Public Sub New()

MyBase.New()

' required by the Windows Forms Designer

InitializeComponent()

' TODO: Add any constructor code after InitializeComponent call

End Sub

Public Property Customer() As Customer

Get

Return customer1

End Get

Set(ByVal Value As Customer)

customer1 = Value

LoadCustomer()

End Set

End Property

Public Sub AcceptChanges()

customer1.Title = textBoxTitle.Text

customer1.FirstName = textBoxFirstName.Text

customer1.LastName = textBoxLastName.Text

customer1.Address = textBoxAddress.Text

End Sub

Public Sub RejectChanges()

LoadCustomer()

End Sub

Private Sub LoadCustomer()

textBoxID.Text = customer1.ID

textBoxTitle.Text = customer1.Title

textBoxFirstName.Text = customer1.FirstName

textBoxLastName.Text = customer1.LastName

textBoxAddress.Text = customer1.Address

End Sub

Public Overloads Overrides Sub Dispose()

MyBase.Dispose()

End Sub

End Class

End Namespace

■ 测试用户控件

右键单击"解决方案",选择"添加|新建项目",为解决方添加一个测试用户控件的项目。并在窗体上添加一个CustomerControl控件和两个Button控件。

添加测试代码

'这些必不可少

Imports System

Imports System.ComponentModel

Imports System.Drawing

Imports System.Windows.Forms

Imports Microsoft.Samples.WinForms.VB.CustomerControl

Imports Microsoft.VisualBasic.ControlChars

Namespace Microsoft.Samples.WinForms.VB.HostApp

Public Class HostApp

Inherits System.Windows.Forms.Form

Public Sub New()

MyBase.New()

HostApp = Me

'This call is required by the Windows Forms Designer.

InitializeComponent()

'TODO: Add any initialization after the InitializeComponent() call

CustomerControl1.Customer = Customer.ReadCustomer()

' Set the minimum form size to the client size + the height of the title bar

Me.MinimumSize = New Size(400,(373 + SystemInformation.TextHeight))

End Sub

'Form overrides dispose to clean up the component list.

Public Overloads Overrides Sub Dispose()

MyBase.Dispose()

components.Dispose()

End Sub

Private Sub buttonCancel_Click(ByVal sender As Object,ByVal e As System.EventArgs) Handles buttonCancel.Click

CustomerControl1.RejectChanges()

End Sub

Private Sub buttonSave_Click(ByVal sender As Object,ByVal e As System.EventArgs) Handles buttonSave.Click

CustomerControl1.AcceptChanges()

MessageBox.Show("Customer Changes Saved: " & CrLf & CustomerControl1.Customer.ToString)

End Sub

<STAThread()> Shared Sub Main()

Application.Run(New HostApp())

End Sub

End Class 'HostApp

End Namespace

■ 运行程序

单击菜单"调试|启动"或单击 图标运行程序。

小结

我们在本实例中自制了一个AcitveX控件,此控件具有定时提示的作用。读者可以自行开发控件,最后将该控件发布即可。

原文链接:https://www.f2er.com/vb/264330.html

猜你在找的VB相关文章