First, familiar with the programming environment of VB 6.0, master the use of VB common controls, try to develop simple VB applications
- See example VB 6.0 - know the programming environment of VB 6.0 -
VB6.0 uses visual programming environment, it is easy to use, after running VB6.0, there will be a window shown below, huh, it is quite complicated, a lot of buttons, menus, small windows, don't worry, stay Will we learn these stuff together for these stuff together.
First solve a small problem, how to build a VB project? Method is to select "Standard EXE" in the "New Engineering" dialog box that appears when the program starts, and click OK to create a new project directly. If you skip this dialog, you can also from the File menu. Select "New Project" to re-call out this dialog.
Let's take a look at the VB programming environment, although it looks complicated, but you can divide it into several parts, each part has its own specific function, so it will be clear. In fact, VB This is combined with similar or similar functions such as such an interface, which makes us easy to control the program when designing programs.
The uppermost layer of the form is "VB 6.0 Menu" and "Convenient Tool Button", which contains options for all VB provided, and some of these common features or operation options are extracted. "Convenient Tool button, by clicking these shortcut buttons can speed up the speed of program development, the following image shows the role of the common tool button, remembering them.
Then look at "VB Component Panel" and "Form Design Area", VB provides a convenient form design area, which is located in the middle of the entire programming window. We can build a beautiful practical program interface in this area, try to find "button commandbutton" from "VB's Component Panel", click to select, go to the "Form Design Area" form, this A button will appear on the form so that we add the button to our program's interface. Hey, I forgot to tell you where "VB's component panel" is the leftmost area containing many small things that look familiar, it is the standard programming component (control) that VB is provided to our standard (control), it Putting something like buttons, picture frames, list boxes, etc.
There are three small windows from top to bottom on the right side of the screen, respectively:
"VB Engineering Manager": Used to manage the various types of forms and modules required to develop a VB program, such as what is included in an engineering, the form is an interface that appears outside, the module is The code used inside the program.
When we click on the "Add Form" Convenient Tool button, the newly added form will be displayed in the "VB Engineering Manager" on the left, which is the "VB Engineering Manager" function, which makes us from the overall Get all parts of the program development.
"Component Properties Window": Used to display the properties of the component, and allow us to modify, set the value of these properties, we select a component on the form of the form design area, with the button as an example, then "Properties Window" It will list its properties, we can see the usual "name", "capition", "font", etc., try to change the "capition" property to "this is a button", then look "window" The text displayed on the button "button is changed.
"Form Layout Window": When the program is running, the program form is moved in the initial position in the screen, moves the mouse to the form in the left screen. At this time, the mouse will become a moving shape, drag the form, Set the position of this form when you are running.
I have said so much, I haven't said a region, that is, "code editing area", when we double-click the mouse in Form Design Area, you can see the VB program of a row is displayed. Among them, we can modify, write to program code, let the program implement a certain function, it is the key to the entire program design, if you are not very understanding about the grammar of the VB language, you can continue the next section to specifically "VB grammar" The explanation, don't worry, come slowly, soon you can get started. - Look at the simple syntax of Example VB 6.0 - VB Learning - the grammatical syntax of the VB language is basically consistent, it can be said that if you can read the QBASIC program, then the program you read is no problem, this section We are just simple to talk about VB's grammar, because we will continue to exposure to new syntax knowledge in future instances, which is also an accumulated process. If you still remember the content in the last section, we mentioned that if you double-click on a component (control) in the Form Design Area, there will be "code editing window", which is the window of the following picture, we can Enter or modify the code of the program. In this process we double-click on the control to write code, we involve a very important concept. This is one of the main differences in VB and QBasic. You know that in QBasic includes Tubro C, TubroPascal, etc. We need a little written code to implement all functions, such as the interface, input and output, keyboard control, etc., which are reflected in the program or function and subroutines, which are full-scale process. Programming language; however, the programming language such as VB, VC, Delphi provides visual programming environments, with a complete set of common components (controls) for us, these controls can be seen as objects (VB strictly said not complete Object-oriented programming language), we will deal with these objects when we write programs, not as original. Therefore, most of the code in VB is related to these, such as a piece of code shown below, it is a sub-process that we double-click on a button (Command) and automatically generated by VB, private sub command1_click () It is the beginning of the sub-process, and End Sub represents the end of the child process. Then we can add program code during the child, which is to write the response of the program when you write when you are clicked.
Let's take a look at this code.
Declaring a variable using the "DIM Variable Name AS Variable Type" method, the variable usually used in the program should have a variable declaration, as the A, B, and I in the above example are defined as integers and strings, respectively. Type variables, the variable types in VB have the following:
Data Type Description BYTE Binary Boolean Real Value Integer Integer Long Long Single Single Real Double Double Confusion Currency Currency Date Date and Time Object Object String String Variant Variable Type
The control structure and cyclic structure are commonly used in programming.
When the computer runs the program, the order in which the statement is executed is downward. Some simple programs can be written only with one-way processes, but a slightly complex program depends on control statements to control the procedures executed. There are mainly two, branch structures, and cyclic structures.
Branch structure: The branch structure is actually a choice, selecting a different block under different conditions. There are a lot of statements that achieve branch structure, the most commonly used is if ... then ... else ...... E.g:
... IF condition THEN {program 1} else {program segment two} end if ... If the condition is satisfied, the computer will execute the statement in the program segment, then skip the block two, execute the following statement. If the condition is not satisfied, the computer will skip the statement of the program segment, execute the block two, and continue to perform the following statement.
Cycle structure: The loop structure is to make the computer to perform a certain segment of a program. Use do ... loop to loop repeat the block and repeat the number of repetitions. Under the conditions of known cycles, use for ... next, you can repeatedly perform unified clause blocks.
With the above two structures, you can refer to the code shown in the figure, carefully understand so much today, we will enter the example-style control learning. - See example in an example VB6.0 - Simple text box textbox example -
Text Box Control (TextBox) is the most commonly used control in VB. It is a container that displays and inputs text, which is equivalent to a text editor, providing all basic text processing functions, such as editing of text, etc. In the following applet, we can see the most basic properties of the text box control (TextBox). In order to learn, the source code provided has made detailed Chinese comments, see the code in the source code:
Option expedition
'--------------------------------------
Example of 'TEXTBOX common attributes
'--------------------------------------
'Hong En online informational unlimited
'--------------------------------------
'------ Name --------------------------
'Cmdclear clear text box content button
'Textall text box
'Cmdok gives the formall text box of form formback
'FormTextBox main form
'--------------------------------------
'--------------------------------------
'---- Text Box Name ---- Property Settings ---
'
'TextPassword PasswordChar *
'TextResume Multiline is Ture, Scrollbars is 2 -Vertical
'Textall Multiline is Ture, Scrollbars is 2 -Vertical
'Appearance is 0 - FLAT
'--------------------------------------
Private Sub Cmdclear_Click ()
'Clear content in all text boxes
Textid.text = "" "
TextPassword.text = ""
TextName.Text = ""
Textmail.text = ""
TextResume.text = "" "
End Sub
Private sub cmdok_click ()
'Assumption of TEXT and tag of the FormTextBox text box
'Formall text box in the textall text box in Formack.Textall.Text = lblid.caption & "& textiD.text_
& vbnewline & _
LBLPassword.caption & "& textpassword.text _
& vbnewline & _
Lblname.caption & "& textname.text _
& vbnewline & _
Lblmail.caption & "& textmail.text _
& vbnewline & _
LBLRESUME.CAPTION & "_ _
& vbnewline & textResume.text p>
'Where VBNewline is displayed for Textall.Text
Load formback 'Load Formack
FORMBACK.SHOW 'Display Form FORMBACK
FormtxtBox.visible = false 'Setting FormTextbox is invisible
End Sub
The two form interfaces of this example are as follows:
For the FormtxtBox form (FORMBACK) we first create a standard EXE project. Add two forms and place the TextBox, Button, and Label controls on the form. The names of each text box are as follows:
Text Box Name Name Property Text Box Name Attribute Member Number Text Box Textid Email Text Box TextMail Password Text Box TextPassword Personal Description Text Box TextResuMe Name Text Box TextName Member Information Box Textall In the Properties window TEXTPASSWORD PASSWORDCHAR attribute set to * TextResume's multiline property is set to TURE, the scrollbars property is set to 2 -Vertical; the multiline property of the textall text box is set to TURE, the ScrollBars property is set to 2 -Vertical, and the Appearance property is set to 0 - flat. Next We add corresponding programs to the CLICK event of "OK" (CMDOK) and "CMDClear) button in the FormtxtBox form. Where: textid.text = "" is to empty the contents in the member number text box, and other statements are similar. FORMBACK.TEXTALL.TEXT = lblid.caption & "& textid.text _ & vbnewline & this statement Sets the text of the Textall text box in the Formback form. Vbnewline is a wrap display flag. "_" Is the continuation mark, and when the statement does not write, you should use this flag, but pay attention to: before you have sparks. After the code is written, we can save the project, then press the "Ctrl F5" button to test the test of our program. - See example VB6.0 - Simple list box ListBox example -
The list box (ListBox) control represents a list of options, and the user may select one or several options with the mouse. The list box features: The item in the list box is inserted by the program. The user cannot enter data in the list, and when the item is selected, the user is clicked or performs an action, by the application Complete specific operations for the specified item. The following example fully illustrates this feature of the list box and describes the common properties of the list box (Listbox). For the convenience of learning, the source code provided has made detailed Chinese comments, see the code in the source code: Option Explicit
'--------------------------------------
'Simple list box ListBox example
'--------------------------------------
'Hong En online informational unlimited
'--------------------------------------
'------ Name --------------------------
'List1, List2 list box
'LeftTorightcmd mobile selected items to another list box (left to right)
'RightToleftCMD mobile selected items to another list box (right to left)
'List1movecmd delete list box List1 selected items
'List2MoveCMD Delete List Box List2 Selected Project
'List1addcmd adds new projects to list box list1
'List2addcmd Add a new project to the list box list2
'List1clearcmd empty list box List1
'List2clearcmd empty list box List2
'--------------------------------------
'--------------------------------------
'---- Name ---- Property Settings ---
'
The 'List1 list box sorted property is set to "false"
'List2 list box sorted property is set to "True"
The 'MultiSelect property is set to 2
', The project in the List2 control is always sorted.
'--------------------------------------
Private sub flow_load ()
'When loading the form, assign the element in the list.
List1.additem "China"
List1.additem "america"
List1.additem "japan"
List2.additem "Soccer"
List2.additem "Tennis"
List2.additem "Basketball"
End Sub
Private sub leavetorightcmd_click ()
'Move the selected elements in the left to the right list
If list1.listindex> = 0 THEN
'Add new elements in the list
List2.additem list1.text
'Delete selected elements in the left list
List1.removeItem List1.listIndexend IF
End Sub
Private sub list1addcmd_click ()
Dim ListItem As String
'
ListItem = INPUTBOX ("Enter a new project in the list")
'Add new project to the list
IF Trim (ListItem) <> "" ""
'Trim () action is to clear spaces within the ListItem string
List1.additem ListItem
END IF
End Sub
Private sub list1clearcmd_click ()
'clear the list
List1.clear
End Sub
Private sub list1movecmd_click ()
DIM I as integer
'Decision list box has only one project selected
If List1.selcount = 1 THEN
List1.removeItem List1.listIndex
Elseif List1.ListCount> 1 THEN
'Delete the selected multiple items in the list box
For i = list1.listcount - 1 to 0 step -1
'Listcount returns the total number of items in the list box
'ListCount-1 is the index number of the last project in the list box
'Determine whether the project is selected, select () returns the Boolean value
If List1.Selected (i) THEN
'Delete the index number I.
List1.removeItem I
END IF
NEXT
END IF
End Sub
Private sub list2addcmd_click ()
'Define variables to receive input strings
Dim ListItem As String
ListItem = INPUTBOX ("Enter a new project in the list")
IF Trim (ListItem) <> "" ""
'Add a string to the list
List2.additem ListItem
END IF
End Sub
Private sub list2clearcmd_click ()
'clear the list
List2.clear
End Sub
PRIVATE SUB LIST2MOVECMD_CLICK ()
DIM I as integer
'Judging whether the selected element has only one
If list2.selcount = 1 THEN
List2.removeItem List2.listIndex
'If the selected element is greater than 1, follow the following operations
Elseif List2.ListCount> 1 THEN
For i = list2.listcount - 1 to 0 step -1
If List2.Selected (i) THEN
List2.removeItem I
END IF
Next I
END IF
End Sub
Private sub righttoleftcmd_click ()
DIM I as integer
'There are several elements that are determined.
If list2.selcount = 1 THEN
List1.additem list2.text
List2.removeItem List2.listIndex
'The elements of the election are greater than one
Elseif List2.selcount> 1 THEN
For i = list2.listcount - 1 to 0 step -1
'If the element is selected, you move it from the right side to the left
If List2.selected (i) Thenlist1.addItem List2.list (i)
List2.removeItem I
END IF
Next I
END IF
End Sub
This example contains a form, there are two list boxes (List1, List2) and eight buttons in the form, as follows:
Formlistbox, we first create a standard EXE project, add a form, and add the list box and button control to the form. The names of each button control are as follows:
Button Name Name Properties Button Name Name Property Delete Selected Item (Left) List1MoveCMD Remove Selected Item (Right) List2MoveCMD Add New Project (left) List1addcmd Add New Project (right) List2addcmd Clear Desk (left) List1clearCMD Clear list (right) List2clearcmd Right Arrow LeftTorightcmd Row RightToleftCMD 'List1 List Sorted Property Set to "false"' list2 list box Sorted Property Set to "True" 'multislect property Set to 2', the item in the List2 control is always sorted in the properties window , Set the List1 list box sorted property to "false"; List2 list box sorted property is set to "true", and the multislect property is set to 2, that is, the project in the List2 control is always sorted. Next we add the corresponding procedures for each control. Where: List1.addItem "China" is to add "China" to the list box. List1.listIndex returns the index number of the currently selected item in the list box List1. List1.listcount returns to the total number of items in the list box List1. List1.listcount - 1 Returns the index number of the last project in the list box List1. List1.selected () returns the selection status of a project, returns the Boolean value. After the code is written, we can save the project, then press the "Ctrl F5" button to test the test of our program. In this applet, the general common attributes and methods in the list box are included.
The CommonDialog control is a common control that provides us with several types of standard dialogs such as opening, saving, font, color, printing, help, and this example demonstrates how all these types of dialogs are used.
In order to learn, the source code provided has made detailed Chinese comments, see the code in the source code:
'-------------------------------------------
'General dialog box usage
'-------------------------------------------
'Hong En online informational unlimited
'-------------------------------------------
'------ Name -------------------------------
'CDLTEST General dialog
'Cmdopen "Open" dialog button
'Cmdsave "Save As" dialog button
'Cmdfont "font" dialog button
'Cmdcolor "color" dialog button
'Cmdprint "Print" dialog button
'Cmdhelp "Help" dialog button
'-------------------------------------------
'When the "Color" dialog button is pressed
Private subdcolor_click ()
ON Error ResMe next
Cdltest.cancelerror = true
CDLTest.Flags = cdlccrgbinit
CDLTest.ShowColor
If Err = CDLCancel Then Exit Sub
TextBoxColor.Forecolor = CDLTest.color
End Sub
'When the "Font" dialog button is pressed
Private sub cmdfont_click ()
ON Error ResMe next
'When the user presses the "Cancel" button, returns an error message so that we can control it.
Cdltest.cancelerror = true
'This sentence must be
CDLTest.Flags = CDLCFBOTH CDLCFEFFECTS
'Display "Font" dialog
CDLTest.ShowFont
'When you "cancel" error, you jump out
IF err = cdlcancel then
EXIT SUB
Else
'Set the TEXTBOX's font attribute according to the change of the Font dialog box
'If the user selects the font to change the font, avoid the font empty error
If Cdltest.FontName <> "" "
TextBoxFont.FontName = CDLTest.FontName
END IF
Textboxfont.fontsize = cdltest.fontsize
Textboxfont.fontbold = cdltest.fontbold
Textboxfont.fontital = cdltest.fontitalic
TextBoxFont.fontstrikethru = CDLTest.FontStrikethru
Textboxfont.fontunderline = cdltest.fontunderline
END IF
End Sub
'When the Help dialog button is pressed
Private subduDhelp_click ()
ON Error ResMe next
'Set the helpcommand property, display the Visual Basic Help Directory Topics
CDLTest.helpCommand = CDLHELPFORCEFILE
'Specify a help file
DIM FULLPATH AS STRING
If right (app.path, 1) = "/" t 'app.path is the root directory
Fullpath = app.path "test.hlp"
Else
Fullpath = app.path "/" "test.hlp"
END IF
'The above is a tip of the path where the application is located.
Cdltest.helpfile = FullPath
'Display Help dialog
Cdltest.showhelp
End Sub
'When the "Open" dialog button is pressed
PRIVATE SUB CMDOPEN_CLICK () 'When there is an error, jump to the next statement
ON Error ResMe next
Cdltest.cancelerror = true
'Property Dialogtitle is the title of the dialog to pop up
CDLTest.Dialogtitle = "Open File"
'The default file name is empty
CDLTest.FileName = "" "
'Attribute Filter is a filter, returns or set the filter shown in the Type Box of the dialog.
'Syntax Object.Filter [= File Type Description 1 | Filter1 | File Type Description 2 | Filter2 ...]
Cdltest.filter = "Text file (.txt) | * .txt"
The use of the 'Flags property varies according to the different dialogs, and uses the online help manual for details.
CDLTest.Flags = CDLOFNCREATEPROMPT CDLOFNHIDEREADOONLY
CDLTest.showopen
If Err = CDLCancel Then Exit Sub
TextBoxOpen.text = cdltest.filename
End Sub
'When the "Print" dialog button is pressed
Private subdprint_click ()
ON Error ResMe next
Cdltest.cancelerror = true
'Display "Print" dialog
CDLTest.Showprinter
If Err = CDLCancel Then Exit Sub
End Sub
'When the "save" dialog button is pressed
Private sub cmdsave_click ()
ON Error ResMe next
Cdltest.cancelerror = true
CDLTest.Dialogtitle = "Save File"
CDLTest.FileName = "" "
'Interpretation
CDLTEST.FILTER = "Text file (* .txt) | * .txt"
CDLTest.Flags = CDLOFNCREATEPROMPT CDLOFNHIDEREADOONLY
Cdltest.showsave
If Err = CDLCancel Then Exit Sub
TextBoxSave.text = CDLTest.FileName
End Sub
If there is no small icon in the Control Panel, you must first add from the Add Control dialog box. Let's take a look at how to call different types of dialogs. - See example of instance VB6.0 - CommMomDialog Used -
The Commondialog control has a range of Show methods, such as Showopen, Showsave, ShowFont, ShowColor, Showprinter, Showhelp, etc. The syntax of these methods is similar, as follows:
Object.showopen, we only need to write to this statement in the program, you can call out the "Open" dialog, which can also call other types of dialogs.
The properties of the Commondialog control are closely related to different dialog types. Some properties are only available for a class dialog, and some attributes differ in different dialogs, so the following categories are listed and different. The usage of the attribute associated with the dialog.
Properties related to Showopen, Showsave methods: ● FileName properties: Return or set the path and file name of the selected file, if you use the show method, set the default file name of the dialog; if it is later Use it to return the selected file name. Use the syntax: CommonDialog.FileName [= pathName] ● Filter Property: Return or set the filter displayed in the Type list box of the dialog (that is, the file type limiting the open or saved), its use syntax is: Object.filter [= Description Text 1 | Filter Number 1 | Description Text 2 | Filter Number 2] Where the text is arbitrary text, and the filter mark is *. file suffix (for example: *. bmp) format, description text and Filtering is separated from "|". ● DEFAULTEXT attribute: Returns or sets the default file extension for the dialog, which is the default value set by the defaultExt property when we do not specify the file type of open or saved. Properties related to showfont methods: ● Color selected color. To use this property, you must first set the Flags property to CDLCFEFFECTS. ● Whether FontBold selects "bold". ● FontITALIC is selected "bevel". ● FontstrikeThru is selected to delete lines. ● FontunderLine is selected to select underscore. ● FontName selected font name. ● Fontsize selected font size. The syntax used is direct reference. For example, we have to set the font of the text box according to the value returned by the Fonts dialog box, then use the statement: Text.Font = Commondialog.FontNname
Properties related to ShowColor methods: ● Color selected color. To use this property, you must first set the Flags property to CDLCFEFFECTS.
Properties related to Showhelp methods: ● HelpCommand properties Back or set the type of online help type ● HelpFile property determines the path and file name syntax of the help file is: Object.helpfile [= filename]
Let's take a look at the Cancelerror property, it is set as an error when electing the "Cancel" button, the syntax used is: commonDialog.cancelerror [= boolean] If we set it to true, when users are set to True, when the user The program will return a CDLCANCEL error when the "Cancel" button is selected, and we can avoid the error of the program by capturing this error. Specific uses can be seen in the source code. - See instance VB6.0 - Display Help Information in the Status Bar -
The status bar is a very useful control in VB, but it is not in the control panel by default, you must add it before we use it, the method is to press the Ctrl T shortcut, in the pop-up "Components" dialog box Select MS Windows Common Controls. At this time, the small icon of the status bar will appear in the control panel, double-click this icon to add it to the form.
Next, based on your own needs, select the status bar on the form, select "Property Properties" in the right mole menu, then pop up the dialog shown above, we select "PANELS pane" . The "INSERT PANEL" button is used to add the number of panes in the status bar. When we add a pane, "index" is automatically added, in the program, we reference the pane through the index value of the pane. Some of the items below are the properties of the pane, and we can set it in the program in this setting. The comparison of commonly used properties are: "text" - text displayed in each pane; "Alignment" - the text of the text in the pane; "style" pane style (select 0-sbrtext to display text, select 6-sbrdate is a display date, the like); "Bevel" - the concavity state of the pane. In addition, we can also add images in the pane, and the method is to click "Browse" and select the image file. In this program we dynamically display the help information in the first pane, display the date and time in the second, three panes. So how do you control the text displayed in the status bar in the program?
For example, we hope that the mouse moves to different buttons to display different help in the first pane of the status bar, then we will add the following statements in the button's onmouseover event: Private subdnew_mousemove (Button As INTEGER, SHIFT AS INTEGER) , X as single, y as single) stasample.panels (1) .text = "" stasample.panels (1) .text = "Empty text box" end sub
Similarly we can add help information for controls such as other buttons.
In order to learn, the source code provided has made detailed Chinese comments, see the code in the source code:
'--------------------------------------
'An example using STATUSBAR
'--------------------------------------
'Hong En online informational unlimited
'--------------------------------------
'------ Name --------------------------
'Cmdital "体" button
'Cmdbold "bold" button
'Cmdexit "exit" button
'Cmdnew "rewrite" button
'Text1 text box
'frMstatusbar main form
'Stasample Status Bar
'--------------------------------------
Option expedition
'When the "Body" button is pressed
Private sub cmditalic_click ()
'Set the font in the text box to italics
TEXT1.FONTITALIC = TRUE
End Sub
'When the mouse moves to the "slope" button
Private Sub Cmditalic_Mousemove (Button As Integer, Shift As Integer, x as single, y as single)
'Set the TEXT attribute in the first panels of the status bar to "Transforming to Body" help information
Stasample.Panels (1) .text = ""
Stasample.Panels (1) .text = "Transforming to Body"
End Sub
'When the "bold" button is pressed
Private subdbold_click ()
'Set the font in the text box to bold
Text1.fontbold = true
End Sub
'When the mouse moves to the "bold" button
Private Sub Cmdbold_Mousemove (Button As Integer, Shift As Integer, x as single, y as single)
'Set the TEXT attribute in the first panels of the status bar to "transform to bold"
Stasample.Panels (1) .text = ""
Stasample.Panels (1) .text = "Transforming into bold"
End Sub
'When the "exit" button is pressed
Private subdexit_click ()
'exit the program
Unload me
End Sub
'When the mouse moves to the "exit" button
Private subdexit_mousemove (Button As Integer, Shift As Integer, x as single, y as single)
'Set the TEXT attribute in the first panel of the status bar to the help information of "exiting the program"
Stasample.Panels (1) .text = ""
Stasample.Panels (1) .Text = "Exit Program"
End Sub
'When the "rewrite" button presses
Private subdnew_click ()
'Clear text in the text box
TEXT1.TEXT = "" "
Text1.fontbold = false
Text1.Fontital = FALSE
End Sub
'When the mouse moves to the "rewrite" button
Private Sub Cmdnew_mousemove (Button As Integer, Shift As Integer, x as single, y as single)
'Set the help information in the first panels in the status bar to "Empty Text Box"
Stasample.Panels (1) .text = ""
Stasample.Panels (1) .text = "Empty Text Box"
End Sub
Private sub edit_click ()
FRMPROPERTIES.SHOW VBMODAL
End Sub
'When the mouse moves onto the text box
Private sub text1_mousemove (Button as Integer, Shift As Integer, x as single, y as single)
'Set the TEXT attribute in the first panel of the status bar to "Enter Text" help information
Stasample.Panels (1) .text = ""
Stasample.Panels (1) .text = "Enter text"
End Sub
This is dynamically displayed in such a status bar. - See instance VB6.0 - Use the right-click menu -
Right-click menu allows the software's user to complete the operation, how do you use the right-click menu in your own written program? In this applet we can see the editing and use the right-click menu.
In order to learn, the source code provided has made detailed Chinese comments, see the code in the source code:
'--------------------------------------
'Pop-up mouse right Menu' ------------------------------------
'Hong En online informational unlimited
'--------------------------------------
'------ Name --------------------------
'Cmdcancel exit button
'MnUBlue "Lan" menu item
'Mnured "Red" menu item
'RichtextBox1 text box
'Popupfrm main form
'Mnufile Right-click on the name of the menu
'--------------------------------------
Private subcmdcancel_click ()
Unload me
End Sub
'When the "red" item of the pop-up menu is clicked
Private sub mnured_click ()
'Set the background color in the richtextbox box to red
RichtextBox1.backcolor = vbred
End Sub
'When the "blue" item of the pop-up menu is clicked
Private sub mnUBlue_click ()
'Set the background color in the richtextbox box to a blue
Richtextbox1.backcolor = vbblue
End Sub
'When you appear in the text box, the mouse is pressed.
Private Sub RichtextBox1_Mousedown (Button As Integer, Shift As Integer, x as single, y as single)
'MouseDown events Various syntaxes include the following sections:
'Button returns an integer, used to identify which button is pressed
'Where left button (bit 0), right button (bit 2), and intermediate buttons (bit 4)
'Shift returns an integer, indicating whether there is SHIFT, CTRL, Alt key to press
'x, y returns a number of current positions specified in the mouse pointer
'Button = 2 indicates right click
If Button = 2 THEN
'PopupMenu method is used to pop up a menu
'Grammar is object.popupmenu Menuname, Flags, X, Y
'Mnufile is the menu we designed in the menu editor
'X, y is the position of the pop-up menu, can be a number, if directly written as X, Y is in the current mouse position pop-up menu
Popupfrm.Popupmenu Mnufile, 0, x, y
END IF
End Sub
Let's open the menu editor (in the Tools menu), then add a first-level menu PopMenu, set its Visible property to "false", which represents the menu is not seen when the program is running, because the first-level menu is The second-level menu of the second-level menu, so the second-level menu is not seen. Then let's add a few second-level menus, pay attention to each menu has a CAPTION property and a name property. These two attributes must be written. The CAPTION property is the content displayed on the menu item, and the Name property is what we want The code specifically referred to in the program, similar to the NAME attribute of other controls. The separation line in the menu is implemented by setting the CAPTION property to "-".
After the menu is edited, you can reference and control it in the program. In this example we want to pop up this right-click menu when you click on the mouse button on the text box, you need to add the control statement in the mousedown event of the text box: Private Sub richtextbox1_mousedown (Button as integer, x as single, y as single) if button = 2 Then 'If the right mouse button is pressed, Form1.PopUpMenu Mnufile, 0, X, Y' pops up right-click menu end if end sub
The mean of various parameters of the mousedown event is as follows:
Button returns an integer to identify which button is generated, where the left button is 0, right-click 2, the middle key is 4 shift returns an integer, indicating whether there is Shift, Ctrl, Alt key Press X , y Returns a number of current positions specified in the mouse pointer
Popupmenu is a method:
The PopupMenu method is used to pop up a menu. The syntax is: Object.popUpMenu Menuname, Flags, x, y mnufile is the name x, y is the location of the menu we design in the menu editor, which can be a number, if directly Written as x, y is in the current mouse position pop-up menu
This will adjust the right-click menu, then we write code for the Click event of each menu item according to your own needs, complete some specific operations, and right-click menu can be used. The final effect is as shown below: