比如text1每隔一个周期收到一个数据,text2中保存最大值,text3中保存最小值,怎么实现啊高手们??
我的代码问题好像大大的存在!!
Private Sub Command1_Click()
Text2.Text = Val(Text1.Text)
Text3.Text = Val(Text1.Text)
If Text2.Text > Text1.Text Then Text2.Text = Text2.Text Else Text2.Text = Text1.Text
If Text3.Text < Text1.Text Then Text3.Text = Text3.Text Else Text3.Text = Text1.Text
End Sub
我的代码问题好像大大的存在!!
Private Sub Command1_Click()
Text2.Text = Val(Text1.Text)
Text3.Text = Val(Text1.Text)
If Text2.Text > Text1.Text Then Text2.Text = Text2.Text Else Text2.Text = Text1.Text
If Text3.Text < Text1.Text Then Text3.Text = Text3.Text Else Text3.Text = Text1.Text
End Sub
Dim max As Single
Dim temp As Single
Dim notfirst As Boolean
Private Sub Timer1_Timer()
Randomize
Text1.Text = Rnd(100)
temp = Val(Text1.Text)
If notfirst = False Then
min = temp: max = temp: notfirst = True
Text3.Text = min: Text2.Text = max
Else
If min > temp Then min = temp: Text3.Text = min
If max < temp Then max = temp: Text2.Text = max
End If
End Sub按这个思路作吧,你的思路全错了
用text1的Change()事件不就轻松搞定了吗!何苦用Timer事件!浪费资源………………
If Text2.text < Text1.text Then Text2.text = Text1.text
If Text3.text > Text1.text Then Text3.text = Text1.text
End Sub
不会用text1的Change()事件,能否进一步说明啊
Private Sub Timer1_Timer()
Randomize
Text1.Text = Rnd(100)
改成
你的
text1每隔一个周期收到一个数据
Private Sub Text1_Change() temp = Val(Text1.Text)
If notfirst = False Then
min = temp: max = temp: notfirst = True
Text3.Text = min: Text2.Text = max
Else
If min > temp Then min = temp: Text3.Text = min
If max < temp Then max = temp: Text2.Text = max
End IfEnd Sub
text1中输入数据text2、text3 同步出来啊,不会出来大小值
Private Sub Command1_Click()
If Text2.Text < Text1.Text Then Text2.Text = Text1.Text
If Text3 = "" Then
Text3 = Text1
ElseIf Text3 > Text1 Then
Text3 = Text1
End If
End SubPrivate Sub Form_Load()
Text3 = ""
End Sub
If Text2.Text < Text1.Text Then Text2.Text = Text1.Text
If Text3 = "" Or Text3 > Text1 Then Text3 = Text1
End SubPrivate Sub Form_Load()
Text3 = ""
End Sub
还有点问题
我的测试过程 ,往text1 输入数值, 点击Command1 ,然后text2、text3显示数值
text1 text2 text3100 100 100 正确
200 200 100 正确
50 50 100 错误
20 50 100 错误
Text3 = Text1
ElseIf Val(Text3.Tag) => temp Then
Text3 = Text1
Enf If If Val(Text4) < temp Then Text4 = Text1End Sub
Private Sub Text1_Change()
Static blnNotFirst As Boolean
If Not blnNotFirst Then Text2.Text = Text1.Text: Text3.Text = Text1.Text: blnNotFirst = True
If Val(Text2.Text) < Val(Text1.Text) Then Text2.Text = Text1.Text
If Val(Text3.Text) > Val(Text1.Text) Then Text3.Text = Text1.Text
End Sub
未等数据输完text3中就出来数据了,假如text1输入100 ,text3=1
你的 Text1 不是数据接收端口吗?不会是键盘键入吧?如果是键盘键入,建议在键入完成后,敲回车表示输入完成,用下面代码:Private Sub Text1_KeyPress(keyascii As Integer)If keyascii = 13 Then
temp = Val(Text1.Text) If Text3 = "" Then
Text3 = Text1
ElseIf Val(Text3.Tag) => temp Then
Text3 = Text1
Enf If If Val(Text4) < temp Then Text4 = Text1 keyascii = 0
End If
End Sub下次记得把需求说清楚。
如果仍然是数据接收用(接收代码中 Text1 = ...),那就还是用原来代码,调试的时候用 Paste 贴入 Text1 中的值,不要键盘敲。
即使是 Timer 方式,也有可能在你敲键盘时碰巧触发。只要你输入长度不定,软件就无法知道你何时输入完成。
Option Explicit
'新建标准EXE工程
'加入 Picture1、Text1、Text2、Text3
'不要更改控件名称
Private x&, max&, min&, vcur&, vsav&, c&Private Sub Form_Initialize()
'界面初始化
Height = 5100: Width = 8040
Picture1.ScaleMode = 3: Picture1.AutoRedraw = True
Picture1.Left = 8: Picture1.Width = 513: Picture1.Height = 260
Text1.Left = 16: Text1.Height = 25: Text1.Width = 136: Text1.Locked = True
Text2.Left = 176: Text2.Height = 25: Text2.Width = 136: Text2.Locked = True
Text3.Left = 352: Text3.Height = 25: Text3.Width = 136: Text3.Locked = True
Timer1.Interval = 500: c = 0: x = 5: Randomize
max = 0: min = 200: vcur = 100: vsav = 130
End SubPrivate Sub Timer1_Timer()
Dim dx&, dy&
c = c + 1
If (c > 100) Then Timer1.Enabled = False: Exit Sub
If (c = 1) Then Picture1.Line (5, 130)-(507, 130), vbBlue
vcur = vcur + Int(Rnd() * 40) - 20
If (vcur > 200) Then vcur = 200
If (vcur < 0) Then vcur = 0
dx = c * 5: dy = 230 - vcur
Picture1.Line (x, vsav)-(dx, dy), vbRed
vsav = dy: x = dx
'****************************************
Text1.Text = vcur ' 模拟 Text1接收数据
End SubPrivate Sub Text1_Change()
' 真正解决楼主问题的代码就这么点:
Dim t&
t = CLng(Text1)
If (t > max) Then max = t: Text2 = "最大值:" & t
If (t < min) Then min = t: Text3 = "最小值:" & t
End Sub
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'过程功能:装载主窗体
'功能描述:初始化设置Text1、Text2、Text3,Text2:最小值、Text3:最大值
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Private Sub Form_Load()
On Error GoTo errSub
Text1.Text = "0.00"
Text2.Text = "5.00" '初始化最小值
Text3.Text = "0.00" '初始化最大值
Exit Sub
errSub:End Sub
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'过程功能:定时器事件
'功能描述:定时触发执行,接受数据
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Private Sub Timer1_Timer()
Dim dblP As Double
On Error GoTo errSub
dblP = funGetData
Text1.Text = Format(dblP, "0.00")
If Val(Text2.Text) > dblP Then
Text2.Text = Format(dblP, "0.00")
ElseIf Val(Text3.Text) < dblP Then
Text3.Text = Format(dblP, "0.00")
End If
Exit Sub
errSub:End Sub
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'函数功能:模拟数据采集函数
'功能描述:假设采集的数据的范围是:0~5.0
'返 回 值:Double类型数据
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Private Function funGetData() As Double
On Error GoTo errFun
Randomize
funGetData = Rnd * 5# '模拟返回的数据
Exit Function
errFun:End Function