vb.net VisualBasic 输入框取消
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19679489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VisualBasic InputBox Cancel
提问by Voidpaw
Quick question:
快速提问:
I'm using a Microsoft.VisualBasic.Interaction.InputBoxin 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.
我Microsoft.VisualBasic.Interaction.InputBox在我的 C# 代码中使用 a来允许用户将网站添加到列表中,但我不希望他们输入空字符串,因此我会在发生这种情况时给出错误弹出窗口。但是,如果用户按“取消”,错误也会弹出,这是我不希望发生的。
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"?
阅读它的文档说按“取消”会返回一个空字符串,因此它会触发错误。有没有办法仍然定义用户是用空字符串按下“确定”还是“取消”?
Thanks in advance,
提前致谢,
-Peter
-彼得
回答by Matt Wilko
You can't do this. From MSDN
你不能这样做。来自MSDN
If the user clicks Cancel, a zero-length string is returned.
如果用户单击取消,则返回长度为零的字符串。
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
The easiest solution is just to create your own simple input form and test the DialogResultvalue
最简单的解决方案就是创建自己的简单输入表单并测试DialogResult值
回答by Alfath Nur Muttaqin
string a;
a = Interaction.InputBox("message", "title");
if (a.Length > 0)
{
comboBox2.Items.Add(a);
// ok
}
else
{
// cancel
}

