VB.NET中的组件开发
展开全部
简介 组件其实是一段可以重用的代码 通过遵循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 (ponents Is Nothing) Then ponents Dispose() End If End If MyBase Dispose(disposing) End Sub Windows 窗体设计器所必需的 Private ponents 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中使用组件 NET可以向后兼容 并支持了和ActiveX对象等早期版本的应用程序 在 NET中使用等很有趣 NET创建一个包将它们包装起来 而它们之间的交互就是通过这个包装来进行的 这个包就叫做运行时可呼叫包装(RCW) 可以直接使用 也可以先转换成 NET程序集后再使用 直接使用组件是通过Add Reference来实现的 这种方法通过RCW来包装 缺点 无法放到 GAC中 不能重用 通过转换成 NET程序集来使用 是通过利用 tlbimp 命令行工具来实现的 语法 tlbimp Name dll /out:Comp dll /namespace:myNS /a *** Version: myVersion /reference:refname 第一个为的名称 第二个为要生成的 NET组件名称 第三个为要生成的名称空间 第四个为 版本号 第四个指定引用的文件名 lishixinzhi/Article/program/net/201311/12392
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询