VB.NET Confidential Manual (Repost)

zhaozj2021-02-16  176

Visual? Basic.net (VB.NET) is the next generation of Visual? Basic, VB.NET will provide first-class, object-oriented programming language, which brings many new features, such as inheritance, overloading, and Parametric constructor. In addition, with the adjustment of language structure, such as structured abnormalities, the code is easier to maintain, which makes the developer will be able to write high-scalability using concise free threads. VB will provide all the language features needed by developers to enable them to establish a stable and scalable distributed web application. At the same time, as the world's most popular development software, it has maintained the characteristics of easy play.

Whether you are a novice, or an old VB programmer, VB.NET will make you prepare the cutting-edge software for the Tomorrow's Internet.

First, VB.NET New Features (1) New features related to object-oriented programming

(1) Inheritance: The programmer reuses code and data by inheritance. Reusing testing code will greatly reduce the possible possible BUG.

(2) Overload: By using different parameter types, the programmer can make the programmer use the same name and functionality different from the same function.

(3) Parameterization Construction Function: While establishing a new instance, pass the parameters to it and simplify the client code. ? (2) Other new features

(1) Free thread: Free thread provides an asynchronous processing mechanism, programmers can create some long tasks without affecting other applications, complex query requests or multiple synchronization calculations.

(2) Structured exception handling: Use the "Try ... catch ... finally" statement, programmers can use the exception to embed the program without using the "ON? ERROR? GOTO statement.

(3) Detection: VB.NET generates compilation errors for any type conversion that is likely to generate runtime, to improve the security of type conversion.

(4) Shared members: refers to data and function members that can be shared by all instances that can be classified.

(5) Initialization function: VB.NET supports direct initialization of variable definition lines.

At the same time, VB.NET also has garbage collection functions; create a compiled server-side code; Web service policy; thoroughly supports new features such as XML format through ADO.NET.

Second, VB.NET's Integrated Development Environment (IDE) Visual? Studio.NET language uses the same IDE, and the new tools provided in IDE are very comprehensive and powerful. Here, you will introduce:

(1) Visual? Studio main window

This is the first interface displayed when the VB.NET is started. The main window shows the recent modified projects, and provides an option to open an existing project to create a new project. All options are displayed in hyperlink.

(2) New Project (New? Project) dialog

Provide options for creating projects in any language. Left and right columns: Several programming languages ​​are listed in the tree list on the left, the VB nodes in the list are open; the right icon displays several options for creating new projects. Note that some new options appear, such as web? Service, web? Control? Library, and class? Library. The last option is displayed: In VB.NET, multiple associate classes can be merged into a single module file.

(3) When new or open project, the content displayed in the IDE

1. Hidden toolbar

The toolbox is hidden by default, click its title to "pull it". For VB programmers, the biggest change here is that the controls in the toolbox are now changed to vertical, and each icon has a text description, not only one icon like VB? 6. 2. Cardian window (tabbed? Child? Window)

Visual? Studio.net replaces the original MDI sub-window layout using the card sub-window layout above the screen. The card sub-window includes: Home? Page, Form Layout Window, Code Window, and Help Screen.

3. Task list (task? List)

Task list is an important improvement that helps developers track the tasks to be completed. Code generator, the compiler will automatically add some content in the task list. Let's manually add a task item:

If you find a change in your code, insert a row, you will automatically appear in the list of tasks in a note in "TODO:". Click on a task in "task? List", the location of the task will appear in the code editor. This rapid definition task, the mechanism for returning tasks will greatly improve the development efficiency.

4. Solution Explorer (Solution? Explore)

It replaces the original engineering resource manager (Project? Explorer). The Program Explorer shows the various resources to be used, including the code modules written in other languages ​​and various non-coded resources. Where "References" is a new resource form in the Program Explorer, "References" manages VB.NET namespace, similar to component reference in VB? 6.

5. Properties (Properties)

The attribute window is similar to VB? 6, but it supports more layout mode (display mode). The example of the "Anchor" property of the sample control below: Usage of the "Dock" property:

