![](/img/trans.png)
[英]How to check if Cancel button or OK button is pressed in InputBox
[英]How to distinguish InputBox Cancel from OK button?
快速提问:
我在我的 C# 代码中使用Microsoft.VisualBasic.Interaction.InputBox
以允许用户将网站添加到列表中,但我不希望他们输入空字符串,因此我会弹出错误消息以防万一。 但是,如果用户按下“取消”,也会弹出错误,这是我不希望发生的。
阅读上面的文档说按“取消”会返回一个空字符串,因此它会触发错误。 有没有办法仍然定义用户是用空字符串按下“okay”还是“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
值
string a;
a = Interaction.InputBox("message", "title");
if (a.Length > 0)
{
comboBox2.Items.Add(a);
// ok
}
else
{
// cancel
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.