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

开发用户自定义控件

实例说明

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

图58-1 运行结果

技术要点

l 定制用户控件界面

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

l 编译用户控件

l 引用用户控件

<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

实现过程

■ 新建项目

打开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控件,此控件具有定时提示的作用。读者可以自行开发控件,最后将该控件发布即可。

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值