VB.NET中的组件开发
None.gif
None.gif
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
None.gif'
??????????????????????????????????????????????????????????????'
None.gif'
?????????????登录验证组件?????????????????????????????????????'
None.gif'
??????????????????????????????????????????????????????????????'
None.gif'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
None.gif

None.gif
Imports?System.Security.Cryptography
None.gif
Imports?System.Text
None.gif
Imports?System.Data
None.gif
Imports?System.Data.SqlClient
None.gif
ExpandedBlockStart.gif
Public?Class?Validator
InBlock.gif????
Inherits?System.ComponentModel.Component
InBlock.gif
InBlock.gif????
Private?username?As?String
InBlock.gif????
Private?userpwd?As?String
InBlock.gif
ExpandedSubBlockStart.gif????
Public?Property?vUsername()?As?String
InBlock.gif????????
Get
InBlock.gif????????????
Return?username
InBlock.gif????????
End?Get
InBlock.gif????????
Set(ByVal?Value?As?String)
InBlock.gif????????????username?
=?Value
InBlock.gif????????
End?Set
ExpandedSubBlockEnd.gif????
End?Property

InBlock.gif
ExpandedSubBlockStart.gif????
Public?Property?vUserpwd()?As?String
InBlock.gif????????
Get
InBlock.gif????????????
Return?userpwd
InBlock.gif????????
End?Get
InBlock.gif????????
Set(ByVal?Value?As?String)
InBlock.gif????????????userpwd?
=?Value
InBlock.gif????????
End?Set
ExpandedSubBlockEnd.gif????
End?Property

InBlock.gif
InBlock.gif????
'转换为MD5
ExpandedSubBlockStart.gif
????Private?Function?convertMD5(ByVal?pwd?As?String)?As?String
InBlock.gif
InBlock.gif????????
Dim?md5?As?New?MD5CryptoServiceProvider
InBlock.gif????????
Dim?password?As?Byte()?=?(New?ASCIIEncoding).GetBytes(pwd)
InBlock.gif
InBlock.gif????????
'转换为哈希值Byte数组
InBlock.gif
????????Dim?mdByte?As?Byte()?=?md5.ComputeHash(password)
InBlock.gif????????
'Dim?mdString?As?String?=?System.BitConverter.ToString(mdByte)
InBlock.gif
????????Dim?mdString?As?String?=?(New?ASCIIEncoding).GetString(mdByte)
InBlock.gif????????
Return?mdString
InBlock.gif
ExpandedSubBlockEnd.gif????
End?Function

InBlock.gif
ExpandedSubBlockStart.gif????
Public?Function?validate()?As?Boolean
InBlock.gif
InBlock.gif????????
'连接到Users表
InBlock.gif

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

InBlock.gif#Region?
"?组件设计器生成的代码?"
InBlock.gif

ExpandedSubBlockStart.gif
????Public?Sub?New(ByVal?Container?As?System.ComponentModel.IContainer)
InBlock.gif????????
MyClass.New()
InBlock.gif
InBlock.gif????????
'Windows.Forms?类撰写设计器支持所必需的
InBlock.gif
????????Container.Add(Me)
ExpandedSubBlockEnd.gif????
End?Sub

InBlock.gif
ExpandedSubBlockStart.gif????
Public?Sub?New()
InBlock.gif????????
MyBase.New()
InBlock.gif
InBlock.gif????????
'该调用是组件设计器所必需的。
InBlock.gif
????????InitializeComponent()
InBlock.gif
InBlock.gif????????
'在?InitializeComponent()?调用之后添加任何初始化
InBlock.gif

ExpandedSubBlockEnd.gif????
End?Sub

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

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

InBlock.gif
InBlock.gif#
End?Region
InBlock.gif
ExpandedBlockEnd.gif
End?Class

None.gif

?简介:组件其实是一段可以重用的代码,通过遵循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就可以了。

None.gifImports?loginValidator
None.gif
Imports?System.Data
None.gif
Imports?System.Data.SqlClient
None.gif
ExpandedBlockStart.gif
Public?Class?loginForm
InBlock.gif????
Inherits?System.Windows.Forms.Form
InBlock.gif
InBlock.gif#Region?
"?Windows?窗体设计器生成的代码?"
InBlock.gif

ExpandedSubBlockStart.gif
????Public?Sub?New()
InBlock.gif????????
MyBase.New()
InBlock.gif
InBlock.gif????????
'该调用是?Windows?窗体设计器所必需的。
InBlock.gif
????????InitializeComponent()
InBlock.gif
InBlock.gif????????
'在?InitializeComponent()?调用之后添加任何初始化
InBlock.gif

ExpandedSubBlockEnd.gif????
End?Sub

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

