本文目录
- vb编程实例
- 求一个简单vb编程实例~
- vb编程实例 交互式窗口,输入100个数据,求平均值、平方后再开方和自动分类
- vb程序设计实例100
- vb编程实例 交互式窗口,手动输入100个数据,求平均值、平方后再开方和自动分类
- 告诉我一些简单的vb编程代码
- vb串口通信程序实例
- vb编程例题
vb编程实例
Private Sub Form_Click()
Form1.Width = 13000
Dim i As Integer, j As Integer
Cls
Print
Print “***************九九乘法表*****************“
Print
For j = 1 To 9
Print “ 1 *“; j; “=“; j;
For k = 2 To j
Print Tab(15 * (k - 1)); k; “*“; j; “=“; j * k;
Next k
Print
Next j
End Sub
求一个简单vb编程实例~
控件:image,picture,timer代码:Dim x As Integer
Dim y As IntegerPrivate Sub Form_Load()
x = 100
y = 100
Timer1.Interval = 100
End SubPrivate Sub Timer1_Timer()
If img.Top 》= pic.Height - img.Height Then
y = -100
End If
If img.Top 《= 0 Then
y = 100
End If
If img.Left 》= pic.Width - img.Width Then
x = -100
End If
If img.Left 《= 0 Then
x = 100
End If
img.Top = img.Top + y
img.Left = img.Left + x
End Sub
以上代码实现的是图片框中的一个小球做的规律的运动,当小球碰到图片框边缘时便反弹继续运动
vb编程实例 交互式窗口,输入100个数据,求平均值、平方后再开方和自动分类
Dim intArray(100) As Single, sglAverage As Single
Private Sub Command1_Click()
Randomize (Timer)
Dim i As Integer, intSum As Integer
Me.Picture1.Cls
For i = 1 To 100
intArray(i) = Int(Rnd(i) * 5 + 95)
Me.Picture1.Print intArray(i);
If i Mod 10 = 0 Then Me.Picture1.Print
intSum = intSum + intArray(i)
Next i
sglAverage = intSum / 100
Label1.Caption = “Sum=“ & intSum
Label2.Caption = “Average=“ & sglAverage
End Sub
Private Sub Command2_Click()
Dim i As Integer, k0 As Integer, k(4) As Integer
For i = 0 To 4
Me.Picture2(i).Cls
Next i
For i = 1 To 100
If Sqr((intArray(i) - sglAverage) ^ 2) 》= 1.67 Then
k(0) = k(0) + 1
Me.Picture2(0).Print intArray(i);
If k(0) Mod 10 = 0 Then Me.Picture2(0).Print
ElseIf Sqr((intArray(i) - sglAverage) ^ 2) 》= 1.33 And Sqr((intArray(i) - sglAverage) ^ 2) 《 1.67 Then-vb编程实例
k(1) = k(1) + 1
Me.Picture2(1).Print intArray(i);
If k(1) Mod 10 = 0 Then Me.Picture2(1).Print
ElseIf Sqr((intArray(i) - sglAverage) ^ 2) 》= 1 And Sqr((intArray(i) - sglAverage) ^ 2) 《 1.33 Then-vb编程
k(2) = k(2) + 1
Me.Picture2(2).Print intArray(i);
If k(2) Mod 10 = 0 Then Me.Picture2(2).Print
ElseIf Sqr((intArray(i) - sglAverage) ^ 2) 》= 0.67 And Sqr((intArray(i) - sglAverage) ^ 2) 《 1 Then-vb编程实例
k(3) = k(3) + 1
Me.Picture2(3).Print intArray(i);
If k(3) Mod 10 = 0 Then Me.Picture2(3).Print
Else
k(4) = k(4) + 1
Me.Picture2(4).Print intArray(i);
If k(4) Mod 10 = 0 Then Me.Picture2(4).Print
End If
Next i
For i = 0 To 4
Me.Label8(i).Caption = “共计“ & k(i)
Next i
End Sub
vb程序设计实例100
试题一.1
Private Sub Form_Click()
Print fs_sum(100)
End Sub
Function fs_sum(ByVal n As Integer) As Single
Dim s!, nc! ’和与倒数
s=0
For i = 1 To n
nc = 1 / i
s = s + nc
Next i
fs_sum = s
End Function
试题一.2
Private Sub Form_Click()
For i = 1 To 4
Print Space(4 - i);
For j = 1 To i
Print “* “;
Next j
Print
Next i
End Sub
待续,未完...
-vb编程
vb编程实例 交互式窗口,手动输入100个数据,求平均值、平方后再开方和自动分类
vb编程实例 交互式窗口,手动输入100个数据,求平均值、平方后再开方和自动分类代码如下(附图):
Dim intArray(100) As Single, sglAverage As Single
Private Sub Command1_Click()
Randomize (Timer)
Dim i As Integer, intSum As Integer
Me.Picture1.Cls
For i = 1 To 100
intArray(i) = Int(Rnd(i) * 5 + 95)
Me.Picture1.Print intArray(i);
If i Mod 10 = 0 Then Me.Picture1.Print
intSum = intSum + intArray(i)
Next i
sglAverage = intSum / 100
Label1.Caption = “Sum=“ & intSum
Label2.Caption = “Average=“ & sglAverage
End Sub
Private Sub Command2_Click()
Dim i As Integer, k0 As Integer, k(4) As Integer
For i = 0 To 4
Me.Picture2(i).Cls
Next i
For i = 1 To 100
If Sqr((intArray(i) - sglAverage) ^ 2) 》= 1.67 Then
k(0) = k(0) + 1
Me.Picture2(0).Print intArray(i);
If k(0) Mod 10 = 0 Then Me.Picture2(0).Print
ElseIf Sqr((intArray(i) - sglAverage) ^ 2) 》= 1.33 And Sqr((intArray(i) - sglAverage) ^ 2) 《 1.67 Then-vb编程实例
k(1) = k(1) + 1
Me.Picture2(1).Print intArray(i);
If k(1) Mod 10 = 0 Then Me.Picture2(1).Print
ElseIf Sqr((intArray(i) - sglAverage) ^ 2) 》= 1 And Sqr((intArray(i) - sglAverage) ^ 2) 《 1.33 Then-vb编程
k(2) = k(2) + 1
Me.Picture2(2).Print intArray(i);
If k(2) Mod 10 = 0 Then Me.Picture2(2).Print
ElseIf Sqr((intArray(i) - sglAverage) ^ 2) 》= 0.67 And Sqr((intArray(i) - sglAverage) ^ 2) 《 1 Then-vb编程实例
k(3) = k(3) + 1
Me.Picture2(3).Print intArray(i);
If k(3) Mod 10 = 0 Then Me.Picture2(3).Print
Else
k(4) = k(4) + 1
Me.Picture2(4).Print intArray(i);
If k(4) Mod 10 = 0 Then Me.Picture2(4).Print
End If
Next i
For i = 0 To 4
Me.Label8(i).Caption = “共计“ & k(i)
Next i
End Sub
告诉我一些简单的vb编程代码
给几个写的模块给你看看
Public Function LoadData(strSQL As String)
Dim rst As DAO.Recordset
Dim ctl As Control
Dim fld As Object
Set rst = CurrentDb.OpenRecordset(strSQL, , dbReadOnly)
If Not rst.EOF Then
For Each ctl In ctlFormName
If Not (TypeOf ctl Is Label Or TypeOf ctl Is CommandButton) Then
For Each fld In rst.Fields
If fld.Name = ctl.Name Then
ctl = rst(fld.Name)
Exit For
End If
Next
End If
Next
End If
rst.Close
Set rst = Nothing
End Function
Public Function AddData(TableName As String)
Dim rst As DAO.Recordset
Dim ctl As Control
Dim fld As Object
Set rst = CurrentDb.OpenRecordset(TableName, , dbReadOnly)
For Each ctl In ctlFormName
If Not (TypeOf ctl Is Label Or TypeOf ctl Is CommandButton) Then
For Each fld In rst.Fields
If fld.Name = ctl.Name Then
ctl = fld.DefaultValue
Exit For
End If
Next
End If
Next
rst.Close
Set rst = Nothing
End Function
Public Function SaveData(strSQL As String)
’ On Error GoTo Err_SaveData
Dim rst As DAO.Recordset
Dim ctl As Control
Dim fld As Object
If MsgBox(“您确认要保存吗?“, vbOKCancel + vbInformation, “提示!!!“) = vbOK Then
If AddTag = True Then
Set rst = CurrentDb.OpenRecordset(strSQL, , dbReadOnly)
rst.AddNew
Else
Set rst = CurrentDb.OpenRecordset(strSQL)
rst.Edit
End If
For Each ctl In ctlFormName
’Debug.Print ctl.Name
If Not (TypeOf ctl Is Label Or TypeOf ctl Is CommandButton) Then
For Each fld In rst.Fields
’Debug.Print fld.Name
If fld.Name = ctl.Name Then
rst(fld.Name) = ctl
Exit For
End If
Next
End If
Next
rst.Update
rst.Close
Set rst = Nothing
MsgBox “数据保存成功!“, vbInformation, “提示!!!“
End If
Exit_SaveData:
Set rst = Nothing
Exit Function
Err_SaveData:
If Err = 3022 Then
MsgBox “同一节点下不能存在相同的子节点,请修改后再点保存!“, vbCritical, “警告!!!“
Else
MsgBox Err.Source & “ #“ & Err & vbCrLf & vbCrLf & Err.Description, vbCritical
On Error Resume Next
End If
Resume Exit_SaveData
End Function
Public Function DelSource(Form As Form)
Dim ctl As Control
Form.RecordSource = ““
For Each ctl In Form
If Not (TypeOf ctl Is Label) Then
ctl.ControlSource = ““
End If
Next
End Function
Public Function DelData(strSQL As String)
Dim rst As New ADODB.Recordset
Dim i As Long
If MsgBox(“你确定要删除当前记录吗?“, vbOKCancel + vbQuestion, “删除!!!“) = vbOK Then
’ Debug.Print strSQL
rst.Open strSQL, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
If rst.RecordCount 》 0 Then
For i = 1 To rst.RecordCount
rst.Delete
rst.Update
rst.MoveNext
Next
End If
rst.Close
Set rst = Nothing
End If
End Function
-vb编程
vb串口通信程序实例
我也是自己摸索开发出来的每一款VBtoCOM通讯,有具体的思路如下:
给你推荐一个工具“com串口测试工具 ComTone V1.0 中文绿色版”
1、打开你的噪音计的测试软件,调整好串口号、通讯频率等等,我用温度计举例说明。
开始查询后有返回数值,这个Receive:就是返回的数值000304012200004b05,
打开串口监视精灵,监视软件的com口事件:
这里面的Write是测试软件发出的查询指令,read是设备返回的数据指令
Private Sub Command1_Click() ’发送指令
If Not MSComm1.PortOpen Then
MSComm1.CommPort = 7 ’串口为7
MSComm1.Settings = “9600,n,8,1“
MSComm1.InBufferCount = 0 ’清除接收缓冲区
MSComm1.OutBufferCount = 0 ’清除发送缓冲区
MSComm1.InputMode = comInputModeBinary ’二进制接收
MSComm1.InputLen = 0 ’读取接收缓冲区的所有字符
MSComm1.PortOpen = True ’打开串口
MSComm1.RTSEnable = False ’置为发送状态
End If
Dim pu() As Byte
Dim strdata As String
Dim crc_js() As Byte
ReDim pu(7) ’这个数组是8位的查询指令
pu(0) = “&H00“ ’温度计地址
pu(1) = “&H03“ ’查询指令
pu(2) = “&H00“ ’2、3为温度计地址
pu(3) = “&H00“ ’2、3为温度计地址
pu(4) = “&H00“ ’4、5为读取寄存器长度
pu(5) = “&H02“ ’4、5为读取寄存器长度
pu(6) = “&HC5“ ’6、7为CRC校验码 因为我们不知道设备的CRC校验规则所以用测试软件产生的校验码
pu(7) = “&HDA“ ’6、7为CRC校验码
MSComm1.Output = pu
’不做任何事情,仅仅允许其它应用程序处理它们的事件。
DoEvents
MSComm1.InBufferCount = 0 ’清除接收缓冲区
MSComm1.RThreshold = 9 ’所要接收的数据长度,我们通过COM检测精灵看到了回传数据一共是9字节
MSComm1.RTSEnable = True ’转为接收状态
End Sub
Private Sub Command2_Click()
Timer1.Enabled = False
End Sub
Private Sub Form_Unload(Cancel As Integer)
If MSComm1.PortOpen Then
MSComm1.PortOpen = False ’关闭串口
End If
Timer1.Enabled = False
End Sub
Private Sub MSComm1_OnComm() ’COM事件
Dim PA() As Byte
Dim PB As String
Select Case MSComm1.CommEvent
Case comEvReceive
MSComm1.InputLen = 0 ’读取接收缓冲区的所有字符
PB = MSComm1.Input
PA() = PB
For i = 0 To UBound(PA())
’Print “PA(“ & i & “)“; PA(i)
If Len(Hex(PA(i))) = 1 Then
strdata = strdata & “0“ & Hex(PA(i))
Else
strdata = strdata & Hex(PA(i))
End If
Next
’回传的数据串:000304012600000AC4,这9字节根据通讯协议我们进行拆分
’00为协议内回传机号,03为读命令,04为返回的数据长度 0126 为我要的温度数据为十六进制表达,下面我进行数据处理
wd = CLng(“&H“ & Left(Right(strdata, 12), 4)) / 10 & “℃“ ’根据通讯协议换算成温度
Text1 = Text1 & vbCrLf & strdata & “ “ & wd
strdata = ““
MSComm1.PortOpen = False ’关闭串口
End Select
End Sub
Private Sub Timer1_Timer()
Call Command1_Click
End Sub
这是我的测试结果。-vb编程实例
下面是通讯协议
这个是我的软件用检测精灵检测的结果
vb编程例题
题目好像不全。以下的程序是实现这样的功能:
用随机函数产生10个50以内(包括50)的随机整数存入一维数组,从键盘上输入一个数,在数组中查找这样的数,最后返回查找结果:在数组的第几个元素中找到,或是未找到这样的数。
Private Sub Command1_Click()
Dim a(10) As Integer, x As Integer
Randomize
For i = 1 To 10
a(i) = Int(Rnd() * 51)
Print a(i);
Next i
Print
x = InputBox(“请输入一个数:“)
For i = 1 To 10
If a(i) = x Then Exit For
Next i
If i 《 11 Then
Print “a(“; i; “)=“; x
Else
Print “未找到“; x
End If
End Sub
-vb编程