现在的位置: 首页 > 综合 > 正文

VB.NET中的组件开发

2013年08月10日 ⁄ 综合 ⁄ 共 6769字 ⁄ 字号 评论关闭

先看段组件的代码:(临时写的,写得比较乱)


''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
                                                              '
'
             登录验证组件                                     '
'
                                                              '
'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Imports System.Security.Cryptography
Imports System.Text
Imports System.Data
Imports System.Data.SqlClient

Public Class Validator
    
Inherits System.ComponentModel.Component

    
Private username As String
    
Private userpwd As String

    
Public Property vUsername() As String
        
Get
            
Return username
        
End Get
        
Set(ByVal Value As String)
            username 
= Value
        
End Set
    
End Property


    
Public Property vUserpwd() As String
        
Get
            
Return userpwd
        
End Get
        
Set(ByVal Value As String)
            userpwd 
= Value
        
End Set
    
End Property


    
'转换为MD5
    Private Function convertMD5(ByVal pwd As StringAs String

        
Dim md5 As New MD5CryptoServiceProvider
        
Dim password As Byte() = (New ASCIIEncoding).GetBytes(pwd)

        
'转换为哈希值Byte数组
        Dim mdByte As Byte() = md5.ComputeHash(password)
        
'Dim mdString As String = System.BitConverter.ToString(mdByte)
        Dim mdString As String = (New ASCIIEncoding).GetString(mdByte)
        
Return mdString

    
End Function


    
Public Function validate() As Boolean

        
'连接到Users表

        
Dim myConnection As New SqlConnection("server=localhost;database=TEST;Trusted_Connection=yes;user id=sa;password=;")
        
Dim selectAdapter As New SqlDataAdapter("select * from Users where UserName='" + username + "'" + "and Password='" + convertMD5(userpwd) + "'", myConnection)
        
Dim ds As New DataSet
        
Try
            selectAdapter.Fill(ds, 
"Users")
            
If (ds.Tables(0).Rows.Count > 0Then
                
Return True
            
Else
                
Return False
            
End If
        
Catch ep As SqlException
            
MsgBox("连接数据库出错")
        
Catch pp As Exception
            
MsgBox("Oh,发生了不可预料的事情在你身边,你死定了。退出吧。")
        
End Try
    
End Function

#Region 
" 组件设计器生成的代码 "

    Public Sub New(ByVal Container As System.ComponentModel.IContainer)
        
MyClass.New()

        
'Windows.Forms 类撰写设计器支持所必需的
        Container.Add(Me)
    
End Sub


    
Public Sub New()
        
MyBase.New()

        
'该调用是组件设计器所必需的。
        InitializeComponent()

        
'在 InitializeComponent() 调用之后添加任何初始化

    
End Sub


    
'组件重写 dispose 以清理组件列表。
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        
If disposing Then
            
If Not (components Is NothingThen
                components.Dispose()
            
End If
        
End If
        
MyBase.Dispose(disposing)
    
End Sub


    
'组件设计器所必需的
    Private components As System.ComponentModel.IContainer

    
'注意: 以下过程是组件设计器所必需的
    '可以使用组件设计器修改此过程。
    '不要使用代码编辑器修改它。
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        components 
= New System.ComponentModel.Container
    
End Sub


#
End Region

End Class

 简介:组件其实是一段可以重用的代码,通过遵循IComponent接口的标准来实现一个组件,所以有组件都是派生于Component类,由Component类来实现IComponent接口。在组件中应正确使用函数的访问级别来控制外部对其的访问限制。

 只要有足够的权限就可以将组件放到自己的程序中而不用担心组件会产生多大的错误,因为组件已经经过测试的。比如说可以把一段登录的程序做成一个组件,或者把经常使用到的一些功能也做成组件,这样就可以减少开发中的错误,也可以缩短开发时间。组件之间也可以互相套用,如一个组件引用另一个组件,都是没问题,但要先在Add Reference中添加对组件的引用,在.NET中是通过把组件放在程序集中来实现的,程序集中存放着这些组件所依赖的文件信息和所在路径,因此CLR就可以通过这些信息来确定组件所需要的其他程序集的位置。

 ( 另外在组件设计过程中应好好利用接口来设计组件)

 在VS中创建组件:选建一个Project,再从模板中选Class Library,OK。接着再从Project菜单中Add Component,到些为止,组件的一个框架就呈现在眼前,平台自动继承了Component类和构造函数。可以删除原先创建类库时自动生成的Class1,看应用的需要。接着就可以在组件类里写要实现的功能,最后从Build(生成)菜单中选择Build Solution(生成解决方案)来生成组件。如果生成成功的话,到应用程序的BIN目录下会看到一个DLL文件。

  引用组件:只要在Solution Explorer窗口中,添加对DLL的Reference就可以了。

Imports loginValidator
Imports System.Data
Imports System.Data.SqlClient

Public Class loginForm
    
Inherits System.Windows.Forms.Form

#Region 
" Windows 窗体设计器生成的代码 "

    Public Sub New()
        
MyBase.New()

        
'该调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        
'在 InitializeComponent() 调用之后添加任何初始化

    
End Sub


    
'窗体重写 dispose 以清理组件列表。
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        
If disposing Then
            
If Not (components Is NothingThen
                components.Dispose()
            
End If
        
End If
        
MyBase.Dispose(disposing)
    
End Sub


    
'Windows 窗体设计器所必需的
    Private components As System.ComponentModel.IContainer

    
'注意: 以下过程是 Windows 窗体设计器所必需的
    '可以使用 Windows 窗体设计器修改此过程。
    '不要使用代码编辑器修改它。
    Friend WithEvents lblUserPwd As System.Windows.Forms.Label
    
Friend WithEvents lblUserName As System.Windows.Forms.Label
    
Friend WithEvents txtUserName As System.Windows.Forms.TextBox
    
Friend WithEvents txtUserPwd As System.Windows.Forms.TextBox
    
Friend WithEvents btnSubmit As System.Windows.Forms.Button
    
Friend WithEvents btnExit As System.Windows.Forms.Button
    
Friend WithEvents Label1 As System.Windows.Forms.Label
    
Friend WithEvents Label2 As System.Windows.Forms.Label
    
Friend WithEvents btnCancel As System.Windows.Forms.Button
    
Friend WithEvents Label3 As System.Windows.Forms.Label
    
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(loginForm))
        Me.lblUserPwd 
= New System.Windows.Forms.Label
        Me.lblUserName 
= New System.Windows.Forms.Label
        Me.txtUserName 
= New System.Windows.Forms.TextBox
        Me.txtUserPwd 
= New System.Windows.Forms.TextBox
        Me.btnSubmit 
= New System.Windows.Forms.Button
        Me.btnExit 
= New System.Windows.Forms.Button
        Me.Label1 
= New System.Windows.Forms.Label
        Me.Label2 
= New System.Windows.Forms.Label
        Me.btnCancel 
= New System.Windows.Forms.Button
        Me.Label3 
= New System.Windows.Forms.Label
        Me.SuspendLayout()
        
'
        'lblUserPwd
        '
        Me.lblUserPwd.Location = New System.Drawing.Point(46176)
        Me.lblUserPwd.Name 
= "lblUserPwd"
        Me.lblUserPwd.Size = New System.Drawing.Size(5223)
        Me.lblUserPwd.TabIndex 
= 0
        Me.lblUserPwd.Text 
= "密码:"
        Me.lblUserPwd.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        
'
        'lblUserName
        '
        Me.lblUserName.Location = New System.Drawing.Point(46128)
        Me.lblUserName.Name 
= "lblUserName"
        Me.lblUserName.Size = New System.Drawing.Size(5223)
        Me.lblUserName.TabIndex 
= 1
        Me.lblUserName.Text 
= "帐号:"
        Me.lblUserName.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        

抱歉!评论已关闭.