Vbprogramming skills)

Source: Internet
Author: User

For programmers, VB is easy to use, but it requires some effort to control it in depth and flexibility. I will introduce several typical programming skills here, hoping to help the majority of VB enthusiasts.

1. How to create a custom cursor 1. When designing an applicationProgramVisual Basic allows programmers to design the mousepointer attribute of many controls into one of 12 pre-defined mouse cursors. However, some programmers may want to display a cursor out of a predefined shape. This article describes how to create a different mouse pointer (cursor), including creating a cursor for a control without the mousepointer attribute. You can addCodeTo change the mousemove and dragover events of the control you want to monitor. The Code contained in the mousemove event is used to trigger the drag method of the control. When the mouse moves over the selected control, the new mouse pointer is displayed in turn. When the mouse pointer leaves the control, the dragover event is triggered. In Visual Basic, You can reset the drag attribute so that the previous mouse pointer can be displayed again.

2. The following sample program changes the mouse pointer to a different shape when moving to a file list box control. First, use the default method to create form1. Add a file list control on form1 and use the default method to create file1. Set the dragicon attribute of the file1 control to the selected. ICO file. Add the following code to the file1 mousemove event: private sub File1-MouseMove (buttonas integer, shift as integer, X as single, y as Single) file1.drag 1 'icon on end sub ---- Add the following code to the dragover event of form1: private sub form-dragover (source as control, X as single, y as single, state as integer) file1.drag 0' icon off end sub press F5 function key to execute this program. The running result is: When you move the mouse pointer to the control of the file list box, the program will set the selected. the ICO file is used as the default mouse cursor. When you move the mouse pointer away from the control, the cursor is automatically restored to the default shape.

2. How to right-click a form to generate a pop-up menu )? As we all know, in Windows 95/98/2000 desktop and many popular software windows, when we right-click, a shortcut menu will pop up at the current position of the mouse. Do many programming-loving friends want to have similar functions in their own programs? In fact, this is not difficult. After some efforts, I will find a common method under VB for you to share. To implement the above functions, two steps are required: 1. use the menu editor of VB to edit the menus and submenus you want to bring up. Note that you must set the visible attribute of the menu to false. 2. write a program in the form (form1) mousedown event to stimulate the edited menu. Assuming the menu name is popmenu, the program source code is as follows: private sub form-mousedown (button as integer, shift as integer, X as single, y as single) If Button = vbrightbutton then popmenu. visible = true popupmenu popmenu end if end sub the preceding method is for the form. You can also right-click a widget and choose a shortcut menu. The method is also very simple. You only need to put the above Code into the mousedown event of the corresponding control.

3. How to dynamically determine whether controls exist in a certain area on the form? In a small program of the author, to output data in a certain area of the form, this requires that no other control exists in this area, then, how can I know whether controls exist in a certain area of the form? To determine whether a widget exists in a certain area of the form, we can use the following vbprogram: function getcontrol (x1 as single, Y1 as single, X2 as single, Y2 as Single) as control dim control as control for each control in form1 with control if (x1 <=. left) and (X2> =. left) and _ (Y1 <=. top) and (Y2> =. top) or _ (x1 <=. left + width) and (X2> =. left + width) and _ (Y1 <=. top) and (Y2> =. top) or _ (x1 <=. left) and (X2> = left) and _ (Y1 <=. top + Height) and (Y2> =. top + height) or _ (x1 <=. left + width) and (X2> =. left + width) and _ (Y1 <=. top + height) and (Y2> =. top + height) then set getcontrol = control exit function end if end with next set getcontrol = nothing end function note :( X1, Y1) and (X2, Y2) the coordinates of the points in the upper left corner and lower right corner of the selected rectangle area. This program calculates the positions of the four corners of all controls on the form to determine whether the control is intersecting with the selected area, and returns the Intersection control.

