Component development in VB.NET

xiaoxiao2021-03-06  109

First see the code of the section assembly: (temporary writing, write more chaos)

'

'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' ''

'

'

'

Login Verification Component '

'

'

'

'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' ''

Imports

System.security.cryptography.cryptography

Imports

System.text

Imports

System.data

Imports

System.data.sqlclient

Public

Class Validator

Class 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 'convert 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) 'is converted to a hash value Byte array 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 Publi c Function validate () Function validate () As Boolean 'connected to the Users table 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 SELECTAPTER.FILL (DS, "Users ") IF (ds.tables (0) .Rows.count>

0) THEN RETURN TRUE ELSE RETURN FALSE END IF CATCH EP AS SQLEXCEPTION MSGBOX ("Connection Database Error") Catch PP As Exception Msgbox ("OH, unpredictable things happened to you, you are dead. Exit." ) END TRY END FUNCTION # Region "Component Designer Generated Code" Public Sub New () Sub New (Byval Container As System.comPonentModel.icontAiner) Myclass.New () 'Windows.Forms Class Writing Designer Support for Container Support .Add (me) end sub public sub new () sub new () mybase.new () 'This call is required for the component designer. InitializeComponent () 'Add any initialization End Sub' component to clear the component list after the initializeComponent () call. 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 'essential component designer Private Components as system.componentmodel.icontainer 'Note: The following process is that the component designer must modify this process using the Component Designer. 'Don't modify it using the code editor. Private Sub InitializeComponent () Sub InitializeComponent () components = New System.ComponentModel.Container End Sub # End RegionEnd Class Description: The component is actually a block of code that can be reused, by following standard interfaces to IComponent Implement a component, so if some components are derived from the Component class, by the Component class to implement the IComponent interface. The access limits of the external to them should be controlled using the access level of the function in the component.

As long as there is enough permissions to put the components in your own procedure without worrying about the components, how much errors are generated because the components have been tested. For example, you can make a login program into a component, or make some features that are often used, so that you can reduce the errors in the development, or shorten the development time. The components can also be used to each other. If one component references another component, it is no problem, but you must first add a reference to the component in Add Reference. In .NET, it is implemented by placing the component in the program set. The file information and the path dependent on these components are stored in the program, so the CLR can determine the location of the other assembly required for the component by this information. (In addition, using interfaces should be used in the assembly design process.

Create a component in VS: Select a Project, select Class Library, OK from the template. Then, then from the Project menu, a framework of the component is presented in front of the platform, the platform automatically inherits the Component class and constructor. You can delete the Class1 that is automatically generated when you originally created the class library, see the needs of the application. Then you can write the functionality to be implemented in the component class, and finally select the Build Solution from the Build Solution menu to generate the component. If you generate success, you will see a DLL file in the bin directory of the application.

Quote Components: As long as in the Solution Explorer window, add the Reference to the DLL.

Imports

Loginvalidator

Imports

System.data

Imports

System.data.sqlclient

Public

Class loginform

Class Loginform Inherits System.Windows.Forms.Form # Region "Windows Form Designer Generated Code" Public Sub New () Sub New () MyBase.new () 'This call is required for the Windows Form Designer. InitializationComponent () 'Add any initialized End Sub' form to clean up the component list after the initializeComponent () call. 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 Form Designer Required Private Components as System.comPonentModel.icontainer 'Note: The following procedure is necessary to use the Windows Form Designer to modify this process using the Windows Form Designer. 'Don't modify it using the code editor.

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 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 =

"Password:" 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 = "account:" 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.borderst yle = 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 = "Login" '' 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 = "Exit" '' 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 = "System login" me.label2.textalign = system.drawing. ContentAlignment.middleCenter '' btncel 'me.btncel.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. Btncel.tabindex = 10 me.btncel.text = "Cancel" '' 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 = "To ensure system security, please log in first" '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.btncel) me.controls.add (me.label2) me.controls.add (me.la.A.Btnexit) Me.Controls.add (M E.TXTUSERPWD) Me.Controls.add (me.txtusername) Me.Controls.add (me.btnsubmit) me.controls.add (me.lblusername) me.Controls.add (me.lbluserpwd) me.formorderstyle = System. Windows.Forms.formBordersty.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 = "login"

Me.Text = "TEST-- System Log" 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 'call loginValidator assembly 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 ("Account or password error, please try again!" () SUB BTNCANCEL_CLICK (B yVal 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 SUBEND CLASS About Using COM Components in .NET: .NET can be compatible backwards and supports an earlier version of the COM and ActiveX objects. Use COM in .NET, use COM, and .NET creates a package to package them, and interaction between them is done through this packaging, this package is called the Packaging (RCW). You can use COM directly, or you can first convert it into the .NET assembly.

1. Directly use the COM component to be implemented by add reference, which is packaged by RCW. Disadvantages: Can't put it in the GAC, you can't be reused.

2, use by converting into .NET assembly, it is achieved by using the TLBIMP command line tool, syntax:

TLBIMP COMNAME.dll /out:Comp.dll / Namespace: myns / asmversion: Myversion / Reference: refName The first name for COM, the second is the name of the .NET component, the third is the name to be generated. Space, fourth is the version number, the fourth specified file name.

转载请注明原文地址:https://www.9cbs.com/read-126949.html

New Post(0)