VB programming method

zhaozj2021-02-16  52

VB programming method

I am engaged in the teaching and programming of VB, some simple skills who use VB programming in teaching and work, I want to share with beginners.

First, how to use the database data to add an example entry to the combo box

Add an example entry to the combo box, usually we use two ways. The first is to add it through the list attribute during the design phase. The second is to add it directly using the AddITEM "string" method in the program. This method is added to the composite box CMBTILE.

WITH CMBTILE

.Clear

.Additem "principal"

.Additem "Vice President"

.Additem "Director"

.Additem "Secretary"

.Additem "Total Tube"

.Additem "officer"

.Additem "Other"

.Listindex = 0

End with

Here are what we have to have a database, want to add some fields of this database to how to do in a combo box. We can build a process such as LoadDataTocombo, three parameters are CMB, the other is Stable, the third is Sfield. Where the CMB is a combo box to which it is to be added, the Stable is a database to add data to the combo box, and the Sfield is the field to add to the combo box. The implementation method is shown in the process.

Private sub loadDataocombo (byref cmbec as comboBOX, BYVAL STABLE AS STRING, BYVAL SFIELD AS STRING)

CMB.CLEAR

DIM RS As New Adodb.Recordset

RS.Open "SELECT" & Sfield & "from" & stable, cn.connectionstring, adopenkeyset, AdlockReadonly

While Not Rs.eof

CMB.Additem Rs.fields (sfield) .value

rs.movenext

Wend

Rs.close

SET RS = Nothing

IF cmb.listcount> 0 Then cmb.listIndex = 0

End Sub

Call the ManagerName fields in the Manager database to CMBMANs can be called as follows.

LoadDataTocombo CMBMAN, "Manager", "Managername"

Second, the method of implementing database table printing in VB

Method 1: On the Project menu, click Add Data Environment "to add a designer to the project. Click the Designer tab. And click the "Data Environment" to add the designer to the menu. Right-click the "Connection1" icon, and then click Add Command to display the "Command1" dialog. Alter the attribute, add a connection, connect the database table to "Command1".

On the Project menu, click Add Data Report, Visual Basic will add it to your project. Click the Designer tab and click "Data Report" to add the designer to the menu. On the Properties window, click Data Source, and then click the appropriate command. Then click DataMember and click Customers. From the Data Environment Designer, drag the "OrderDate" field (under the Orderation command) to the details (Orders_Detail) section. At the same time, some horizontal lines and vertical lines are added in the details section. Specific details can refer to "Creating a Simple Data Report" in MSDN.

Method 2: In fact, we can have a more direct way to utilize the Print method of the Printer object, while using the tab symbols in the location code and the TAB function to implement the printing of the database table. Here is a simple example and can be referred to. Private subdprint_click ()

Dim n as integer

DIM RS As New Adodb.Recordset

Rs.Open "Select * from JGGZ", "provider = microsoft.jet.Oledb.4.0; data source = d: /jggz/jggz.mdb; persist security info = false, AdopenStatic, AdlockPESSIMISTIC

N = 0

rs.movefirst

Form1.print "┌ - ─ ┬─── ┐"

While Not Rs.eof

Printer.print "│" & │Fields ("Name") .value; Tab (11); "│"; RS.Fields ("Class Tour"); Tab (21); "│"

rs.movenext

n = n 1

IF n / 5 = int (n / 5) or or.eof kil

Printer.print "└ - ─ ┴─── ┘"

Printer.print "┌ - ─ ┬─── ┐"

Else

Printer.print "├──────── ┤"

END IF

Wend

Printer.print "└ - ─ ┴─── ┘"

Rs.close

End Sub

Attachment 1: Table symbol location control

Location 0904 0906 0916 0920 0924 0928 0932 0940 0948 Symbol ─ │ ┌ ┐ └ ┘ ├ ┤ ┬ District 0956 0964 0905 0907 0919 0923 0927 0931 0939 Symbol ┴ ━ ━ ┏ ┏ ┓ ┗ ┛ ┣ ┣ 0979 0936 0944 0951 0959

Symbol ┫ ┳ ┻ ╋ ┠ ┨ ┯ ┷

Third, using Winsock to achieve simple network programming

TCP, UDP protocol communications can be implemented using Winsock. We know that network communication is looking for three addresses, host addresses, service addresses (port numbers). The network and host addresses can be obtained by the IP address of the machine and the subnet mask. And each machine's service address (port number) has 65536. Let's talk about how to use Winsock to implement network communications.

Server-side programming: Create a form, add a Text control to store the content of sending information, a command button (cmdsend) is used to send information, two Winsock controls (Scklisten, Sckserver), one for listening, one Accept. Set the LocalPort property of SckServer to 8001 (must not be like the port number of Scklisten).

The program code is as follows:

Private subcmdsend_click ()

Sckserver.senddata text1.text

End Sub

Private sub flow_load ()

Scklisten.localport = 8000

Scklisten.listen

End Sub

Private sub scklisten_connectionRequest (ByVal Requestid As Long)

Sckserver.accept Requestid

cmdsend.enabled = trueend sub

Private sub sckserver_dataarrival (byval Bytestotal As Long)

DIM S As String

Sckserver.getdata S, VBString, Bytestotal

Msgbox S

End Sub

Workstation End Program Writing: Create a form, add a Text control to store the content of the sending information, a command button (cmdsend) is used to send information, a Winsock Control (SCKCLIENT) is used to accept information. Set the SCKCLICALPORT property to 8002.

The program code is as follows:

Private subcmdsend_click ()

Sckclient.senddata text1.text

End Sub

Private sub flow_load ()

DIM SIP As String

SIP = INPUTBOX ("Please Input IP Address", SCKCLIENT.LOCALIP)

Sckclient.Connect SIP, 8000

End Sub

Private sub sckclient_dataarrival (byval bytestotal as long)

DIM S As String

Sckclient.getdata S, VBString, Bytestotal

Msgbox S

End Sub

How to use: Start the server, run the workstation program, enter the IP address of the server host, enter the content you want to send in the Text box, click Send Command Button, at which time the server accepts information. At this time, enter the content sent in the server's text box, click Send Command Button, at which time the workstation accepts information.

Of course, this is just a simple test program, which can use variables, databases, etc. to do a lot in practical applications. At the same time, this program can only be transmitted one-to-one, want to implement multiple workstations of a server, can build several Winsock receiving controls in the server side, one control corresponding to a workstation, but note that the LocalPort property of the Winsock control is different. . That is, a port corresponds to a service.

Gao Xiaofei

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

New Post(0)