开发用户自定义控件  
发布时间: 2008-2-18 文章流量: 1723 次 文章来源: 本站
作品名称   开发用户自定义控件  
作品编号  
编程工具  
数 据 库  
论文字数   2000 字
运行环境  
论文价格   200 元
推荐指数   ☆☆☆☆☆
包含内容  
整理日期  
作品简介

 

实例说明

在本实例中将自行制作一个用户自定义控件并在另外的一个应用程序中加以引用。程序运行后,可以更改其中的内容,然后单击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
        Shared Sub Main()
            Application.Run(New HostApp())
        End Sub
    End Class   "HostApp
End Namespace 

■       运行程序

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

小结

我们在本实例中自制了一个AcitveX控件,此控件具有定时提示的作用。读者可以自行开发控件,最后将该控件发布即可。
下载地址  
购买说明 计算机毕业设计完整作品。
包括源程序、可执行文件、开题报告、论文、英文文献和中文翻译。
请加客户服务联系 15303601058 联系QQ:380894045/279018395 EMAIL:CareF@CareF.CN 获取作品的详细资料。
相关链接  
上一篇: 多彩文本
下一篇: 连接数据库(一)
信息回复  
版权所有 Copyright 2005-2008 悠索科技 Inc. All Rights Reserved
联系QQ: 380894045 279018395 EMAIL:CareF@CareF.CN
黑ICP备06003839号 黑ICP备08000316号