InBlock.gif
InBlock.gif????
'Windows?窗体设计器所必需的
InBlock.gif
????Private?components?As?System.ComponentModel.IContainer
InBlock.gif
InBlock.gif????
'注意:?以下过程是?Windows?窗体设计器所必需的
InBlock.gif
????'可以使用?Windows?窗体设计器修改此过程。
InBlock.gif
????'不要使用代码编辑器修改它。
InBlock.gif
????Friend?WithEvents?lblUserPwd?As?System.Windows.Forms.Label
InBlock.gif????
Friend?WithEvents?lblUserName?As?System.Windows.Forms.Label
InBlock.gif????
Friend?WithEvents?txtUserName?As?System.Windows.Forms.TextBox
InBlock.gif????
Friend?WithEvents?txtUserPwd?As?System.Windows.Forms.TextBox
InBlock.gif????
Friend?WithEvents?btnSubmit?As?System.Windows.Forms.Button
InBlock.gif????
Friend?WithEvents?btnExit?As?System.Windows.Forms.Button
InBlock.gif????
Friend?WithEvents?Label1?As?System.Windows.Forms.Label
InBlock.gif????
Friend?WithEvents?Label2?As?System.Windows.Forms.Label
InBlock.gif????
Friend?WithEvents?btnCancel?As?System.Windows.Forms.Button
InBlock.gif????
Friend?WithEvents?Label3?As?System.Windows.Forms.Label
ExpandedSubBlockStart.gif????
<System.Diagnostics.DebuggerStepThrough()>?Private?Sub?InitializeComponent()
InBlock.gif????????
Dim?resources?As?System.Resources.ResourceManager?=?New?System.Resources.ResourceManager(GetType(loginForm))
InBlock.gif????????Me.lblUserPwd?
=?New?System.Windows.Forms.Label
InBlock.gif????????Me.lblUserName?
=?New?System.Windows.Forms.Label
InBlock.gif????????Me.txtUserName?
=?New?System.Windows.Forms.TextBox
InBlock.gif????????Me.txtUserPwd?
=?New?System.Windows.Forms.TextBox
InBlock.gif????????Me.btnSubmit?
=?New?System.Windows.Forms.Button
InBlock.gif????????Me.btnExit?
=?New?System.Windows.Forms.Button
InBlock.gif????????Me.Label1?
=?New?System.Windows.Forms.Label
InBlock.gif????????Me.Label2?
=?New?System.Windows.Forms.Label
InBlock.gif????????Me.btnCancel?
=?New?System.Windows.Forms.Button
InBlock.gif????????Me.Label3?
=?New?System.Windows.Forms.Label
InBlock.gif????????Me.SuspendLayout()
InBlock.gif????????
'
InBlock.gif
????????'lblUserPwd
InBlock.gif
????????'
InBlock.gif
????????Me.lblUserPwd.Location?=?New?System.Drawing.Point(46,?176)
InBlock.gif????????Me.lblUserPwd.Name?
=?"lblUserPwd"
InBlock.gif
????????Me.lblUserPwd.Size?=?New?System.Drawing.Size(52,?23)
InBlock.gif????????Me.lblUserPwd.TabIndex?
=?0
InBlock.gif????????Me.lblUserPwd.Text?
=?"密码:"
InBlock.gif
????????Me.lblUserPwd.TextAlign?=?System.Drawing.ContentAlignment.MiddleCenter
InBlock.gif????????
'
InBlock.gif
????????'lblUserName
InBlock.gif
????????'
InBlock.gif
????????Me.lblUserName.Location?=?New?System.Drawing.Point(46,?128)
InBlock.gif????????Me.lblUserName.Name?
=?"lblUserName"
InBlock.gif
????????Me.lblUserName.Size?=?New?System.Drawing.Size(52,?23)
InBlock.gif????????Me.lblUserName.TabIndex?
=?1
InBlock.gif????????Me.lblUserName.Text?
=?"帐号:"
InBlock.gif
????????Me.lblUserName.TextAlign?=?System.Drawing.ContentAlignment.MiddleCenter
InBlock.gif????????
'
InBlock.gif
????????'txtUserName
InBlock.gif
????????'
InBlock.gif
????????Me.txtUserName.BackColor?=?System.Drawing.SystemColors.Info
InBlock.gif????????Me.txtUserName.BorderStyle?
=?System.Windows.Forms.BorderStyle.FixedSingle
InBlock.gif????????Me.txtUserName.Location?
=?New?System.Drawing.Point(110,?128)
InBlock.gif????????Me.txtUserName.Name?
=?"txtUserName"
InBlock.gif
????????Me.txtUserName.Size?=?New?System.Drawing.Size(151,?21)
InBlock.gif????????Me.txtUserName.TabIndex?
=?3
InBlock.gif????????Me.txtUserName.Text?
=?""
InBlock.gif
????????'
InBlock.gif
????????'txtUserPwd
InBlock.gif
????????'
InBlock.gif
????????Me.txtUserPwd.BackColor?=?System.Drawing.SystemColors.Info
InBlock.gif????????Me.txtUserPwd.BorderStyle?
=?System.Windows.Forms.BorderStyle.FixedSingle
InBlock.gif????????Me.txtUserPwd.Location?
=?New?System.Drawing.Point(110,?176)
InBlock.gif????????Me.txtUserPwd.Name?
=?"txtUserPwd"
InBlock.gif
????????Me.txtUserPwd.PasswordChar?=?Microsoft.VisualBasic.ChrW(42)
InBlock.gif????????Me.txtUserPwd.Size?
=?New?System.Drawing.Size(151,?21)
InBlock.gif????????Me.txtUserPwd.TabIndex?
=?4
InBlock.gif????????Me.txtUserPwd.Text?
=?""
InBlock.gif
????????'
InBlock.gif
????????'btnSubmit
InBlock.gif
????????'
InBlock.gif
????????Me.btnSubmit.BackColor?=?System.Drawing.SystemColors.ActiveBorder
InBlock.gif????????Me.btnSubmit.Cursor?
=?System.Windows.Forms.Cursors.Hand
InBlock.gif????????Me.btnSubmit.ForeColor?
=?System.Drawing.SystemColors.InfoText
InBlock.gif????????Me.btnSubmit.Location?
=?New?System.Drawing.Point(56,?216)
InBlock.gif????????Me.btnSubmit.Name?
=?"btnSubmit"
InBlock.gif
????????Me.btnSubmit.TabIndex?=?5
InBlock.gif????????Me.btnSubmit.Text?
=?"登录"
InBlock.gif
????????'
InBlock.gif
????????'btnExit
InBlock.gif
????????'
InBlock.gif
????????Me.btnExit.BackColor?=?System.Drawing.SystemColors.ActiveBorder
InBlock.gif????????Me.btnExit.Cursor?
=?System.Windows.Forms.Cursors.Hand
InBlock.gif????????Me.btnExit.Location?
=?New?System.Drawing.Point(141,?216)
InBlock.gif????????Me.btnExit.Name?
=?"btnExit"
InBlock.gif
????????Me.btnExit.TabIndex?=?6
InBlock.gif????????Me.btnExit.Text?
=?"退出"
InBlock.gif
????????'
InBlock.gif
????????'Label1
InBlock.gif
????????'
InBlock.gif
????????Me.Label1.Font?=?New?System.Drawing.Font("Impact",?17.0!,?System.Drawing.FontStyle.Regular,?System.Drawing.GraphicsUnit.Point,?CType(134,?Byte))
InBlock.gif????????Me.Label1.Location?
=?New?System.Drawing.Point(38,?64)
InBlock.gif????????Me.Label1.Name?
=?"Label1"
InBlock.gif
????????Me.Label1.Size?=?New?System.Drawing.Size(256,?32)
InBlock.gif????????Me.Label1.TabIndex?
=?6
InBlock.gif????????Me.Label1.Text?
=?"TEST"
InBlock.gif
????????'
InBlock.gif
????????'Label2
InBlock.gif
????????'
InBlock.gif
????????Me.Label2.BackColor?=?System.Drawing.SystemColors.ControlText
InBlock.gif????????Me.Label2.ForeColor?
=?System.Drawing.SystemColors.HighlightText
InBlock.gif????????Me.Label2.Location?
=?New?System.Drawing.Point(0,?0)
InBlock.gif????????Me.Label2.Name?
=?"Label2"
InBlock.gif
????????Me.Label2.Size?=?New?System.Drawing.Size(320,?24)
InBlock.gif????????Me.Label2.TabIndex?
=?7
InBlock.gif????????Me.Label2.Text?
=?"系统登录"
InBlock.gif
????????Me.Label2.TextAlign?=?System.Drawing.ContentAlignment.MiddleCenter
InBlock.gif????????
'
InBlock.gif
????????'btnCancel
InBlock.gif
????????'
InBlock.gif
????????Me.btnCancel.BackColor?=?System.Drawing.SystemColors.ActiveBorder
InBlock.gif????????Me.btnCancel.Cursor?
=?System.Windows.Forms.Cursors.Hand
InBlock.gif????????Me.btnCancel.Location?
=?New?System.Drawing.Point(225,?215)
InBlock.gif????????Me.btnCancel.Name?
=?"btnCancel"
InBlock.gif
????????Me.btnCancel.TabIndex?=?10
InBlock.gif????????Me.btnCancel.Text?
=?"取消"
InBlock.gif
????????'
InBlock.gif
????????'Label3
InBlock.gif
????????'
InBlock.gif
????????Me.Label3.ForeColor?=?System.Drawing.Color.Red
InBlock.gif????????Me.Label3.Location?
=?New?System.Drawing.Point(163,?262)
InBlock.gif????????Me.Label3.Name?
=?"Label3"
InBlock.gif
????????Me.Label3.Size?=?New?System.Drawing.Size(149,?16)
InBlock.gif????????Me.Label3.TabIndex?
=?11
InBlock.gif????????Me.Label3.Text?
=?"为保证系统安全,请先登录"
InBlock.gif
????????'
InBlock.gif
????????'loginForm
InBlock.gif
????????'
InBlock.gif
????????Me.AutoScaleBaseSize?=?New?System.Drawing.Size(6,?14)
InBlock.gif????????Me.BackColor?
=?System.Drawing.SystemColors.ActiveBorder
InBlock.gif????????Me.ClientSize?
=?New?System.Drawing.Size(319,?284)
InBlock.gif????????Me.Controls.Add(Me.Label3)
InBlock.gif????????Me.Controls.Add(Me.btnCancel)
InBlock.gif????????Me.Controls.Add(Me.Label2)
InBlock.gif????????Me.Controls.Add(Me.Label1)
InBlock.gif????????Me.Controls.Add(Me.btnExit)
InBlock.gif????????Me.Controls.Add(Me.txtUserPwd)
InBlock.gif????????Me.Controls.Add(Me.txtUserName)
InBlock.gif????????Me.Controls.Add(Me.btnSubmit)
InBlock.gif????????Me.Controls.Add(Me.lblUserName)
InBlock.gif????????Me.Controls.Add(Me.lblUserPwd)
InBlock.gif????????Me.FormBorderStyle?
=?System.Windows.Forms.FormBorderStyle.None
InBlock.gif????????Me.Icon?
=?CType(resources.GetObject("$this.Icon"),?System.Drawing.Icon)
InBlock.gif????????Me.MaximizeBox?
=?False
InBlock.gif????????Me.MinimizeBox?
=?False
InBlock.gif????????Me.Name?
=?"loginForm"
InBlock.gif
????????Me.SizeGripStyle?=?System.Windows.Forms.SizeGripStyle.Hide
InBlock.gif????????Me.StartPosition?
=?System.Windows.Forms.FormStartPosition.CenterScreen
InBlock.gif????????Me.Tag?
=?"登录"
InBlock.gif
????????Me.Text?=?"TEST--系统登录"
InBlock.gif
????????Me.ResumeLayout(False)
InBlock.gif
ExpandedSubBlockEnd.gif????
End?Sub