4. Obtain and modify the computer name in Win 95/98/2000. The computer has a name. Run regedit and find "computername" = "default" (or another string) in "HKEY-LOCAL-MACHINE \ System \ CurrentControlSet \ Control \ computername ), you can view and modify this name in regedit. You can also use the getcomputername and setcomputername functions provided by WIN32API in the program to view and modify the computer name. The following uses VB as an example to discuss how to compile a program that allows you to view and modify computer names. 1. insert a new module and add the following code: 'declare getcomputername declare function getcomputername lib "kernel 32" alias "getcomputernamea" (byval lpbuffer as string, nsize as long) as long 'Declares setcomputername declare function setcomputername lib "kernel 32" alias "setcomputernamea" (byval LP computername as string) As long' defines a function for obtaining computer names. Public Function getcname (cname) as Boolean dim scomputername as string 'computer name dim lcomput Ername as long 'computer name length dim lresult as long 'getcomputername return value dim RV as Boolean' getcname return value. If it is true, the operation is successful. lcomputernamelen = 256 scomputername = space (lcomputernamelen) lresult = getcomputername (scomputername, lcompputernamelen) If lresult <> 0 then cname = left $ (scomputername, lcomputernamelen) rv = true else Rv = false end if getcname = RV end function 'defines a function named public function setcname (cname) Boolean dim lresult as long dim RV as Boolean lresult = setcomputername (cname) If lresult <> 0 then Rv = true' modified else Rv = false end if setcname = RV end function 2. add a command button command1 in the form, double-click the button, and add the following code in it: Sub Command1-Click () dim cn as string x = getcname (CN) print "this computer name is: ", CN Cn =" mycomputer "x = setcname (CN) print" Now the computer name is: ", CN end sub OK, save the above settings and code, then press F5 to run the program.

5. Anyone who has used the picturebox control to add a scroll bar to the picturebox control of VB knows that we can load images in it. When the image size is not large, it may not be a problem. However, if the image size is larger than picturebox, we can only see a part of the image, how can we see other parts? To solve the above problem, we can add a horizontal and vertical scroll bars inside the picturebox and use the scroll bars to display invisible images. The specific method is as follows: first add an OCX control to the project, click the project menu item on the menu, and click components in the drop-down menu ), select "Microsoft common dialog control 5.0" to finish loading. Then, draw a picturebox, use the default name picture1 provided by VB, and then draw a picturebox on picture1, the default name is picture2. Do not forget to set picture2.autosize to true. Then, with the horizontal and vertical scroll bars added, the default names are hscroll1 and vscroll1, respectively. Then, the image will be loaded to picture2, finally, you can introduce other controls in the form: A button (command), the default name is command1 and a "Microsoft common dialog control", the default name is commondialog1. The specific VB Code is as follows: private sub form-load () temperature = 0 temperature = 0 picture2.width = temperature picture2.height = picture1.height temperature = 0 temperature = 0 temperature = 0 temperature = picture2.height-picture1.height temperature = picture2.width-picture1.width if temperature <0 Then temperature = false if vscroll1.max <0 then vscroll1.enabled = false End sub private sub Command-click () on error goto errexit commondialog1.filter = "bitmap file (*. BMP) | *. BMP | all file (*. *) | *. * "rows = 1 rows = loadpicture (rows) vscroll1.min = 0 hscroll1.min = 0 rows = picture2.height-picture1.height rows = picture2.width-picture1.width if hscroll1.max <0 then rows = false if Croll1.max <0 then vscroll1.enabled = false errexit: End sub private sub HScroll1-Change () picture2.left =-hscroll1.value end sub private sub VScroll1-Change () picture2.top =-vscroll1.value end sub the program by clicking the command1 button, in the pop-up dialog box, select a graphic file to be loaded to picture2, and use the horizontal and vertical scroll bars to scroll the image.

6. Using VB as a chat program the so-called "chat" means that two programs can send data to each other. This program involves the knowledge of data communication. It seems very complicated. However, the problem becomes very simple because VB provides us with a Winsock Control. First write the "chat (host)" program. Add the Winsock control to the form, set its Protocol attribute to 1-sckudpprotocol, and set other attributes to the default value. Add two labels and two text boxes, and set the title attributes of the two labels to "Receiving Window" and "sending window" respectively. The title attributes of the two text boxes are empty. Finally, write the code: 1. "chat (host)" private sub form-load () 'sets network address winsock1.localport = 1024 winsock1.remotehost = "202.96.6.1" winsock1.remoteport = 1999 end sub private sub Text1-Change () 'Send user input content winsock1.senddata text1.text end sub private sub Winsock1-DataArrival (byval bytestotal as long) dim rec as string' receive peer data and display winsock1.getdata REC in the text box, VB string text2.text = rec end Sub 2. "chat (sub-host)" private sub form_load ()′ Set the network address winsock1.localport = 1999 winsock1.remotehost = "202.96.6.1" winsock1.remoteport = 1024. Other programs are the same as (host. Finally, save the two programs and compile them into the execution (. EXE) file. Now you can use this program for dialog.

