Component development in VB.NET

xiaoxiao2021-03-06  113

'

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

'

??????????????????????????????????????????? ???????????? '

'

?????????????? 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? Usrname? As? String ???? private? Userpwd? As? String ???? public? Proty? VUSERNAME () Property? Vusername ()? As? String ???????? Get ???????????? Return? UserName ???????? End? Get ???????? set (BYVAL? VALUE? AS? STRING) ???????????? username? =? Value ???????? End? Set ???? End? Proty ???? public? Property? Vuserpwd () Property? Vuserpwd ()? As? String ???????? Get ???????????? Return? Userpwd ???????? End? Get? ??????? set (byval? Value? As? String) ???????????? userpwd? =? Value ???????? End? Set ???? ? Property ???? 'converted to MD5 ???????????? Function? ConvertMD5 () Function? ConvertMD5 (ByVal? PWD? String)? As? String ???????? DIM? MD5? AS NEW? MD5CRYPTOSERVICEPROVIDER ????????? DIM? Password? As? Byte ()? =? (New? Asciiencoding) .getbytes (PWD) ???????? 'converted 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? ??? Publi C? function? Validate () function? Validate ()? as? boolean ???????? 'connected to the user table ???????? DIM? MyConnection? AS? new? sqlConnection ("Server = Localhost; Database = Test; trusted_connection = yes; user? id = sa; password =; ") ???????? DIM? SELECTAPTER? AS? new? SqlDataAdapter (" SELECT? *? from? Users? Where? Username = '" ? Usrname? ?"' "? ?" And? Password = '? ? ConvertMD5 (userpwd)? ? "'" ,? myconnection) ???????? DIM ? ds? as? new? DataT ???????? Try ??????????? SELECTAPTER.FILL (DS,? "USERS") ?????????? ?? IF? (Ds.tables (0) .rows.count?>

? 0)? Then ???????????????? Return? True ???????????? Else ???????????? ??? Return? False ?????????????????????????????????????????????????? msgbox ( "Connecting the database error") ???????? carat? Pp? As? EXCEPTION ???????????? msgbox ("OH, an unpredictable thing happened to you, you die Ideated. Exit it. ") ???????? End? TRY ???? End? Function # region?" • Component Designer generated code? "???? public? Sub? New () Sub? New (byval? Container? System.componentmodel.icontainer) ???????? myclass.new () ???????? 'windows.forms? Class composing designer support ???????? container.add (me) ???? end? Sub ???? public? Sub? New () sub? New () ???????? mybase.new () ???????? 'This call is necessary for the component designer. ???????? INITIALIZECMOMPONENT () ???????? 'is in? INITIALIZECMOMPONENT ()? Add any initialization after calling ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? Clean up the list of components. ???? protected? OVERLOADS? OVERRIDES? SUB? DISPOSE () SUB? Dispose (BYVAL? Disposing? AS? Boolean) ???????? f? Disposing? THEN ?????????? ?? if? Not? (Components? Is? Nothing)? THEN ???????????????????????????? end? IF ???????? end? If ???????? mybase.dispose (Disposing) ???? end? Sub ???? 'component designer ???? private? Components? as? system.componentmodel.icontainer ???? 'Note: • The following process is necessary for the component designer to modify this process using the component designer. ???? 'Don't modify it using the code editor. ???? ? Private () sub? InitializationComponent () ???????? Components? =? New? System.componentmodel.container ???? End • Sub # END? RegionEndend? Class? Introduction: Components are actually a reusable code that implements a component by following the IComponent interface standard, so component is 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, you can put the components in your own program and don't worry that the components will generate how much errors 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.

