×

VB.NET中的组件开发

Kalet Kalet 发表于2009-03-20 12:00:14 浏览159 评论0

抢沙发发表评论

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

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

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

  在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 loginFormClass loginForm
Inherits System.Windows.Forms.Form

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

Public Sub New()Sub New()
MyBase.New()

'该调用是 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
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()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(46, 176)
Me.lblUserPwd.Name = "lblUserPwd"
Me.lblUserPwd.Size = New System.Drawing.Size(52, 23)
Me.lblUserPwd.TabIndex = 0
Me.lblUserPwd.Text = "密码:"
Me.lblUserPwd.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'lblUserName
'
Me.lblUserName.Location = New System.Drawing.Point(46, 128)
Me.lblUserName.Name = "lblUserName"
Me.lblUserName.Size = New System.Drawing.Size(52, 23)
Me.lblUserName.TabIndex = 1
Me.lblUserName.Text = "帐号:"
Me.lblUserName.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'txtUserName
'
Me.txtUserName.BackColor = System.Drawing.SystemColors.Info
Me.txtUserName.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.txtUserName.Location = New System.Drawing.Point(110, 128)
Me.txtUserName.Name = "txtUserName"
Me.txtUserName.Size = New System.Drawing.Size(151, 21)
Me.txtUserName.TabIndex = 3
Me.txtUserName.Text = ""
'
'txtUserPwd
'
Me.txtUserPwd.BackColor = System.Drawing.SystemColors.Info
Me.txtUserPwd.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.txtUserPwd.Location = New System.Drawing.Point(110, 176)
Me.txtUserPwd.Name = "txtUserPwd"
Me.txtUserPwd.PasswordChar = Microsoft.VisualBasic.ChrW(42)
Me.txtUserPwd.Size = New System.Drawing.Size(151, 21)
Me.txtUserPwd.TabIndex = 4
Me.txtUserPwd.Text = ""
'
'btnSubmit
'
Me.btnSubmit.BackColor = System.Drawing.SystemColors.ActiveBorder
Me.btnSubmit.Cursor = System.Windows.Forms.Cursors.Hand
Me.btnSubmit.ForeColor = System.Drawing.SystemColors.InfoText
Me.btnSubmit.Location = New System.Drawing.Point(56, 216)
Me.btnSubmit.Name = "btnSubmit"
Me.btnSubmit.TabIndex = 5
Me.btnSubmit.Text = "登录"
' VB.NET中的组件开发
'btnExit
'
Me.btnExit.BackColor = System.Drawing.SystemColors.ActiveBorder
Me.btnExit.Cursor = System.Windows.Forms.Cursors.Hand
Me.btnExit.Location = New System.Drawing.Point(141, 216)
Me.btnExit.Name = "btnExit"
Me.btnExit.TabIndex = 6
Me.btnExit.Text = "退出"
'
'Label1
'
Me.Label1.Font = New System.Drawing.Font("Impact", 17.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
Me.Label1.Location = New System.Drawing.Point(38, 64)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(256, 32)
Me.Label1.TabIndex = 6
Me.Label1.Text = "TEST"
'
'Label2
'
Me.Label2.BackColor = System.Drawing.SystemColors.ControlText
Me.Label2.ForeColor = System.Drawing.SystemColors.HighlightText
Me.Label2.Location = New System.Drawing.Point(0, 0)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(320, 24)
Me.Label2.TabIndex = 7
Me.Label2.Text = "系统登录"
Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'btnCancel
'
Me.btnCancel.BackColor = System.Drawing.SystemColors.ActiveBorder
Me.btnCancel.Cursor = System.Windows.Forms.Cursors.Hand
Me.btnCancel.Location = New System.Drawing.Point(225, 215)
Me.btnCancel.Name = "btnCancel"
Me.btnCancel.TabIndex = 10
Me.btnCancel.Text = "取消"
'
'Label3
'
Me.Label3.ForeColor = System.Drawing.Color.Red
Me.Label3.Location = New System.Drawing.Point(163, 262)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(149, 16)
Me.Label3.TabIndex = 11
Me.Label3.Text = "为保证系统安全,请先登录"
'
'loginForm
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.BackColor = System.Drawing.SystemColors.ActiveBorder
Me.ClientSize = New System.Drawing.Size(319, 284)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.btnCancel)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.btnExit)
Me.Controls.Add(Me.txtUserPwd)
Me.Controls.Add(Me.txtUserName)
Me.Controls.Add(Me.btnSubmit)
Me.Controls.Add(Me.lblUserName)
Me.Controls.Add(Me.lblUserPwd)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "loginForm"
Me.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Tag = "登录"
Me.Text = "TEST--系统登录"
Me.ResumeLayout(False)
End Sub

#End Region

Private Sub loginForm_Load()Sub loginForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txtUserName.Focus()
' btnCancel.Visible = False
End Sub

Private Sub btnExit_Click()Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Application.Exit()
End Sub

Private Sub btnSubmit_Click()Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click

'调用loginValidator组件

Dim validator As New Validator
validator.vUsername = txtUserName.Text.Trim
validator.vUserpwd = txtUserPwd.Text.Trim
If (validator.validate() = True) Then
Me.Close()
Else
MessageBox.Show("帐号或密码错误,请重试!", "验证错误", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
End Sub

Private Sub btnCancel_Click()Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Dispose()
End Sub

Private Sub btnCancel_Click_1()Sub btnCancel_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
Me.Close()
End Sub
End Class

  关于在.NET中使用COM组件:.NET可以向后兼容,并支持了COM和ActiveX对象等早期版本的应用程序。在.NET中使用COM等很有趣,.NET创建一个包将它们包装起来,而它们之间的交互就是通过这个包装来进行的,这个包就叫做运行时可呼叫包装(RCW)。可以直接使用COM,也可以先转换成.NET程序集后再使用。

  1、直接使用COM组件是通过Add Reference来实现的,这种方法通过RCW来包装。缺点:无法放到 GAC中,不能重用。

  2、通过转换成.NET程序集来使用,是通过利用 tlbimp 命令行工具来实现的,语法: tlbimp COMName.dll /out:Comp.dll /namespace:myNS /asmVersion: myVersion /reference:refname
VB.NET中的组件开发
  第一个为COM的名称,第二个为要生成的.NET组件名称,第三个为要生成的名称空间,第四个为 版本号,第四个指定引用的文件名。


群贤毕至

访客