简介组件其实是一段可以重用的代码
通过遵循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组件名称第三个为要生成的名称空间第四个为 版本号第四个指定引用的文件名