7. How to highlight a specific character or string in the text box at the same time. Because the common Textbox Control does not support simultaneous highlighting of discontinuous strings, we select the RichTextBox Control. Click the project menu item. In the displayed drop-down menu, click the component menu item. In the displayed dialog box, select the Microsoft Rich Textbox Control 5.0 check box and confirm to load the RichTextBox Control. Create a new project and add a RichTextBox Control and two command (button) controls on the form. The default value of name attribute is used; set the text attribute value of RichTextBox to null. The caption attribute values of command1 and command2 are set to "input text" and "select string" respectively ". Finally, add the following VB code: private sub Command1-Click () dim STR as string dim text as string STR = "input text" text = inputbox (STR) richtextbox1.text = text end sub private sub Command2-Click () dim STR as string dim text as string dim position as integer dim lenth as integer STR = "Enter the string to be highlighted" text = inputbox (STR) if text <>″ "then position = instr (richtextbox1.text, text)-1 lenth = Len (text) richtextbox1.selstart = position richt Extbox1.sellength = lenth richtextbox1.selcolor = RGB (255, 0) do while instr (Position + lenth + 1, richtextbox1.text, text) <> 0 position = instr (Position + lenth + 1, richtextbox1.text, text)-1 richtextbox1.selstart = position richtextbox1.sellength = lenth richtextbox1.selcolor = RGB (255, 0, 0) loop end if end sub execute the program by pressing F5, click "input text, enter some text in the pop-up dialog box. After confirmation, the text you just entered will be displayed in RichTextBox, and then click "select string, in the displayed dialog box, enter the string you want to highlight, The corresponding string in RichTextBox is highlighted in red.

8. to program the Windows 95/98 operating system Hot Start method, you need to use the program to realize the system restart. You can call the API function in your program. Create a sub-function: (using VB as an example) Declare function systemparametersinfo lib "USER32" alias-"systemparametersinfo" (byval uaction as long, byval uparam as long, byval lpvparam as any, byval fuwinini as long) As long sub disablectrlaltdelete (bdisabled as Boolean) dim X as long x = systemparametersinfo (97, bdisabled, CSTR (1), 0) end sub call disablectrlaltdelete (true) 'No hot start call disablectrlaltdelete (false) 'allow hot start

9. we all know how to automatically start a program after Windows 95/98 is started. There is a "start" menu item in the "Start" → "program" menu of Windows 95/98, every time you start Windows 95 or Windows 98, the system automatically starts the executable program in the "Start" menu. However, there are a lot of software, such as jieba 5, ICQ, and most of the software that detects viruses in real time. After installation, it is not included in the "Start" menu, it can also be automatically started when the operating system is started. How can this problem be achieved? In fact, as long as you know some knowledge about the Windows registry, this problem cannot be called a problem. Click Start, open the Start Menu, click Run, a dialog box appears, enter regedit, OK, the system Registry Editor opens, find the HKEY-LOCAL-MACHINE? Software? Microsoft? Windows? CurrentVersion? Run, add the entry to your program. If you do not know how to add a key, refer to the existing key value.

10. How to output data files to the text control? If the data size is large and the full screen of the form is not large enough, how can this problem be solved? A simple method is to put the data in a text box and add a horizontal and vertical scroll bars. The specific implementation steps are as follows: First add a text box in the form with the default name text1; then, set the attributes of the text box text1: The text attribute is set to null, set the multiline attribute to true and the scrollbars attribute to 3-both. Then add the following VB code: private sub form-load () dim handle as integer dim filename as string on error goto errexit begin: 'enter the name of the data file to be displayed filename = inputbox $ ("input FILENAME", "Open File ″) on Error goto fileerr handle = freefile open filename for input as # handle 'output data in the data file to text1.text = Input $ (lof (handle ), Handle) close # handle exit sub fileerr: dim errnum as integer if err. number = 53 then errnum = msgbox ("file not exist", vbokcancel, "error information") if errnum = 1 Then goto begin else exit sub end if msgbox err. description, "file open failed" errexit: Exit sub end sub 'Fill the text box with the entire form private sub form-resize () text1.left = 0 text1.top = 0 text1.width = form1.width-100 text1.height = form1.height-400 End sub not only solves the problem, but also allows you to edit the data in the text box.

11. to create a dialog form that contains the drive list box, directory list box, and file list box, how can I synchronize the three items? This is a problem that we often encounter in practical applications. It is very simple to solve this problem in VB. It can be achieved through the change event caused by the change of the path attribute. For example: Sub Dir1-Change () file1.path = dir1.path end sub this event process synchronizes the directory list box dir1 on the form with the file list box file1. Because changes to the path attribute of the directory list box will generate a change event, so in the Dir1-Change event process, the dir1.path is assigned to file1.path, you can produce a synchronization effect. Similarly, by adding the following event process, you can make three ListBox synchronization operations: Sub Drive1-Change () dir1.path = drive1.drive end sub this process to synchronize the drive list box and directory list box, in the previous process, the directory list box and the file list box are synchronized, so that the three lists are synchronized. The problem can be solved.

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.