VB.NET中的组件开发
VB.NET中的组件开发<br/>''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''<br/>' '<br/>' 登录验证组件 '<br/>' '<br/>''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''<br/>Imports System.Security.Cryptography<br/>Imports System.Text<br/>Imports System.Data<br/>Imports System.Data.SqlClient<br/>Public Class ValidatorClass Validator<br/> Inherits System.ComponentModel.Component<br/> Private username As String<br/> Private userpwd As String<br/> Public Property vUsername()Property vUsername() As String<br/> Get<br/> Return username<br/> End Get<br/> Set(ByVal Value As String)<br/> username = Value<br/> End Set<br/> End Property<br/> Public Property vUserpwd()Property vUserpwd() As String<br/> Get<br/> Return userpwd<br/> End Get<br/> Set(ByVal Value As String)<br/> userpwd = Value<br/> End Set<br/> End Property<br/> '转换为MD5<br/> Private Function convertMD5()Function convertMD5(ByVal pwd As String) As String<br/> Dim md5 As New MD5CryptoServiceProvider<br/> Dim password As Byte() = (New ASCIIEnco*).GetBytes(pwd)<br/> '转换为哈希值Byte数组<br/> Dim mdByte As Byte() = md5.ComputeHash(password)<br/> 'Dim mdString As String = System.BitConverter.ToString(mdByte)<br/> Dim mdString As String = (New ASCIIEnco*).GetString(mdByte)<br/> Return mdString<br/> End Function<br/> Public Function validate()Function validate() As Boolean<br/> '连接到Users表<br/> Dim myConnection As New SqlConnection("server=localhost;database=TEST;Trusted_Connection=yes;user id=sa;password=;")<br/> Dim selectAdapter As New SqlDataAdapter("select * from Users where UserName='" + username + "'" + "and Password='" + convertMD5(userpwd) + "'", myConnection)<br/> Dim ds As New DataSet<br/> Try<br/> selectAdapter.Fill(ds, "Users")<br/> If (ds.Tables(0).Rows.Count > 0) Then<br/> Return True<br/> Else<br/> Return False<br/> End If<br/> Catch ep As SqlException<br/> MsgBox("连接数据库出错")<br/> Catch pp As Exception<br/> MsgBox("Oh,发生了不可预料的事情在你身边,你死定了。退出吧。")<br/> End Try<br/> End Function<br/>#Region " 组件设计器生成的代码 "<br/> Public Sub New()Sub New(ByVal Container As System.ComponentModel.IContainer)<br/> MyClass.New()<br/> 'Windows.Forms 类撰写设计器支持所必需的<br/> Container.Add(Me)<br/> End Sub<br/> Public Sub New()Sub New()<br/> MyBase.New()<br/> '该调用是组件设计器所必需的。<br/> InitializeComponent()<br/> '在 InitializeComponent() 调用之后添加任何初始化<br/> End Sub<br/> '组件重写 dispose 以清理组件列表。<br/> Protected Overloads Overrides Sub Dispose()Sub Dispose(ByVal disposing As Boolean)<br/> If disposing Then<br/> If Not (components Is Nothing) Then<br/> components.Dispose()<br/> End If<br/> End If<br/> MyBase.Dispose(disposing)<br/> End Sub<br/> '组件设计器所必需的<br/> Private components As System.ComponentModel.IContainer<br/> '注意: 以下过程是组件设计器所必需的<br/> '可以使用组件设计器修改此过程。<br/> '不要使用代码编辑器修改它。<br/> <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()Sub InitializeComponent()<br/> components = New System.ComponentModel.Container<br/> End Sub<br/>#End Region<br/>End Class <br/>简介:组件其实是一段可以重用的代码,通过遵循IComponent接口的标准来实现一个组件,所以有组件都是派生于Component类,由Component类来实现IComponent接口。在组件中应正确使用函数的访问级别来控制外部对其的访问限制。<br/>只要有足够的权限就可以将组件放到自己的程序中而不用担心组件会产生多大的错误,因为组件已经经过测试的。比如说可以把一段登录的程序做成一个组件,或者把经常使用到的一些功能也做成组件,这样就可以减少开发中的错误,也可以缩短开发时间。组件之间也可以互相套用,如一个组件引用另一个组件,都是没问题,但要先在Add Reference中添加对组件的引用,在.NET中是通过把组件放在程序集中来实现的,程序集中存放着这些组件所依赖的文件信息和所在路径,因此CLR就可以通过这些信息来确定组件所需要的其他程序集的位置。<br/>( 另外在组件设计过程中应好好利用接口来设计组件)<br/>在VS中创建组件:选建一个Project,再从模板中选Class Library,OK。接着再从Project菜单中Add Component,到些为止,组件的一个框架就呈现在眼前,平台自动继承了Component类和构造函数。可以删除原先创建类库时自动生成的Class1,看应用的需要。接着就可以在组件类里写要实现的功能,最后从Build(生成)菜单中选择Build Solution(生成解决方案)来生成组件。如果生成成功的话,到应用程序的BIN目录下会看到一个DLL文件。<br/> 引用组件:只要在Solution Explorer窗口中,添加对DLL的Reference就可以了。<br/>Imports loginValidator<br/>Imports System.Data<br/>Imports System.Data.SqlClient<br/>Public Class loginFormClass loginForm<br/> Inherits System.Windows.Forms.Form<br/>#Region " Windows 窗体设计器生成的代码 "<br/> Public Sub New()Sub New()<br/> MyBase.New()<br/> '该调用是 Windows 窗体设计器所必需的。<br/> InitializeComponent()<br/> '在 InitializeComponent() 调用之后添加任何初始化<br/> End Sub<br/> '窗体重写 dispose 以清理组件列表。<br/> Protected Overloads Overrides Sub Dispose()Sub Dispose(ByVal disposing As Boolean)<br/> If disposing Then<br/> If Not (components Is Nothing) Then<br/> components.Dispose()<br/> End If<br/> End If<br/> MyBase.Dispose(disposing)<br/> End Sub<br/> 'Windows 窗体设计器所必需的<br/> Private components As System.ComponentModel.IContainer<br/> '注意: 以下过程是 Windows 窗体设计器所必需的<br/> '可以使用 Windows 窗体设计器修改此过程。<br/> '不要使用代码编辑器修改它。<br/> Friend WithEvents lblUserPwd As System.Windows.Forms.Label<br/> Friend WithEvents lblUserName As System.Windows.Forms.Label<br/> Friend WithEvents txtUserName As System.Windows.Forms.TextBox<br/> Friend WithEvents txtUserPwd As System.Windows.Forms.TextBox<br/> Friend WithEvents btnSubmit As System.Windows.Forms.Button<br/> Friend WithEvents btnExit As System.Windows.Forms.Button<br/> Friend WithEvents Label1 As System.Windows.Forms.Label<br/> Friend WithEvents Label2 As System.Windows.Forms.Label<br/> Friend WithEvents btnCancel As System.Windows.Forms.Button<br/> Friend WithEvents Label3 As System.Windows.Forms.Label<br/> <System.Diagnostics.DebuggerStepThrough()> <p></p><p> Private Sub InitializeComponent()Sub InitializeComponent()<br/> Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(loginForm))<br/> Me.lblUserPwd = New System.Windows.Forms.Label<br/> Me.lblUserName = New System.Windows.Forms.Label<br/> Me.txtUserName = New System.Windows.Forms.TextBox<br/> Me.txtUserPwd = New System.Windows.Forms.TextBox<br/> Me.btnSubmit = New System.Windows.Forms.Button<br/> Me.btnExit = New System.Windows.Forms.Button<br/> Me.Label1 = New System.Windows.Forms.Label<br/> Me.Label2 = New System.Windows.Forms.Label<br/> Me.btnCancel = New System.Windows.Forms.Button<br/> Me.Label3 = New System.Windows.Forms.Label<br/> Me.SuspendLayout()<br/> '<br/> 'lblUserPwd<br/> '<br/> Me.lblUserPwd.Location = New System.Drawing.Point(46, 176)<br/> Me.lblUserPwd.Name = "lblUserPwd"<br/> Me.lblUserPwd.Size = New System.Drawing.Size(52, 23)<br/> Me.lblUserPwd.TabIndex = 0<br/> Me.lblUserPwd.Text = "密码:"<br/> Me.lblUserPwd.TextAlign = System.Drawing.ContentAlignment.MiddleCenter<br/> '<br/> 'lblUserName<br/> '<br/> Me.lblUserName.Location = New System.Drawing.Point(46, 128)<br/> Me.lblUserName.Name = "lblUserName"<br/> Me.lblUserName.Size = New System.Drawing.Size(52, 23)<br/> Me.lblUserName.TabIndex = 1<br/> Me.lblUserName.Text = "帐号:"<br/> Me.lblUserName.TextAlign = System.Drawing.ContentAlignment.MiddleCenter<br/> '<br/> 'txtUserName<br/> '<br/> Me.txtUserName.BackColor = System.Drawing.SystemColors.Info<br/> Me.txtUserName.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle<br/> Me.txtUserName.Location = New System.Drawing.Point(110, 128)<br/> Me.txtUserName.Name = "txtUserName"<br/> Me.txtUserName.Size = New System.Drawing.Size(151, 21)<br/> Me.txtUserName.TabIndex = 3<br/> Me.txtUserName.Text = ""<br/> '<br/> 'txtUserPwd<br/> '<br/> Me.txtUserPwd.BackColor = System.Drawing.SystemColors.Info<br/> Me.txtUserPwd.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle<br/> Me.txtUserPwd.Location = New System.Drawing.Point(110, 176)<br/> Me.txtUserPwd.Name = "txtUserPwd"<br/> Me.txtUserPwd.PasswordChar = Microsoft.VisualBasic.ChrW(42)<br/> Me.txtUserPwd.Size = New System.Drawing.Size(151, 21)<br/> Me.txtUserPwd.TabIndex = 4<br/> Me.txtUserPwd.Text = ""<br/> '<br/> 'btnSubmit<br/> '<br/> Me.btnSubmit.BackColor = System.Drawing.SystemColors.ActiveBorder<br/> Me.btnSubmit.Cursor = System.Windows.Forms.Cursors.Hand<br/> Me.btnSubmit.ForeColor = System.Drawing.SystemColors.InfoText<br/> Me.btnSubmit.Location = New System.Drawing.Point(56, 216)<br/> Me.btnSubmit.Name = "btnSubmit"<br/> Me.btnSubmit.TabIndex = 5<br/> Me.btnSubmit.Text = "登录"<br/> '<br/> 'btnExit<br/> '<br/> Me.btnExit.BackColor = System.Drawing.SystemColors.ActiveBorder<br/> Me.btnExit.Cursor = System.Windows.Forms.Cursors.Hand<br/> Me.btnExit.Location = New System.Drawing.Point(141, 216)<br/> Me.btnExit.Name = "btnExit"<br/> Me.btnExit.TabIndex = 6<br/> Me.btnExit.Text = "退出"<br/> '<br/> 'Label1<br/> '<br/> Me.Label1.Font = New System.Drawing.Font("Impact", 17.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(134, Byte))<br/> Me.Label1.Location = New System.Drawing.Point(38, 64)<br/> Me.Label1.Name = "Label1"<br/> Me.Label1.Size = New System.Drawing.Size(256, 32)<br/> Me.Label1.TabIndex = 6<br/> Me.Label1.Text = "TEST"<br/> '<br/> 'Label2<br/> '<br/> Me.Label2.BackColor = System.Drawing.SystemColors.ControlText<br/> Me.Label2.ForeColor = System.Drawing.SystemColors.HighlightText<br/> Me.Label2.Location = New System.Drawing.Point(0, 0)<br/> Me.Label2.Name = "Label2"<br/> Me.Label2.Size = New System.Drawing.Size(320, 24)<br/> Me.Label2.TabIndex = 7<br/> Me.Label2.Text = "系统登录"<br/> Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter<br/> '<br/> 'btnCancel<br/> '<br/> Me.btnCancel.BackColor = System.Drawing.SystemColors.ActiveBorder<br/> Me.btnCancel.Cursor = System.Windows.Forms.Cursors.Hand<br/> Me.btnCancel.Location = New System.Drawing.Point(225, 215)<br/> Me.btnCancel.Name = "btnCancel"<br/> Me.btnCancel.TabIndex = 10<br/> Me.btnCancel.Text = "取消"<br/> '<br/> 'Label3<br/> '<br/> Me.Label3.ForeColor = System.Drawing.Color.Red<br/> Me.Label3.Location = New System.Drawing.Point(163, 262)<br/> Me.Label3.Name = "Label3"<br/> Me.Label3.Size = New System.Drawing.Size(149, 16)<br/> Me.Label3.TabIndex = 11<br/> Me.Label3.Text = "为保证系统安全,请先登录"<br/> '<br/> 'loginForm<br/> '<br/> Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)<br/> Me.BackColor = System.Drawing.SystemColors.ActiveBorder<br/> Me.ClientSize = New System.Drawing.Size(319, 284)<br/> Me.Controls.Add(Me.Label3)<br/> Me.Controls.Add(Me.btnCancel)<br/> Me.Controls.Add(Me.Label2)<br/> Me.Controls.Add(Me.Label1)<br/> Me.Controls.Add(Me.btnExit)<br/> Me.Controls.Add(Me.txtUserPwd)<br/> Me.Controls.Add(Me.txtUserName)<br/> Me.Controls.Add(Me.btnSubmit)<br/> Me.Controls.Add(Me.lblUserName)<br/> Me.Controls.Add(Me.lblUserPwd)<br/> Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None<br/> Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)<br/> Me.MaximizeBox = False<br/> Me.MinimizeBox = False<br/> Me.Name = "loginForm"<br/> Me.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide<br/> Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen<br/> Me.Tag = "登录"<br/> Me.Text = "TEST--系统登录"<br/> Me.ResumeLayout(False)<br/> End Sub<br/>#End Region<br/> Private Sub loginForm_Load()Sub loginForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load<br/> txtUserName.Focus()<br/> ' btnCancel.Visible = False<br/> End Sub<br/> Private Sub btnExit_Click()Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click<br/> Application.Exit()<br/> End Sub<br/> Private Sub btnSubmit_Click()Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click<br/> '调用loginValidator组件<br/> Dim validator As New Validator<br/> validator.vUsername = txtUserName.Text.Trim<br/> validator.vUserpwd = txtUserPwd.Text.Trim<br/> If (validator.validate() = True) Then<br/> Me.Close()<br/> Else<br/> MessageBox.Show("帐号或密码错误,请重试!", "验证错误", MessageBoxButtons.OK, MessageBoxIcon.Warning)<br/> End If<br/> End Sub<br/> Private Sub btnCancel_Click()Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)<br/> Me.Dispose()<br/> End Sub<br/> Private Sub btnCancel_Click_1()Sub btnCancel_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click<br/> Me.Close()<br/> End Sub<br/>End Class<br/>关于在.NET中使用COM组件:.NET可以向后兼容,并支持了COM和ActiveX对象等早期版本的应用程序。在.NET中使用COM等很有趣,.NET创建一个包将它们包装起来,而它们之间的交互就是通过这个包装来进行的,这个包就叫做运行时可呼叫包装(RCW)。可以直接使用COM,也可以先转换成.NET程序集后再使用。<br/>1、直接使用COM组件是通过Add Reference来实现的,这种方法通过RCW来包装。缺点:无法放到 GAC中,不能重用。<br/> 2、通过转换成.NET程序集来使用,是通过利用 tlbimp 命令行工具来实现的,语法:<br/> tlbimp COMName.dll /out:Comp.dll /namespace:myNS /asmVersion: myVersion /reference:refname<br/> 第一个为COM的名称,第二个为要生成的.NET组件名称,第三个为要生成的名称空间,第四个为 版本号,第四个指定引用的文件名。 </p>
页:
[1]