注册 登录
编程论坛 VB6论坛

减法---求助打算看下哪里错误了

hh0796 发布于 2018-12-13 13:51, 1437 次点击
求助打算看下哪里错误了

Private Sub Command1_Click()

Dim x1 As Single
Dim x2 As Single
Dim x3 As Single
Dim x4 As Single

Text5.Text = Text3.Text - Text2.Text
Text6.Text = Text4.Text - Text1.Text

End Sub

Private Sub Text1_Change()
Text1.Text = Str(x1)
End Sub

Private Sub Text2_Change()
Text2.Text = Str(x2)
End Sub

Private Sub Text3_Change()
Text3.Text = Str(x3)
End Sub

Private Sub Text4_Change()
Text4.Text = Str(x4)
End Sub




只有本站会员才能查看附件,请 登录
7 回复
#2
icecool2018-12-13 14:24
你的x1,x2,x3,x4都没有定义赋值怎么可以拿出来用?
#3
wds12018-12-13 15:12
这么写就行了。
text1-text4直接输入数据
Private Sub Command1_Click()
  Text5.Text =Text3.Text - Text2.Text'或者Text5.Text =val(Text3.Text)- val(Text2.Text)
  Text6.Text =Text4.Text - Text1.Text
End Sub
#4
hh07962018-12-13 15:32
回复 2楼 icecool
刚开始学,菜鸟
#5
hh07962018-12-13 15:37
回复 3楼 wds1
多谢大神!调试OK
#6
icecool2018-12-13 16:12
以下是引用hh0796在2018-12-13 15:32:25的发言:

刚开始学,菜鸟

知道你是刚开始学,说一下根本原因,好让你自已思考
这是最基本的东西,你运行时x1~x4都没有赋值,str()后都是0
而四个文本框的Change()事件中你设定的text1.text=str(x1)....text4.text=str(x4)即是令4个文本框在你输入任何字符时都会变成0
最后text5\text6通过计算也是0了。
#7
hh07962018-12-13 16:19
回复 6楼 icecool
多谢
#8
ZHRXJR2018-12-14 10:10
程序代码:
Dim x1 As Single
Dim x2 As Single
Dim x3 As Single
Dim x4 As Single   'x1   x4变量设置为程序级变量

Private Sub Command1_Click()
Text5.Text = Format((Val(Text3.Text) - Val(Text2.Text)), "0.000")   '文本框数据必须转换为数字
Text6.Text = Format((Val(Text4.Text) - Val(Text1.Text)), "0.000")   '计算结果并且格式化
End Sub

Private Sub Form_Load()
x1 = 25.4: x2 = 22.5: x3 = 22.7: x4 = 25.41     '在启动时给变量赋值
Text1.Text = x1: Text2.Text = x2: Text3.Text = x3: Text4.Text = x4   '将变量的值赋值给个文本框
End Sub
1