?? Reference Components: Just 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 necessary for the Windows? Form Designer. ???????? INITIALIZECMPONENT () ???????? 'is in? INITIALIZECMOMPONENT ()? Add any initialization after calling ??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? Clean the list of components. ???? protected? OVERLOADS? OVERRIDES? SUB? DISPOSE () SUB? Dispose (BYVAL? Disposing? AS? Boolean) ???????? f? Disposing? THEN ?????????? ?? if? Not? (Components? Is? Nothing)? THEN ???????????????????????????? end? IF ???????? End? IF ???????? mybase.dispose (Disposing) ???? end? Sub ???? 'windows? The form designer is necessary??? ? Private? Components? As? System.componentmodel.icontager ???? 'Note: • The following process is the Windows? The Form Designer is necessary ????' Can use? Windows? Form Designer Modify this process? . ???? 'Don't modify it using the code editor.

???? friend? Withevents? System.windows.Forms.Label ???? friend? Withevents? Lblusername? As? System.windows.forms.Label ???? friend? Withevents? Txtusername? As? TxtuserName? System.Windows.Forms.TextBox ???? Friend? WitHevent? TXTUSERPWD? AS? System.Windows.Forms.TextBox ???? friend? Withevents? Btnsubmit? As? System.windows.Forms.Button ???? Friend ? WitHevent? Btnexit? As? System.windows.Forms.Button ???? Friend? Withevents? Label1? As? System.windows.Forms.Label ???? Friend? WitHevents? Label2? System.windows.Forms .Label ???? friend? Withevents? Btncel? As? System.windows.Forms.Button ???? Friend? Withevents? Label3? As? System.windows.Forms.Label ???? ? Private? SUB? INITIALIZECOMPONENT () SUB? INITIALIZECMOMPONENT () ???????? 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.btncel? =? 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.fixedsingser ???????? 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.borders Tyle? =? 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?=?? ?? ?????? 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" ????????meme .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.btncel.backcolor? =

? System.drawing.systemcolors.activeborder ???????? me.btncel.cursor? =? System.windows.forms.cursors.hand ???????? me.btncancel.location? =? New ? System.drawing.point (225,? 215) ???????? me.btncancel.name?=?"btncel" ????????me.btncancel.tabindex?=?10 ?? ?????? me.btncancel.text? =? "" Cancel "???????? '?????????? ????? ??? me.label3.forcolor? =? 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.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.formorderstyle? =? System.windows.forms.formorderstyle.none? ??????? me.icon? =? Ctype (Resources.GetObject ("$ this.icon") ,? system.drawing.icon) ???????? me.maximizebox? =? False? ??????? me.minimizebox? =? False ???????? me.name?=?"loginform" ?????????me.sizegripstyle?=?thystem.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.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 ???????? 'calling LoginValidator Components ???????? DIM Validator? as? new? validator ???????? Validator.vuserName? =? txtusername.text.trim ???????? validator.vuserpwd? =? txtuserpwd.text.trim ????? ???? i? (Validator.validate ()? =? True)? THEN ???????????? me.close () ???????? ELSE ????? ??????? messagebox.show ("account or password error, please try again!", "Verification error",? Messageboxbuttons.ok,? Messageboxicon.warning) ???????? End? IF ???? End? Sub ???? private? Sub? Btncel_click () sub? Btncel _Click (byval? Sender? As? System.object,? Byval? E? As? System.eventargs) ???????? me.dispose () ???? End? Sub ???? private? Sub ???? • btncel_click_1 () sub? btncel_click_1 (Byval? Sender? as? system.object,? Byval? E? as? system.EventArgs? Handles? btncel.click ???????? me.close () ?? ?? END? SUBEND? Class? About using COM components in .NET: .NET can be compatible backwards, and support early versions 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 using the COM component is implemented by add reference, which is packaged through RCW. Disadvantages: Cannot put it? In the GAC, it cannot be reused.

?? 2, by converting into a .NET program set, is it implemented by using the TLBIMP? Command line tool, syntax:

?? TLBIMP? COMNAME.dll /out:Comp.dll / namespace: myns / asmversion:? myversion / reference: refName ?? The first for com name, the second is the name of the .NET component to be generated, third For the namespace to be generated, the fourth is the version number, the fourth specified file name.

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

New Post(0)