vb问题~错在哪儿了?

来源:百度知道 编辑:UC知道 时间:2024/06/21 00:50:36
Dim x As Single, y As Single, c As Single
Text1.Text = x
Text2.Text = y
Text3.Text = c
c = x + y
三个textbox,两个命令按钮,输入两个数值a和b,求a+b

Dim x As Single, y As Single, c As Single
x =val(Text1.Text)
y =val(Text2.Text)
c = x + y
Text3.Text = cstr(c)
这样写才对。

Text1,Text2,Text3 是啥? Label, TextBox?

你要得到什么效果呢?

请把你想做什么说明白点!还有Text1.Text=x 你想写赋值语句的话应该写成x=Text1.Text。发邮件到1083421197@qq.com告诉我你想写什么吧,也许我能帮你!

你反了吧。
x = Val(Text1.Text)
y = Val(Text2.Text)
c = x+y
Text3.Text = CStr(c)

当然上面四句你可以直接这样写:
Text3.Text = CStr(Val(Text1.Text)+Val(Text2.Text))

========================

Val:任意转数字
CStr:任意转字符(常用于数字转字符因为这样不会多出来空格,如果你用Text3.Text = c则会多出空格,例如c是1.0那么Text3.Text是" 1.0 "。Cstr可以避免这个问题

Dim x As Single, y As Single, c As Single
Private Sub Command1_Click()
x=val(text1.text)
y=val(text2.text)
c=x + y
text3.text = c
End Sub
val字符串 转 数值型