vb.net

位置:IT落伍者 >> vb.net >> 浏览文章

VB.NET 中的组件开发源代码剖析


发布日期:2018年09月18日
 
VB.NET 中的组件开发源代码剖析

登录验证组件

Imports SystemSecurityCryptography

Imports SystemText

Imports SystemData

Imports SystemDataSqlClient

Public Class ValidatorClass Validator

Inherits SystemComponentModelComponent

Private username As String

Private userpwd As String

Public Property vUsername()Property vUsername() As String

Get

Return username

End Get

Set(ByVal Value As String)

username = Value

End Set

End Property

Public Property vUserpwd()Property vUserpwd() As String

Get

Return userpwd

End Get

Set(ByVal Value As String)

userpwd = Value

End Set

End Property

转换为MD5

Private Function convertMD()Function convertMD(ByVal pwd As String) As String

Dim md As New MDCryptoServiceProvider

Dim password As Byte() = (New ASCIIEncoding)GetBytes(pwd)

转换为哈希值Byte数组

Dim mdByte As Byte() = mdComputeHash(password)

Dim mdString As String = SystemBitConverterToString(mdByte)

Dim mdString As String = (New ASCIIEncoding)GetString(mdByte)

Return mdString

End Function

Public Function validate()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= + convertMD(userpwd) + myConnection)

Dim ds As New DataSet

Try

selectAdapterFill(ds Users)

If (dsTables()RowsCount > ) Then

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()Sub New(ByVal Container As SystemComponentModelIContainer)

MyClassNew()

WindowsForms 类撰写设计器支持所必需的

ContainerAdd(Me)

End Sub

Public Sub New()Sub New()

MyBaseNew()

该调用是组件设计器所必需的

InitializeComponent()

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

End Sub

组件重写 dispose 以清理组件列表

Protected Overloads Overrides Sub Dispose()Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

componentsDispose()

End If

End If

MyBaseDispose(disposing)

End Sub

组件设计器所必需的

Private components As SystemComponentModelIContainer

注意: 以下过程是组件设计器所必需的

可以使用组件设计器修改此过程

不要使用代码编辑器修改它

<SystemDiagnosticsDebuggerStepThrough()> Private Sub InitializeComponent()Sub InitializeComponent()

components = New SystemComponentModelContainer

End Sub

#End Region

End Class

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

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

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

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

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

Imports loginValidator

Imports SystemData

Imports SystemDataSqlClient

Public Class loginFormClass loginForm

Inherits SystemWindowsFormsForm

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

Public Sub New()Sub New()

MyBaseNew()

该调用是 Windows 窗体设计器所必需的

InitializeComponent()

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

End Sub

窗体重写 dispose 以清理组件列表

Protected Overloads Overrides Sub Dispose()Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

componentsDispose()

End If

End If

MyBaseDispose(disposing)

End Sub

Windows 窗体设计器所必需的

Private components As SystemComponentModelIContainer

注意: 以下过程是 Windows 窗体设计器所必需的

可以使用 Windows 窗体设计器修改此过程

不要使用代码编辑器修改它

Friend WithEvents lblUserPwd As SystemWindowsFormsLabel

Friend WithEvents lblUserName As SystemWindowsFormsLabel

Friend WithEvents txtUserName As SystemWindowsFormsTextBox

Friend WithEvents txtUserPwd As SystemWindowsFormsTextBox

Friend WithEvents btnSubmit As SystemWindowsFormsButton

Friend WithEvents btnExit As SystemWindowsFormsButton

Friend WithEvents Label As SystemWindowsFormsLabel

Friend WithEvents Label As SystemWindowsFormsLabel

Friend WithEvents btnCancel As SystemWindowsFormsButton

Friend WithEvents Label As SystemWindowsFormsLabel

<SystemDiagnosticsDebuggerStepThrough()> Private Sub InitializeComponent()Sub InitializeComponent()

Dim resources As SystemResourcesResourceManager = New SystemResourcesResourceManager(GetType(loginForm))

MelblUserPwd = New SystemWindowsFormsLabel

MelblUserName = New SystemWindowsFormsLabel

MetxtUserName = New SystemWindowsFormsTextBox

MetxtUserPwd = New SystemWindowsFormsTextBox

MebtnSubmit = New SystemWindowsFormsButton

MebtnExit = New SystemWindowsFormsButton

上一篇:VB.NET中层次数据绑定TreeView的实现

下一篇:一个vb.net的用户登陆页面代码