熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

VB.NET中的組件開發

2022-06-13   來源: .NET編程 
簡介組件其實是一段可以重用的代碼通過遵循IComponent接口的標准來實現一個組件所以有組件都是派生於Component類由Component類來實現IComponent接口在組件中應正確使用函數的訪問級別來控制外部對其的訪問限制

  只要有足夠的權限就可以將組件放到自己的程序中而不用擔心組件會產生多大的錯誤因為組件已經經過測試的比如說可以把一段登錄的程序做成一個組件或者把經常使用到的一些功能也做成組件這樣就可以減少開發中的錯誤也可以縮短開發時間組件之間也可以互相套用如一個組件引用另一個組件都是沒問題但要先在Add Reference中添加對組件的引用NET中是通過把組件放在程序集中來實現的程序集中存放著這些組件所依賴的文件信息和所在路徑因此CLR就可以通過這些信息來確定組件所需要的其他程序集的位置

( 另外在組件設計過程中應好好利用接口來設計組件)

  在VS中創建組件選建一個Project再從模板中選Class LibraryOK接著再從Project菜單中Add Component到些為止組件的一個框架就呈現在眼前平台自動繼承了Component類和構造函數可以刪除原先創建類庫時自動生成的Class看應用的需要接著就可以在組件類裡寫要實現的功能最後從Build(生成)菜單中選擇Build Solution(生成解決方案)來生成組件如果生成成功的話到應用程序的BIN目錄下會看到一個DLL文件

  引用組件只要在Solution Explorer窗口中添加對DLL的Reference就可以了

Imports loginValidator
Imports SystemData
Imports SystemDataSqlClient

Public Class loginFormClass loginForm
Inherits SystemWindowsFormsForm

#Region Windows 窗體設計器生成的代碼

Public Sub New()Sub New()
MyBaseNew()

該調用是 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
componentsDispose()
End If
End If
MyBaseDispose(disposing)
End Sub

Windows 窗體設計器所必需的
Private components As SystemComponentModelIContainer

注意: 以下過程是 Windows 窗體設計器所必需的
可以使用 Windows 窗體設計器修改此過程
不要使用代碼編輯器修改它
Friend WithEvents lblUserPwd As SystemWindowsFormsLabel
Friend WithEvents lblUserName As SystemWindowsFormsLabel
Friend WithEvents txtUserName As SystemWindowsFormsTextBox
Friend WithEvents txtUserPwd As SystemWindowsFormsTextBox
Friend WithEvents btnSubmit As SystemWindowsFormsButton
Friend WithEvents btnExit As SystemWindowsFormsButton
Friend WithEvents Label As SystemWindowsFormsLabel
Friend WithEvents Label As SystemWindowsFormsLabel
Friend WithEvents btnCancel As SystemWindowsFormsButton
Friend WithEvents Label As SystemWindowsFormsLabel
<SystemDiagnosticsDebuggerStepThrough()> Private Sub InitializeComponent()Sub InitializeComponent()
Dim resources As SystemResourcesResourceManager = New SystemResourcesResourceManager(GetType(loginForm))
MelblUserPwd = New SystemWindowsFormsLabel
MelblUserName = New SystemWindowsFormsLabel
MetxtUserName = New SystemWindowsFormsTextBox
MetxtUserPwd = New SystemWindowsFormsTextBox
MebtnSubmit = New SystemWindowsFormsButton
MebtnExit = New SystemWindowsFormsButton
MeLabel = New SystemWindowsFormsLabel
MeLabel = New SystemWindowsFormsLabel
MebtnCancel = New SystemWindowsFormsButton
MeLabel = New SystemWindowsFormsLabel
MeSuspendLayout()

lblUserPwd

MelblUserPwdLocation = New SystemDrawingPoint( )
MelblUserPwdName = lblUserPwd
MelblUserPwdSize = New SystemDrawingSize( )
MelblUserPwdTabIndex =
MelblUserPwdText = 密碼
MelblUserPwdTextAlign = SystemDrawingContentAlignmentMiddleCenter

lblUserName

MelblUserNameLocation = New SystemDrawingPoint( )
MelblUserNameName = lblUserName
MelblUserNameSize = New SystemDrawingSize( )
MelblUserNameTabIndex =
MelblUserNameText = 帳號
MelblUserNameTextAlign = SystemDrawingContentAlignmentMiddleCenter

txtUserName

MetxtUserNameBackColor = SystemDrawingSystemColorsInfo
MetxtUserNameBorderStyle = SystemWindowsFormsBorderStyleFixedSingle
MetxtUserNameLocation = New SystemDrawingPoint( )
MetxtUserNameName = txtUserName
MetxtUserNameSize = New SystemDrawingSize( )
MetxtUserNameTabIndex =
MetxtUserNameText =

txtUserPwd

MetxtUserPwdBackColor = SystemDrawingSystemColorsInfo
MetxtUserPwdBorderStyle = SystemWindowsFormsBorderStyleFixedSingle
MetxtUserPwdLocation = New SystemDrawingPoint( )
MetxtUserPwdName = txtUserPwd
MetxtUserPwdPasswordChar = MicrosoftVisualBasicChrW()
MetxtUserPwdSize = New SystemDrawingSize( )
MetxtUserPwdTabIndex =
MetxtUserPwdText =

