网站运营seo文章大全提供全面的站长运营经验及seo技术!先看段组件的代码:(临时写的,写得比较乱)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' '
' 登录验证组件 '
' '
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
imports system.security.cryptography
imports system.text
imports system.data
imports system.data.sqlclient
public class validatorclass validator
inherits system.componentmodel.component
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 convertmd5()function convertmd5(byval pwd as string) as 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()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 > 0) 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 system.componentmodel.icontainer)
myclass.new()
'windows.forms 类撰写设计器支持所必需的
container.add(me)
end sub
public sub new()sub new()
mybase.new()
'该调用是组件设计器所必需的。
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
'组件设计器所必需的
private components as system.componentmodel.icontainer
'注意: 以下过程是组件设计器所必需的
'可以使用组件设计器修改此过程。
'不要使用代码编辑器修改它。
<system.diagnostics.debuggerstepthrough()> private sub initializecomponent()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 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 = "登录"
'
'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
第一个为com的名称,第二个为要生成的.net组件名称,第三个为要生成的名称空间,第四个为 版本号,第四个指定引用的文件名。