如何区分 InputBox Cancel 和 OK 按钮? [英] How to distinguish InputBox Cancel from OK button?
问题描述
快速问题:
我在 C# 代码中使用 Microsoft.VisualBasic.Interaction.InputBox
来允许用户将网站添加到列表中,但我不希望他们输入空字符串,所以我如果发生这种情况,请给出错误弹出窗口.但是,如果用户按取消",错误也会弹出,这是我不希望发生的.
I'm using a Microsoft.VisualBasic.Interaction.InputBox
in my C# code to allow users to add websites to a list, but I don't want them to enter an empty string so I give an error popup in case that happens. However, the error will also pop up if the user presses "cancel", which I don't want to happen.
阅读它的文档说按取消"会返回一个空字符串,因此它会触发错误.有没有办法仍然定义用户是用空字符串按下确定"还是取消"?
Reading the documentation on it says that pressing "cancel" returns an empty string, hence why it fires the error. Is there a way to still define wether the user pressed "okay" with an empty string or "cancel"?
提前致谢,
-彼得
推荐答案
你不能这样做.来自 MSDN
如果用户单击取消,则返回零长度字符串.
Dim result As String = Microsoft.VisualBasic.InputBox("Enter some text")
If result.Length > 0 Then
Debug.WriteLine("Something entered and OK Clicked")
Else
Debug.WriteLine("Cancel Button Clicked OR Blank String Entered and OK Clicked")
End If
最简单的解决方案就是创建自己的简单输入表单并测试 DialogResult
值
The easiest solution is just to create your own simple input form and test the DialogResult
value
这篇关于如何区分 InputBox Cancel 和 OK 按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!