当用户按下InputBox的“取消(Cancel)”按钮,返回的字符串是vbNullString。如果用户输入0长度的字符串,然后按下“确定(OK)”按钮,返回的是空字符串“”。不幸的是,在VB里我没无法比较“”和vbNullString,因为他们在VB中相等(虽然实际上他们非常不同)。
但是用一个非官方函数可以解决这个问题:StrPtr,返回字符串的地址(VB.net中没有了)
(BTW:类似的函数还有VarPtr,ObjPtr)
因为vbNullString的指针被定义为0,而“”不是。
Dim strReturn As String
strReturn=InputBox("Enter a value")
If StrPtr(strReturn)=0 Then
'用户取消操作
End If
在VB.net要想实现VarPtr就要自己来写了:
Module Module1
Public Function VarPtr(ByVal o As Object) As Integer
Dim GC As System.Runtime.InteropServices.GCHandle = System.Runtime.InteropServices.GCHandle.Alloc(o, System.Runtime.InteropServices.GCHandleType.Pinned)
Dim ret As Integer = GC.AddrOfPinnedObject.ToInt32
GC.Free()
Return ret
End Function
End Module
打印 | 张贴于 2003-12-04 10:35:00 | Tag:暂无标签
留言反馈
Private Sub Command1_Click()
Dim str As String
Dim Cancel As Boolean
str = InputBox("")
Cancel = Not CBool(StrPtr(str))
MsgBox Cancel
End Sub
ayayiya@163.com
GCHandle gch=new GCHandle(ret);
我这样后ret的地址就不会变了吗?
你好!
我没说清楚,我上面指的并不是在VB.net中而是VB6中,VB.net的代码只是谈到VARPTR函数顺便一题,而且我的目的并不是实际应用,而仅仅为说明一种思路。
其实不应该在这些,有把这里作为论坛的嫌疑!