VisualBasic InputBox Cancel

99封情书 提交于 2019-12-12 11:13:02

问题


Quick question:

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"?

Thanks in advance,

-Peter


回答1:


You can't do this. From 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 DialogResult value




回答2:


string a;
            a = Interaction.InputBox("message", "title");
            if (a.Length > 0)
            {
                comboBox2.Items.Add(a);
                // ok
            }
            else
            {
                // cancel
            }


来源:https://stackoverflow.com/questions/19679489/visualbasic-inputbox-cancel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!