簡介組件其實是一段可以重用的代碼
通過遵循IComponent接口的標准來實現一個組件
所以有組件都是派生於Component類
由Component類來實現IComponent接口
在組件中應正確使用函數的訪問級別來控制外部對其的訪問限制
只要有足夠的權限就可以將組件放到自己的程序中而不用擔心組件會產生多大的錯誤
因為組件已經經過測試的
比如說可以把一段登錄的程序做成一個組件
或者把經常使用到的一些功能也做成組件
這樣就可以減少開發中的錯誤
也可以縮短開發時間
組件之間也可以互相套用
如一個組件引用另一個組件
都是沒問題
但要先在Add Reference中添加對組件的引用
在
NET中是通過把組件放在程序集中來實現的
程序集中存放著這些組件所依賴的文件信息和所在路徑
因此CLR就可以通過這些信息來確定組件所需要的其他程序集的位置
( 另外在組件設計過程中應好好利用接口來設計組件)
在VS中創建組件
選建一個Project
再從模板中選Class Library
OK
接著再從Project菜單中Add Component
到些為止
組件的一個框架就呈現在眼前
平台自動繼承了Component類和構造函數
可以刪除原先創建類庫時自動生成的Class
看應用的需要
接著就可以在組件類裡寫要實現的功能
最後從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 Label
As System
Windows
Forms
Label
Friend WithEvents Label
As System
Windows
Forms
Label
Friend WithEvents btnCancel As System
Windows
Forms
Button
Friend WithEvents Label
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
Label
= New System
Windows
Forms
Label
Me
Label
= New System
Windows
Forms
Label
Me
btnCancel = New System
Windows
Forms
Button
Me
Label
= New System
Windows
Forms
Label
Me
SuspendLayout()
lblUserPwd
Me
lblUserPwd
Location = New System
Drawing
Point(
)
Me
lblUserPwd
Name =
lblUserPwd
Me
lblUserPwd
Size = New System
Drawing
Size(
)
Me
lblUserPwd
TabIndex =
Me
lblUserPwd
Text =
密碼
Me
lblUserPwd
TextAlign = System
Drawing
ContentAlignment
MiddleCenter
lblUserName Me
lblUserName
Location = New System
Drawing
Point(
)
Me
lblUserName
Name =
lblUserName
Me
lblUserName
Size = New System
Drawing
Size(
)
Me
lblUserName
TabIndex =
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(
)
Me
txtUserName
Name =
txtUserName
Me
txtUserName
Size = New System
Drawing
Size(
)
Me
txtUserName
TabIndex =
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(
)
Me
txtUserPwd
Name =
txtUserPwd
Me
txtUserPwd
PasswordChar = Microsoft
VisualBasic
ChrW(
)
Me
txtUserPwd
Size = New System
Drawing
Size(
)
Me
txtUserPwd
TabIndex =
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(
)
Me
btnSubmit
Name =
btnSubmit
Me
btnSubmit
TabIndex =
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(
)
Me
btnExit
Name =
btnExit
Me
btnExit
TabIndex =
Me
btnExit
Text =
退出
Label Me
Label
Font = New System
Drawing
Font(
Impact
!
System
Drawing
FontStyle
Regular
System
Drawing
GraphicsUnit
Point
CType(
Byte))
Me
Label
Location = New System
Drawing
Point(
)
Me
Label
Name =
Label
Me
Label
Size = New System
Drawing
Size(
)
Me
Label
TabIndex =
Me
Label
Text =
TEST
Label Me
Label
BackColor = System
Drawing
SystemColors
ControlText
Me
Label
ForeColor = System
Drawing
SystemColors
HighlightText
Me
Label
Location = New System
Drawing
Point(
)
Me
Label
Name =
Label
Me
Label
Size = New System
Drawing
Size(
)
Me
Label
TabIndex =
Me
Label
Text =
系統登錄
Me
Label
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(
)
Me
btnCancel
Name =
btnCancel
Me
btnCancel
TabIndex =
Me
btnCancel
Text =
取消
Label Me
Label
ForeColor = System
Drawing
Color
Red
Me
Label
Location = New System
Drawing
Point(
)
Me
Label
Name =
Label
Me
Label
Size = New System
Drawing
Size(
)
Me
Label
TabIndex =
Me
Label
Text =
為保證系統安全
請先登錄
loginForm Me
AutoScaleBaseSize = New System
Drawing
Size(
)
Me
BackColor = System
Drawing
SystemColors
ActiveBorder
Me
ClientSize = New System
Drawing
Size(
)
Me
Controls
Add(Me
Label
)
Me
Controls
Add(Me
btnCancel)
Me
Controls
Add(Me
Label
)
Me
Controls
Add(Me
Label
)
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_
()Sub btnCancel_Click_
(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程序集後再使用
直接使用COM組件是通過Add Reference來實現的
這種方法通過RCW來包裝
缺點
無法放到 GAC中
不能重用
通過轉換成
NET程序集來使用
是通過利用 tlbimp 命令行工具來實現的
語法
tlbimp COMName
dll /out:Comp
dll /namespace:myNS /asmVersion: myVersion /reference:refname
第一個為COM的名稱
第二個為要生成的
NET組件名稱
第三個為要生成的名稱空間
第四個為 版本號
第四個指定引用的文件名
From:http://tw.wingwit.com/Article/program/net/201311/12392.html