VB programming experience

zhaozj2021-02-17  46

---- I am getting bigger and bigger when I see VB programmer and enthusiasts, and now VB6 has come to the world, and I have a little experience in this point of VB5 programming, and exchange communication with the majority of friends. Inappropriate, more advice.

---- 1. Display a variety of types of files in the same time in the open dialog box

---- When you open a file, we tend to open the dialog CommonDialog, you want to limit the file type to a certain range of Filter properties, such as setting the Filter property to "* .txt | * .txt "Then open the dialog only to display the text file. If you want to display several types of files, for example, you want to display all graphics files, its setting method is set to "image files | * .bmp; *. Jpg; *. Gif", open the dialog box. Display BMP files, JPG files, GIF files. This question seems to be very simple, but the author once checked a large amount of books that were related to VB for this question, but did not find it.

---- 2. Note the problem of the most compilation

---- If you use the data library control in the program, you will pay attention to the DATEBASE property of the database control in the last time, and you will be installing the dish, which is installed on its machine. The original is the path issues stored in the Datebase property. Before compiling, set its path to the front path. If you use the Access data library, you can remove the path information in the DATEBASE attribute. For example, the original "c: /money/money.mdb" is first changed to "Money.mdb". If you are foxpro data, you can change the DATEBASE attribute to "." You can, "." For example, the original "C: / Money" is first changed to "." The approval of the data of its data library is not much different. When making a safe discharge, add the data documents to the data, which is not detailed.

---- 3. Calling its procedure in the program

---- Doing the big family under DOS can use the system () function to call its license file, how do you call it under Windows? Do you have this function in the Windows API. The function is as follows:

UINT WINEXEC (LPCSTR LPSZCMDLINE, // Command Line Address UINT FUCMDSHOW // The window type when you open);

---- If you use it, you should add a statement:

Private Declare Function Winexec LIB "Kernel32" (Byval Ncmdshow As Long) AS Long

---- If you want to call the self-container in Windows in the program, you can use the code of the lower surface: Winexec "C: /Windows/calc.exe", 10

---- The parameter 10 makes the start-up window in the default state, if you use 3 to maximize the window of the start, if you use 6 to minimize the starting window. In practice, many multi-merchant software, the meter, which is directly connected to the Windows self-belled.

---- 4. Enter the legal detection of data

---- A software often requires users to enter some data, and these data often require a certain format, such as entered a shaping, or a date, etc., such as software designers must think of the possibility of user error, in the user Tips and corrections are given when an error occurs. Only if you are your program is a good program. There are many ways to detect in VB programming, such as you can use some functions to detect data in the data of the data. The writer issued only to suit the error-in-one data between the wrong trap technique and VB in VB, the auto-conversion function of VB can be well realistic, the program is simple and the effect is good, and the various types of data can be detected.

---- If there is no need to detect the string, if you want to enter, you must enter a fixed or currency shape. Let's talk about the intepralation test. For example, if you have a text (Text1), you need to enter an integer, we can check in its LostFocus event, the code is as follows:

Private sub text1_lostfocus () DIM I as integer 'Defines a shaping Ion error goto error1' setting error trap if text1.text is not an integer jump to error1i = text1.text 'You can do other processing EXIT SUB ERROR1: MSGBOX ("Please enter An integer ") Text1.setfocus end SUB

---- This can improve the legality of the legality, if you want the floating point, you can define it into float, namely:

Private sub text1_lostfocus () DIM I as Singleon Error Goto Error1i = text1.textexit suberror1: msgbox ("Please enter a single precision floating point number) Text1.setfocusend Sub

---- The same, if the date is that the date will be determined into the date, it is to say that what is the amount of data that is determined to determine the data type.

---- When you don't have to detect data from the affirmation in the LostFocus event, if it is a data library application process, you can check the data in the data before the data is added to the data library, and the user will add more convenience. ---- 5. Probe and settings of the window body

---- Want to obtain the cursor position on the window with VB, you can get the position of the cursor on the window with the parameters of the MouseMove event. However, if you want to get the cursor position outside the window or want to set the cursor position in the program, then you can help the API, it's okay, these API functions are non-simple, that is, make the friend who have just connected friends can learn. The author is a simple introduction, hopes and big family sharing to use the API fun.

---- First, let's take a relationship and function of the associated API. First look at this function that gets the cursor position, its sound is as follows:

Bool getCursorpos (LPPOINT LPPOINT / / Pointer to point the Point structure, which contains the location information of the obtained cursor); then look at the structure of the Point: typedef struct tagpoint {// pt long x; long y;} Point; is very simple Finally, look at the function of setting the cursor position: BOOL setCursorpos (int X, // To set the horizontal consoct INT Y // on the screen to set the ordinate on the screen);

---- In VB, it is necessary to make a definitely clear, but also VB provides a "API text check-in". You only have to use the sound of the API you want to use. Let us do a simple example to try it. First build an item, put two TextBox controls (Text1 and Text2) on the window (Form1), a CommandButton control (Command1) and a Timer Control (Timer1). Change Timer1's Interval property to 1000. The function of this example is the position coordinate of the scales in TEXT1 and TEXT2 every other second, and press the Command1 button cursor to drift on the screen at a fixed rule. All code is as follows:

Private Type POINTAPIx As Longy As LongEnd TypePrivate Declare Function GetCursorPosLib "user32" (lpPoint As POINTAPI) As LongPrivate Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long 'above the API and the data structure declarations

Private Sub Command1_Click () Dim i As IntegerDim j As IntegerFor i = 0 To Screen.WidthSetCursorPos i, 100 'set the cursor position For j = 0 To 1000' cycle delay Next jNext iEnd SubPrivate Sub Timer1_Timer () Dim point1 As POINTAPIGetCursorPos point1 ' Get cursor position text1.text = Point1.x 'Display cursor position xtext2.text = Point1.y' Display cursor position YEND SUB

---- If the input is completed, the deployment is running, and it can be easily realized in VB.

---- 6.Windows obtained various paths

---- Programming under Windows It is often necessary to obtain various paths in the program, more than one path, the path of the system, and the like. The full API function is provided in the API of Windows for our call.

---- Let's take a look at these API functions:

---- (1). DWORD GETCURRENTDIRECTORY

DWORD NBUFFERLENGTH, / / ​​Receive the length of the character string of the directory lptstr lpbuffer // Receive the address of the character string of the directory);

---- Pass the function of this function will get the previous directory, if the call is lost, this function will return zero. When calling this function, the NBufferLength parameter should be large enough, and the string of the received directory should have this long. See the case. Another SETCURRENTDIRECTORY () function can be used to set the front directory.

---- (2). Uint getsystemdirectory

LPTSTR LPBUFFER, / / ​​Receive the address of the address of the character string of the directory UINT USIZE / / Receive the length of the character string of the directory);

---- Conducting this function can get the directory of the system, if the call is lost, this function will return zero.

---- 3. Uint getWindowsDirectory

LPTSTR LPBUFFER, / / ​​Receive the address of the address of the character string of the directory UINT USIZE / / Receive the length of the character string of the directory);

---- Conducting this function can get a Windows directory, if the call is lost, it will return zero. Cover this function and the second function of the second function, you will not be clearly clear.

---- Under the next, VB is used. First, you should first add the API's sound of the API to the code, start a new project, put three buttons on the form (Form1) (Command1, Command2, Command3). This example of this example is that the click button is displayed with the path shown by MSGBOX.

---- The writer issued the sound of the VB5 API text in the case, the sound of the API.

Private Declare Function GetCurrentDirectoryALib "kernel32" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As LongPrivate Declare Function GetWindowsDirectoryALib "kernel32" (ByVal lpBuffer As String, ByVal nSize As Long) As LongPrivate Declare Function GetSystemDirectoryA Lib "kernel32" (ByVal lpBuffer As String, byval nsize as long) As longprivate submmand1_click () DIM path as string * 255GetcurrentDirectorya (Len (PATH), PATH) Gets the current path MSGBOX (PATH) End Sub

Private Sub Command2_Click () DIM PATH AS STRING * 255GETWINDOWSDIRECTORYA (PATH, LEN (PATH)) Gets Windows Path MSGBOX (PATH) End Sub

Private submmand3_click () Dim path as string * 255GetsystemDirectorya (Path, Len (Path)) Gets the System Path Msgbox (PATH) End Sub

---- 7. Question on the wrong trap

---- The writer issued a problem with the wrong trap technique in the cycle. Compared with the following sequence:

Private submmand1_click () DIM I as INTEGERDIM J AS INTEGERON ERROR GOTO JERROR I = 0 to 3MSGBOX (i) j = 10000 * 10000 'Let J have overflow error Jerror: Next IEND SUB

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

New Post(0)