设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

123下一页
返回列表 发新帖
查看: 6382|回复: 26
打印 上一主题 下一主题

[宏/菜单/工具栏] [分享]一个判断用户是否按下了InputBox的取消按钮的例子

[复制链接]
跳转到指定楼层
1#
发表于 2006-10-27 05:24:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
游客,如果您要查看本帖隐藏内容请回复

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
2#
发表于 2006-10-27 07:04:00 | 只看该作者
请说明一下在什么场合能派上用场?
3#
发表于 2006-10-27 16:45:00 | 只看该作者
Cancel = Not CBool(StrPtr(str))

这一句不理解。
4#
 楼主| 发表于 2006-10-27 17:35:00 | 只看该作者
以下是引用一点通在2006-10-26 23:04:00的发言:


请说明一下在什么场合能派上用场?

     对inputbox 的用法相信大家并不陌生,也经常会用到,但是可否留意用inputbox传递变量,有时候在处理这个变量时按取消会出错,或者就把当它作为一个if ..else  来用吧, if 按确定 就.....否则 就....
5#
 楼主| 发表于 2006-10-27 17:50:00 | 只看该作者
以下是引用wuaza在2006-10-27 8:45:00的发言:


Cancel = Not CBool(StrPtr(str))

这一句不理解。



   StrPtr
Strings in Visual Basic are stored as BSTR's. If you use the VarPtr on a variable of type String, you will get the address of the BSTR, which is a pointer to a pointer of the string. To get the address of the string buffer itself, you need to use the StrPtr function. This function returns the address of the first character of the string. Take into account that Strings are stored as UNICODE in Visual Basic.

To get the address of the first character of a String, pass the String variable to the StrPtr function.


[此贴子已经被作者于2006-10-27 9:51:15编辑过]

6#
发表于 2006-10-27 17:59:00 | 只看该作者
andymark的例子改成了函数,方便大家调用:

Type InputInfo
    Value As String
    Buttons As Integer
End Type

Public Function MyInputBox(Message As String, Optional Title As String = "请输入参考值:", Optional Default As String = "") As InputInfo
    Dim strValue As String
    Dim blnCancel As Boolean
        strValue = InputBox(Message, Title, Default)
        blnCancel = CBool(StrPtr(strValue))
        If blnCancel Then
          MyInputBox.Buttons = vbOK
          MyInputBox.Value = strValue
        Else
          MyInputBox.Buttons = vbCancel
          MyInputBox.Value = ""
        End If
End Function

调用例子:

Private Sub Command1_Click()
    Dim intButtons As Integer
    Dim MyInfo As InputInfo
    MyInfo = MyInputBox("你好")
        intButtons = MyInfo.Buttons
    If intButtons = vbOK Then
       MsgBox "你按了'确定'键"
       MsgBox "你输入的是:" & MyInfo.Value
    Else
       MsgBox "你按了'取消'键"
    End If
End Sub

[此贴子已经被作者于2006-10-27 10:16:17编辑过]

7#
发表于 2006-10-27 18:04:00 | 只看该作者
网络上搜索的一些关于StrPtr的一些资料。贴在这里供大家参考:

StrPtr

该函数主要用来产生高效的UNICODE API调用。在VB4,UNICODE形式的API函数的调用必须借助于Byte数组,例如:

Declare Sub MyUnicodeCall Lib "MyUnicodeDll.dll" (pStr as Byte)

Sub MakeCall (MyStr as String)

Dim bTmp() as Byte
bTmp=MyStr & vbNullChar
MyUnicodeCall bTmp(0)
MyStr=bTmp
MyStr=left(MyStr, Len(MyStr)-1)

End Sub

如果使用StrPtr,上面的代码精简为:

Declare Sub MyUnicodeCall Lib "MyUnicodeDll.dll" (pStr as Byte)

Sub MakeCall (MyStr as String)

MyUnicodeCall StrPtr(MyStr)

End Sub

VarPtr/StrPtr/ObjPtr的执行速度非常非常快,因此调用UNICODE函数所赞成有系统负担实际上小于调用相对应的ANSI函数。因为前者不需进行转换。

StrPtr还能用于优化ANSI API函数的调用。在调用时使用StrConv和StrPtr就能避免将一个字符串变量多资传递给函数以及为每个调用而执行转换操作所造成的系统负担。例如原来的:

Declare Sub MyAnsiCall Lib "MyAnsiDll.dll" (ByVal pStr As String)

MyAnsiCall MyStr

现在变为:

Declare Sub MyAnsiCall Lib "MyAnsiDll.dll" (ByVal pStr As Long)

MyStr=StrConv(MyStr,vbFromUnicode)
MyAnsiCall StrPtr(MyStr)
MyStr=StrConv(MyStr,vbUnicode) 注释:并不总是要求

StrPtr还是唯一能直观地告诉你空字符串和null字符串的不同的方法。对于null字符串(vbNullString),StrPtr的返回值为0,而对于空字符串,函数的返回值为非零。
8#
发表于 2007-8-30 09:56:48 | 只看该作者
又没钱了,为了下载要回复,,,,
9#
发表于 2007-8-30 11:10:35 | 只看该作者
正好
我现在就能用到这个
谢谢
10#
发表于 2008-5-5 20:47:21 | 只看该作者
[:34] [:34] [:34] [:34]
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-5-24 16:58 , Processed in 0.121126 second(s), 35 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表