如何替换IF中的表达式

fenqing 2003-10-11 11:50:29
Private Sub Command1_Click()
If Text1.Text Then
MsgBox "test"
End If
End Sub

Private Sub Form_Load()
a = 1
End Sub


在 Text1 里输入 a=1 ,然后点击 command1 ,报错,说类型不匹配
...全文
58 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zlpanzy 2003-10-12
  • 打赏
  • 举报
回复
还需外部过程
fenqing 2003-10-12
  • 打赏
  • 举报
回复
还有其他办法吗?难道VB内部没有把字符串转换成表达式的函数?
cbool是把表达式转换成字符串用的
daviddivad 2003-10-12
  • 打赏
  • 举报
回复
您可以利用Microsoft Script Control来实现这个功能。

首先,你需要在工程里引用一下Microsoft Script Control。然后加入一下代码:

Private Sub command1_click()

Dim scr As New ScriptControl
Dim mycode As String

scr.Language = "vbscript"
mycode = "public function Test()" + vbCrLf
mycode = mycode + "if " + text1.text + "then" + vbcrlf
mycode = mycode + "MsgBox ""test""" + vbCrLf
mycode = mycode + "End if" + vbcrlf
mycode = mycode + "end function"

scr.AddCode (mycode)
scr.Eval ("Test()")

End Sub

qiqunet 2003-10-11
  • 打赏
  • 举报
回复
Private Sub Command1_Click()
If Text1.Text<>"" Then
MsgBox "test"
End If
End Sub
daviddivad 2003-10-11
  • 打赏
  • 举报
回复
if val(text1.text)>0 then

or

if cbool(text1.text) then

7,789

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