網站運營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組件名稱,第三個為要生成的名稱空間,第四個為 版本號,第四個指定引用的文件名。