Most VB.NET controls contain "Anchor" and "Dock" properties. The "Anchor" property can enhance one or more edges of the control to keep the fixed distance from the edge of the form. Create an application below, add a text box at the bottom of the form, and can telescope with the size of the form. Proceed as follows:

(1) Add a text box to the form.

(2) Click the "TextBox1" control to view its "Anchor" property. By default, "Anchors" is in the upper left corner, that is, it keeps a fixed distance from the top and left edges of the form.

(3) Modify "Anchor" attribute: Do not select "TOP" (top), select "Left" (left, "right" and "bottom". The attribute text is modified to "BottomLight". This means that the control will keep a fixed distance at the bottom, left and right side and the form.

(4) Press F5 to run the program and adjust the size of the form and enjoy your results.

"Dock" attribute is also very important, it is similar to the "Align" property of some controls in VB? 6. It enforces the specific control to secure the side of the form or use the "Fill" option to overwrite the entire form.

6. Form layout toolbar (Form? Layout)

In VB? 6, the task of arranging the control on the form is done by a toolbar called "Form® Editor". VB.NET's new toolbar provides more features and opens in the WinForms type project.

7. Code editor

In this window, click the " " or "-" number on the left side of the code line, you can extend or fold the code block to hide the code that is not related to the current task. 8. VB.NET form engine

VB.NET uses a Windows Forms engine that uses all CLR-based languages. It has many outstanding advantages. For example: Windows? Forms supports creation of a form that automatically changing component size, allows controls to anchor the control to a specific location of the form, create a transparent form, and the like. The following example creates a Windows? Forms, steps below:

(1) Start VB.NET. Click "NEW? Project" on the start page.

(2) Under "Visual? Basic? Projects, select" Windows? Application ".

(3) Modify the text and location of the name if needed. Click the "OK" button and you will see a Windows? Form.

All VB code, whether the form is still a class module, is stored as a .vb file. The code in the file describes what it is and what it does.

Click Form1 to browse the property window: Here is some changes with VB? 6, the title property is now called text (text), the font attribute is divided into name and font size, etc., and a "ismdicontAiner" property is used to establish an MDI application . Note: ". Caption" property disappears in VB.NET, they are all standardized to ".text", and ".tag" attribute disappeared.

9. Common control in the toolbox

VB.NET bundles commonly used controls: Button (button) (corresponding to the previous commandbutton) ,? Radiobutton (corresponding to previous optionButton)? (Corresponding to previous frame). Of course, is there? TextBox, Checkbox, PictureBox, ListBox, and ComboBox (comboBOBOX).

Note: The Shape control in VB.NET is not, add graphics to the form, will use the "System.drawing.Graphics" namespace, which is "using code drawing".

10. New control

VB.NET provides some controls that have never seen. Let's take a look at the most important one.

(1) MainMenu Control: This is the previous menu® Builder (menu generator). The following example how to create a new menu using "MainMenu":

1 Add in the form? MainMenu control.

2 In the form, click Show the following: Type? Here (type here); type: & file.

3 Now add some children to the new file menu; click in the "Type? Here" box, expand it to the "file" right side, add some menus.

4 Press F5, run the application, test the new menu. You can then add the code to these menu items like VB? 6.

Note: When adding an interval to the menu, you can type a single-snap number.

(2) LinkLabel Control: Implement hyperlink. Add a "LinkLabel" control to the form, press? F5? Run and test the new "LinkLabel". Run results look like a hyperlink for a web page. By default it is a loop with underscore, when the mouse will become a hand shape, and when it is clicked, the color will change. But in the default, it is not linked to a site. When you click on it, the code of the Click event will run.

Note: You can get more effects of "LinkLabel" through the "LinkBehavior" property setting option.

(3) ErrorProvider Control: If a confirmation error has occurred, it automatically uses an icon to highlight the control.

(4) Other new controls: VB.NET also adds the Tooltip control, Windows? Opening and saving dialog box now used as the "Built-in" control. The TRAYICON control makes it simplistic to add a custom icon and menu to the system tray. The Helpprovider control makes you easy access to help files.

Third, VB.NET basic syntax (1) variables and constants

VB.NET uses variables to store values, and variables have names and data types. An array is used to store related variable sets that established indexes.

1. Data types of variables (as shown in the following table)

(Figure)

In order to match the data type of the VB and other .NET language, Microsoft modifies the representation of integer class data types and joins a new data type. These changes are especially important for the method of performing external calls (such as API calls). For example, if the called function requires a 32-bit integer parameter, it should be declared as long in VB? 6, which should be declared as Integer. As shown in the following table.

(Figure)

2. Named of variable

Nameout of variables:

The beginning of the letter must be started; the embedded statement or embedded type declaration character; must not exceed 255 characters; must be unique in the same range.

3. Variable statement

VB.NET has changed more than VB? 6 declaration, and the general format is:

DIM? VARIABLENAME? AS? TYPE

Such as: DIM? STRS1? AS? STRING

DIM? INTX? AS? INTEGER

The new statement is shown in the following table.

(Figure)

4. The role of keywords in variable definitions (shown in the following table)

(Figure)

(2) constant

The constant is the same as the amount of the program run, and there are two categories: one is the constant of the user declaration; the other is the constant provided by the system.

1. User declaration constant

Format: const? Constant name? [As? Type] = expression, such as:

Const? Pi = 301415926? 'Declaration constant pi, represents 3.14159, single precision type

2. Constant provided by the system

The constant provided by the system is located in the object library. Using constants, make the program easier to read and prepare, or keep the program in different versions. Compatibility.

(3) operator

Operators in VB.NET can be divided into arithmetic operators, string operators, relational operators, and logical operators.

1. Arithmetic operator

VB.NET includes ^ (multiplication), * (multiplying), / (divided), mod (molding), (plus),-(reduction) and other calculations symbol.

New operator: VB.NET brings some new mathematical operators to reduce the code. Such as "X? =? 4" in VB.NET is the same as "x? =? X? ? 4" in VB? 6. Other, "x - =? 10,? X? * =? 2", "x? / =? 13", "x? \ = 13", "x? ^ =? 3", "X? & =? "OK" "is legal. 2. String operator

There are two string operators: "&" and " ", they all spliced ​​two strings. Note difference:

"&": The operand on both sides of the connection is a character type or a numerical type. Before the connection operation is performed, the system will convert the operand to a word symbol, and then connect it.

" ": The number of operands is characterized by the connectors. If the numerical type is arithmetic, if one is a character type, the other is a numeric model error.

E.g:

"AbcDef" "GH"? '? The result is "abcdefgh"

"AbcDef"? ? 12345? '? Result is "abcdef12345"

3. Relational operator

The relationship operator is a binocular operator, and the role is to compare the two operands. If the relationship is established, return true, otherwise returns false. In VB.NET, TRUE uses 1, FALSE is represented by 0. The number of operands can be numeric, characters. VB.NET includes = (equal),> (greater than),> = (greater than or equal), <(less than), <= (less than or equal), <> (not equal), LIKE (string matching), IS (Object reference comparison and other relationship operators).

Pay attention to the following rules when compared:

If the two operands are numeric patterns, they are compared by their size; if the two operands are character patterns, they are compared from left to right from left to right. The priority of the relationship operator is the same.

4. Logical Operators

The logical operator except that Not NOT is an unique operator, the rest is a binocular operator. The role is to logical operations, the result is the logic value true or false. As shown in the following table:

(Figure)

The logical operator has the following effects on the value:

AND operations are often used to block certain bits; OR operations are often used to put certain locations 1; for a number of XOR operations, they can restore the original value.

(4) Expression

1. Expression composition

Composed of variables, constants, operators, and parentheses, with a certain rule, the expression has a result after the operation, and the type of calculation results are determined by the data and operators.

2. Expression writing rules

Whether the multiplication cannot be omitted; brackets must be paired, all use parentheses; expressions from left to right on the same reference, no high, size;

3. priority

Arithmetic operators, logical operators have different priorities, and the relationship operator has the same priority. Different types of operator priorities are as follows:

Arithmetic Operators> Relationship Operators> Logic Operators

Note: For a variety of operators coexisting expressions, parentheses can be increased, changing priority or makes the expression clearer.

(5) VB.NET new grammar

VB.NET language has many changes in grammar. In addition to listed above, there are as follows. The form is not fully listed all changes, but lists some of the most important changes.

1. Syntax comparison

The following table compares some of the familiar grammar forms in VB? 6 and its syntax form that is closest to the NET in VB.NET.

(Figure)

2. Replaced keyword

In VB.NET, Microsoft reduces many keywords originally used for VB? 6, and in order to "frame". This replacement is for this replace because the features in the framework class are valid for all .NET languages. The following table lists some of the affected keywords. (Figure)

3. New declaration method

In VB.NET, variables, arrays, and UDT declarations and initialization methods have changed. The following table summarizes the changes in VB.NET in this regard.

(Figure)

(1) The array in VB.NET is based on 0: that is, they always start from 0, similar to "DIM? MyArray (10)? As? INTEGER", the result is a result of 11 elements INTEGER array: from 0 to 10.

(2) Changes in UDT (user-defined type): Customized types are very useful. But when you declare a custom type in the future, you need to use similar to the API keyword structure without using Type. Here's how to declare an UDT example:

STRUCTURE? MyStructure

PUBLIC? Name? As? String

PUBLIC? AGE? AS? BYTE

END? Structure

4. New keywords for vb.net

A large number of new keywords realize new features in VB.NET. The following table is some of the most important keywords and their uses, and the usage is simple.

(Figure)

5. Property declaration

VB.NET's attribute declaration is different from VB? 6, no longer uses separate "lets" and "gets". Below is an example of a new attribute program, please note that "value" is now a keyword, always contains a value passed to this property:

PUBLIC? PROPERTY? MACHINEPART ()? AS? STRING

Set

MSTRNAME? =? Value

END? SET

Get

MachinePart? =? MSTRNAME

END? Get

END? Property

6. special attention items

(1) Changes in the default mode of data transmission data to process parameters: The original transfer reference (byref) has become a pass value (byval), which is a very important change. By citing the transmission parameter than passing the parameters through the value of passing parameters: The called process may not intend to change the value of the parameters. VB.NET is still allowed to deliver parameters in a reference method, but the change in the default parameter transfer means that the program must be adjusted accordingly.

(2) There is no longer a set statement in VB.NET: In VB.NET, the object reference to the variable is only one equal sign, and the object can be treated as any other value. Although SET simplifies code, there is also an incidental impact: The default attribute is no longer valid. For example, the following attribute value reference method is no longer legal:

TEXT1? =? "This is the default attribute value of the object."

Instead, the attribute value must be explicitly referred to as follows:

TEXT1.TEXT? =? "This is the default attribute value of the object."

VB.NET makes this requirement that it is necessary for getting rid of the default properties. For example, suppose there is an object variable name "objfoo". Since there is no set statement, if the attribute value is still quoted like it, what is the meaning of this statement in the end: It is difficult to determine:

Objfoo? =? text1

This statement is to set a reference to "text1" or assign "text1" "text" attribute to "objfoo"? We can't make judgments, the compiler also cannot be judged. Therefore, abandoning the SET statement means that the default attribute value must be abandoned.

(3) VB.NET introduces an Option? Strict keyword: "Option? Strict" keyword is used to replace "Option? Explicit". The original VB allows a numeric value to a string variable, or other similar abnormal assignment operations, "Option? Strict" ends this situation. Declaration "OPTION? Strict" tells VB.NET not to make any mandatory type conversion. Using "Option? Strict" to help developers reduce many errors, including many difficulty errors. (4) Short cycle: VB.NET has a short cycle "if ... the" declaration. If your "if ... then" has two parts, the first part returns "false", and VB.NET will no longer pay attention to the second part.

(5) Return Keyword: Inside a function, you do not set a function to the return value or object, but simply declare "Return? MyData", which allows you to change the function name without modifying the actual code.

(6) Note: Most VB? 6 features can be used in VB.NET. Just "Microsoft.visualBasic.comPatibility.vb6" namespace reference is available. It is recommended that you use the latest.

Fourth, Programming Example 1: Create a form and add two controls: Button control and TextBox control, and create two event processing: button1_click? And? Button1_gotfocus.

(1) Description: The Visual? Studio.NET base provides a large number of controls, the hierarchy of the Button control is as follows:

Object

MarshalByrefObject

Component

CONTROL

Buttonbase

Button

All controls are from the Control class.

(2) Adding a control to a form requires three steps:

Announce control variables; instantiate controls; add controls to forms.

(3) The code is as follows:

IMPORTS? System.Windows.Forms

PUBLIC? CLASS? FORM1

Inherits? Form

'? Use the keyword WitHevents to the Button1 control when the variable is announced.

PRIVATE? WITHEVENTS? Button1? As? Button

PRIVATE? TEXTBOX1? AS? TEXTBOX

Public? Sub? New ()

InitializeComponent ()

END? SUB

PRIVATE? SUB? INITIALIZEC MOMPONENT ()

Me.Text? =? "Exvelope? Form"

Me.Width? =? 400

Me.height? =? 300

Button1? =? New? Button ()

TextBox1? =? New? Textbox ()

Button1.Left? =? 200

Button1.top?=?200

Button1.width? =? 100

Button1.height? =? 40

Button1.tabindex? =? 0

Button1.text? =? "Click? Me"

TEXTBOX1.LEFT? =? 200

Textbox1.top?=?30

TEXTBOX1.WIDTH? =? 150

TEXTBOX1.HEIGHT? =? 40

'Increases controls into your form.

Me.Controls.add (Button1)

Me.Controls.add (TextBox1)

END? SUBPRIVATE? SUB? Button1_click (BYVAL? Sender? As? Object,? _

BYVAL? E? As? System.eventargs? Handles? Button1.click

TEXTBOX1.TEXT? =? "You click Button1."

END? SUB

PRIVATE? SUB? Button1_gotfocus (Byval? Sender? As? Object,? _

BYVAL? E? As? System.eventargs? Handles? Button1.gotfocus

TEXTBOX1.TEXT? =? "? Button1 won the focus."

END? SUB

END? CLASS

Example 2: Create a company's Employee form, two tags, two text boxes, two buttons in the Employee form. Then create a form for the company's contractor, the contractor has first? Name, Last? Name, there is an outer company domain. Create an extension form Contractor for an Employee form. Note that two controls have been added and the location of the OK button is changed.

(1) Description: When a new form is created, the System.WinForms.Form® class is expanded, and VB.NET supports inheritance. When you need to create a form similar to the previous form, you can implement the previous form. This can save a lot of time. As shown below: Table Single NewForm inherits the WindowsApplication1.existingform class.

PUBLIC? CLAS? NewForm

Inherits? WindowsApplication1.existingform

(2) The results of the Employee form are shown in the following figure: (Figure)

(3) The code of the Employee form:

IMPORTS? System.drawing

IMPORTS? System.Windows.Forms

IMPORTS? System.comPonentModel

PUBLIC? CLAS? FORM2

Inherits? Form

Public? Sub? New ()

Mybase.new ()

'Initialization module

InitializeComponent ()

END? SUB

#REGION? "? Windows? Form? Designer? Generated? Code?"

'statement

PRIVATE? Components? As? System.componentmodel.container

PROTECTED? CANCEL? AS? System.Windows.Forms.Button

PRIVATE? LastName? As? System.windows.Forms.TextBox

PRIVATE? FIRSTNAME? AS? System.Windows.Forms.TextBox

PRIVATE? LABEL2? AS? System.Windows.Forms.Label

PRIVATE? LABEL1? AS? System.Windows.Forms.Label

PROTECTED? OK? AS? System.Windows.Forms.Button

DIM? FORM2? AS? System.Windows.Forms.form

PRIVATE? SUB? INITIALIZEC MOMPONENT ()

Me.label1? =? New? System.windows.forms.label ()

Me.firstname? =? New? System.windows.Forms.TextBox ()

Me.ok? =? New? System.windows.Forms.Button ()

Me.Label2? =? New? System.windows.forms.label () me.lastname? =? New? System.windows.Forms.TextBox ()

Me.cancel? =? New? System.windows.Forms.Button ()

Me.suspendlayout ()

'Set the properties of label Label1

Me.label1.location? =? New? System.drawing.point (8,? 24)

Me.Label1.name?=?"label1 "

Me.Label1.tabindex? =? 1

Me.Label1.Text? =? "First? Name"

'Text Box Firstname

Me.firstname.location? =? New? System.drawing.point (120,? 16)

Me.firstname.name?=?"firstname "

Me.firstname.size? =? New? System.drawing.size (136,? 20)

Me.firstname.tabindex? =? 3

Me.firstname.text? =? ""

'OK button

Me.ok.location? =? New? System.drawing.point (8,? 128)

Me.ok.name?=?"om "

ME.ok.size? =? New? System.drawing.size (112,? 32)

Me.ok.tabindex? =? 0

Me.ok.text? =? "Ok"

'Label Label2

Me.Label2.Location? =? New? System.drawing.point (8,? 48)

Me.Label2.name?=?"label2 "

Me.label2.size? =? New? System.drawing.size (88,? 16)

Me.Label2.tabindex? =? 2

Me.Label2.text? =? "" Last? Name "

'Text Box Lastname

Me.lastname.location? =? New? System.drawing.point (120,? 40)

Me.lastname.name?=?"lastname "

Me.lastname.size? =? New? System.drawing.size (136,? 20)

Me.lastname.tabindex? =? 4

Me.lastname.text? =? ""

'CANCEL button

Me.cancel.location? =? New? System.drawing.point (144,? 128)

Me.cancel.name?=?"cancel "

Me.cancel.size? =? New? System.drawing.size (112,? 32)

Me.cancel.tabindex? =? 5

Me.cancel.text? =? "CANCEL"

'Form FORM2

ME.AUTOSCALEBASSESIZE? =? New? System.drawing.size (5,? 13)

Me.ClientSize? =? New? System.drawing.size (272,? 181)

Me.Controls.addrange (new? System.windows.forms.control

{Me.cancel,? Me.lastname,? Me.firstname,? Me.label2,? Me.label1, ?? me.ok})

Me.Name?=?"form2 "

Me.Text? =? "Employee? Form"

Me.ResumeLayout (false)

END? SUB

#End? Region

END? CLASS

Note: Except for OK and Cancel buttons, all other controls are announced as private variables, OK and CANCEL controls are announced as a Protect type, which can access the protect type control by inherited classes.

(4) The code for the Contractor form is as follows:

It inherits the Employee form and makes modifications: two controls are added and change the location of the OK button.

IMPORTS? System.Windows.Forms

PUBLIC? CLASS? FORM1

Inherits? WindowsApplication1.form2

PRIVATE? LABEL3? AS? Label

PRIVATE? EXTERNALCOMPANY? AS? TEXTBOX

Public? Sub? New ()

InitializeComponent ()

END? SUB

PRIVATE? SUB? INITIALIZEC MOMPONENT ()

'Add two new controls Label3 and text box Externalcompany

Label3? =? New? Label ()

EXTERNALCOMPANY? =? New? Textbox ()

Label3.Location? =? New? System.drawing.point (8,? 72)

Label3.size? =? New? System.drawing.size (88,? 16)

Label3.text? =? "EXT? Company"

Externalcompany.location? =? New? System.drawing.point (120,? 62)

EXTERNALCOMPANY.SIZE? =? New? System.drawing.size (136,? 20)

'? Change the location of the OK button

Ok.location? =? new? system.drawing.point (8,? 140)

Me.Text? =? "Contractor? Form"

Me.Controls.add (label3)

Me.Controls.add (Externalcompany)

END? SUB

END? CLASS

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

New Post(0)