btnSubmit

MebtnSubmitBackColor = SystemDrawingSystemColorsActiveBorder
MebtnSubmitCursor = SystemWindowsFormsCursorsHand
MebtnSubmitForeColor = SystemDrawingSystemColorsInfoText
MebtnSubmitLocation = New SystemDrawingPoint( )
MebtnSubmitName = btnSubmit
MebtnSubmitTabIndex =
MebtnSubmitText = 登錄

btnExit

MebtnExitBackColor = SystemDrawingSystemColorsActiveBorder
MebtnExitCursor = SystemWindowsFormsCursorsHand
MebtnExitLocation = New SystemDrawingPoint( )
MebtnExitName = btnExit
MebtnExitTabIndex =
MebtnExitText = 退出

Label

MeLabelFont = New SystemDrawingFont(Impact ! SystemDrawingFontStyleRegular SystemDrawingGraphicsUnitPoint CType( Byte))
MeLabelLocation = New SystemDrawingPoint( )
MeLabelName = Label
MeLabelSize = New SystemDrawingSize( )
MeLabelTabIndex =
MeLabelText = TEST

Label

MeLabelBackColor = SystemDrawingSystemColorsControlText
MeLabelForeColor = SystemDrawingSystemColorsHighlightText
MeLabelLocation = New SystemDrawingPoint( )
MeLabelName = Label
MeLabelSize = New SystemDrawingSize( )
MeLabelTabIndex =
MeLabelText = 系統登錄
MeLabelTextAlign = SystemDrawingContentAlignmentMiddleCenter

btnCancel

MebtnCancelBackColor = SystemDrawingSystemColorsActiveBorder
MebtnCancelCursor = SystemWindowsFormsCursorsHand
MebtnCancelLocation = New SystemDrawingPoint( )
MebtnCancelName = btnCancel
MebtnCancelTabIndex =
MebtnCancelText = 取消

Label

MeLabelForeColor = SystemDrawingColorRed
MeLabelLocation = New SystemDrawingPoint( )
MeLabelName = Label
MeLabelSize = New SystemDrawingSize( )
MeLabelTabIndex =
MeLabelText = 為保證系統安全請先登錄

loginForm

MeAutoScaleBaseSize = New SystemDrawingSize( )
MeBackColor = SystemDrawingSystemColorsActiveBorder
MeClientSize = New SystemDrawingSize( )
MeControlsAdd(MeLabel)
MeControlsAdd(MebtnCancel)
MeControlsAdd(MeLabel)
MeControlsAdd(MeLabel)
MeControlsAdd(MebtnExit)
MeControlsAdd(MetxtUserPwd)
MeControlsAdd(MetxtUserName)
MeControlsAdd(MebtnSubmit)
MeControlsAdd(MelblUserName)
MeControlsAdd(MelblUserPwd)
MeFormBorderStyle = SystemWindowsFormsFormBorderStyleNone
MeIcon = CType(resourcesGetObject($thisIcon) SystemDrawingIcon)
MeMaximizeBox = False
MeMinimizeBox = False
MeName = loginForm
MeSizeGripStyle = SystemWindowsFormsSizeGripStyleHide
MeStartPosition = SystemWindowsFormsFormStartPositionCenterScreen
MeTag = 登錄
MeText = TEST系統登錄
MeResumeLayout(False)
End Sub

#End Region

Private Sub loginForm_Load()Sub loginForm_Load(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles MyBaseLoad
txtUserNameFocus()
btnCancelVisible = False
End Sub

Private Sub btnExit_Click()Sub btnExit_Click(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles btnExitClick
ApplicationExit()
End Sub

Private Sub btnSubmit_Click()Sub btnSubmit_Click(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles btnSubmitClick

調用loginValidator組件

Dim validator As New Validator
validatorvUsername = txtUserNameTextTrim
validatorvUserpwd = txtUserPwdTextTrim
If (validatorvalidate() = True) Then
MeClose()
Else
MessageBoxShow(帳號或密碼錯誤請重試! 驗證錯誤 MessageBoxButtonsOK MessageBoxIconWarning)
End If
End Sub

Private Sub btnCancel_Click()Sub btnCancel_Click(ByVal sender As SystemObject ByVal e As SystemEventArgs)
MeDispose()
End Sub

Private Sub btnCancel_Click_()Sub btnCancel_Click_(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles btnCancelClick
MeClose()
End Sub
End Class

  關於在NET中使用COM組件NET可以向後兼容並支持了COM和ActiveX對象等早期版本的應用程序NET中使用COM等很有趣NET創建一個包將它們包裝起來而它們之間的交互就是通過這個包裝來進行的這個包就叫做運行時可呼叫包裝(RCW)可以直接使用COM也可以先轉換成NET程序集後再使用

直接使用COM組件是通過Add Reference來實現的這種方法通過RCW來包裝缺點無法放到 GAC中不能重用

通過轉換成NET程序集來使用是通過利用 tlbimp 命令行工具來實現的語法 tlbimp COMNamedll /out:Compdll /namespace:myNS /asmVersion: myVersion /reference:refname

  第一個為COM的名稱第二個為要生成的NET組件名稱第三個為要生成的名稱空間第四個為 版本號第四個指定引用的文件名
From:http://tw.wingwit.com/Article/program/net/201311/12392.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.