Vb. Component Development in net

Source: Internet
Author: User
Tags exit md5 reference trim wrapper
Component development First look at the code of the Segment component: (Temporary, write more messy)


''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' '
' Login Verification component '
' '
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Imports System.Security.Cryptography
Imports System.Text
Imports System.Data
Imports System.Data.SqlClient

Public Class Validatorclass 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 to 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)

' Convert to 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

Public function validate () function validate () as Boolean

' Connect 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= '" + conve RtMD5 (USERPWD) + "'", MyConnection)
Dim DS as New DataSet
Try
Selectadapter.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, something unexpected is happening around you, you're dead.") Quit it. ")
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 is required to compose designer support
Container.add (Me)
End Sub

Public Sub New () Sub New ()
MyBase.New ()

' This call is required by the Component Designer.
InitializeComponent ()

' Add any initialization after the InitializeComponent () call

End Sub

' Component overrides dispose to clean up the list of components.
Protected Overloads Overrides Sub Dispose () Sub Dispose (ByVal disposing as Boolean)
If disposing Then
If not (components are nothing) Then
Components. Dispose ()
End If
End If
Mybase.dispose (disposing)
End Sub

' Required by the Component Designer
Private Components as System.ComponentModel.IContainer

' NOTE: The following procedure is required by the Component Designer
' You can use the Component Designer to modify this procedure.
' Do not modify it using the Code Editor.
<system.diagnostics.debuggerstepthrough () > Private Sub InitializeComponent () Sub InitializeComponent ()
components = New System.ComponentModel.Container
End Sub

#End Region

End Class

Description: A component is actually a reusable code that implements a component by following the standards of the IComponent interface, so that all components are derived from the component class and the IComponent interface is implemented by the component class. The access level of the function should be used correctly in the component to control external access restrictions on it.

As long as you have sufficient permissions, you can put components into your own programs without having to worry about how big an error the component will cause because the component has been tested. For example, a login program can be made into a component, or some of the features often used to make components, which can reduce development errors, but also shorten the development time. Components can also be applied to each other, if a component references another component, it is fine, but first add a reference to the component in Add Reference, which is implemented in. NET by placing components in an assembly, where the file information and the path on which these components depend are stored. The CLR can therefore use this information to determine the location of the other assemblies required by the component.

(also in the component design process should make good use of interface to design components)

Create components in VS: Select a project, then select Class Library,ok from the template. Then add Component from the Project menu, to the point where a frame of the component is present, and the platform automatically inherits Component classes and constructors. You can delete the Class1 that were automatically generated when you originally created the class library to see the application's needs. You can then write the functionality you want to implement in the component class, and finally build the component from the Build Solution menu. If the build succeeds, you will see a DLL file in the application's Bin directory.

Reference component: Just add the reference to the DLL in the Solution Explorer window.

Imports Loginvalidator
Imports System.Data
Imports System.Data.SqlClient

Public Class Loginformclass LoginForm
Inherits System.Windows.Forms.Form

#Region "code generated by the Windows forms Designer"

Public Sub New () Sub New ()
MyBase.New ()

' This call is required by the Windows Forms Designer.
InitializeComponent ()

' Add any initialization after the InitializeComponent () call

End Sub

' Form overrides dispose to clean up the list of components.
Protected Overloads Overrides Sub Dispose () Sub Dispose (ByVal disposing as Boolean)
If disposing Then
If not (components are nothing) Then
Components. Dispose ()
End If
End If
Mybase.dispose (disposing)
End Sub

' Required by the Windows Forms Designer
Private Components as System.ComponentModel.IContainer

' NOTE: The following procedure is required by the Windows Forms Designer
' You can use the Windows Forms Designer to modify this procedure.
' Do not 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
<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.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 Number:"
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.BorderStyle = 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
'
' Btncancel
'
Me.btnCancel.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.btnCancel.TabIndex = 10
Me.btnCancel.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 login 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.btncancel)
ME.CONTROLS.ADD (ME.LABEL2)
ME.CONTROLS.ADD (ME.LABEL1)
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 = "Login"
Me.Text = "test--System Login"
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.c Lick
Application.exit ()

End Sub

Private Sub btnSubmit_Click () Sub btnSubmit_Click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Btnsu Bmit. Click

' Invoke the Loginvalidator component

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!") "," Validation error ", 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_1 () Sub Btncancel_click_1 (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles b Tncancel.click
Me.close ()

End Sub
End Class

About in. NET uses COM components:. NET can be backward-compatible and supports earlier versions of applications such as COM and ActiveX objects. It's fun to use COM in. Net. NET creates a package that wraps them up, and the interaction between them is done through this wrapper, which is called the runtime-calling wrapper (RCW). You can use COM directly, or you can convert it first. NET assembly before use.

1. The direct use of COM components is implemented through Add reference, which is packaged by an RCW. Disadvantage: cannot be placed in the GAC and cannot be reused.

2, through conversion into. NET assemblies, which are implemented by using the TlbImp command-line tool, Syntax:

TlbImp comname.dll/out:comp.dll/namespace:myns/asmversion:myversion/reference:refname

The first is the name of COM, and the second is to be generated. NET component name, the third is the namespace to be generated, the fourth is the version number, and the fourth specifies the file name of the reference.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.