InBlock.gif
InBlock.gif#
End?Region
InBlock.gif
ExpandedSubBlockStart.gif????
Private?Sub?loginForm_Load(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?MyBase.Load
InBlock.gif????????txtUserName.Focus()
InBlock.gif????????
'?btnCancel.Visible?=?False
ExpandedSubBlockEnd.gif
????End?Sub

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

InBlock.gif
ExpandedSubBlockStart.gif????
Private?Sub?btnSubmit_Click(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?btnSubmit.Click
InBlock.gif
InBlock.gif????????
'调用loginValidator组件
InBlock.gif

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

InBlock.gif
ExpandedSubBlockStart.gif????
Private?Sub?btnCancel_Click(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)
InBlock.gif????????Me.Dispose()
ExpandedSubBlockEnd.gif????
End?Sub

InBlock.gif
ExpandedSubBlockStart.gif????
Private?Sub?btnCancel_Click_1(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?btnCancel.Click
InBlock.gif????????Me.Close()
InBlock.gif
ExpandedSubBlockEnd.gif????
End?Sub

ExpandedBlockEnd.gif
End?Class

None.gif

?关于在.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

??第一个为COM的名称,第二个为要生成的.NET组件名称,第三个为要生成的名称空间,第四个为 版本号,第四个指定引用的文件名。

阅读更多
个人分类: .NET
想对作者说点什么? 我来说一句

VB.NET 面向对象可重用组件开发

2007年11月06日 7.37MB 下载

黑魔方vb.net组件开发源码

2008年12月03日 2.45MB 下载

VB.NET组件开发实例教程

2009年07月06日 2.43MB 下载

PHP组件开发实例.rar

2009年07月15日 23.51MB 下载

没有更多推荐了,返回首页

不良信息举报

VB.NET中的组件开发

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