Search code examples
c#buttoninputbox

What button pressed inputbox C#


I am using the input box from visual basic in c# and I couldn't figure out how I know what button has been pressed. The input box return the string that has been written. How I know if the cancel button has been clicked or the OK button?

Thank you very much for the help, I didn't find the answer :)

This is what I tried:

string notineName = Interaction.InputBox("Enter the notice name:", "Enter notice name", "");

If you have another way to do input box ( I wanted to make my own but I don't know how to return what button has been clicked) please write it here.


Solution

  • As an alternative you could use dialog boxes.

    InputDialog dialog = new InputDialog("Caption Here", "Label Text Here", "Default Textbox String");
    if (dialog.ShowDialog() == DialogResult.OK)
    {
        string result_text = dialog.ResultText;
        // use result_text...
    }
    else
    {
        // user cancelled out, do something...
    }
    

    Here an enum result determines what button was selected.