Home > Software engineering >  VB controls, same same events, to seek simple method
VB controls, same same events, to seek simple method

Time:09-18

Using VB to write a serial port communication of upper machine, there are many keys to send data, but after each key press send content is similar, only the individual bytes, is now a button click event of a function to write, basic it is copy and paste, then change details,
Do not know to have simple way to use a function to write, tell the difference between the button and using the method of parameter byte data transfer into, so also easy to maintain;
Or have other what method do this,

Private Sub Command21_Click ()
Dim Data (4) As Byte
If Form1. MSComm1. PortOpen=False Then
MsgBox "[please open the serial port]," vbOKOnly, "tip"
The Else
If Len (Text1. Text)=0 Then
MsgBox "please input address!" VbOKOnly, "Warning"
GoTo finish
End the If
Data (0)=& amp; HAA
Data (1)=Val (Text1. Text)
Data (2)=& amp; H14
Data (3)=& amp; HB
Data (4)=& amp; H11
Form1. MSComm1. The Output=Data
Command21. BackColor=& amp; HC000 & amp;
Command22. BackColor=& amp; H8000000F
Finish:
End the If
End Sub

CodePudding user response:

With the control array bai,

In the process of event, which is to distinguish between "control index number" according to the control, deal with the corresponding "different data" area,

CodePudding user response:

Study the VB control array

CodePudding user response:

Control array of events, can add an Index parameters to the difference between the current control,

CodePudding user response:

Statement send byte global variables, may change in each button Click event to byte value assignment, and perform the sending process, a serial port is opened, when the form loads without MsgBox statements, process that executes instructions to send,

CodePudding user response:

Use the control array can do it:
Private Sub Text1_Click ()
For I=0 to text1. Count - 1
Text1 (I). The text="OK"
Next I
End Sub
Can do that without control array:
Private Sub Text1_Click ()
Text1. Text="OK"
End Sub

Private Sub Text2_Click ()
Call Text1_Click ()
End Sub
.
TEXT3 4 5 6 7... In the same way

CodePudding user response:

With the control array,,,
  • Related