Skip to content

Need Input Box

CJ

Asked Dec 5, 2005, 1:09 PM EST3 answers
Share
LinkedInX
In Visual Basic, I can use an InputBox to request a variable from a user.  Is there an equivalent to the InputBox in C#.NET?

Thanks,
cj

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    I think there is no equalent control to InputBox. This InputBox is like a input box in JavaScript, right?

    Derive custom form class from Form base, add appropriate controls on it, and use it instead. Add public property, e.g. InputText, which gets the string user has submitted. There's no much work to do, and you can use it all the time when its ready, and you can event name it as InputBox ;)
    0
  • 0
    Do I just go to Project-->Add Class-->then ?

    Or do I just add a form?

    I'm sorry,  I'm not familiar with base collections or anything like that.

    Thanks,
    cj
    0
  • 0
    Project->Add->Add windows form. Visual Studio automatically derives your new windows form from Form class. New forms name will be the one you entered in the "Add windows form" window.

    Then just add Label to it in the desing view to show message to the user, and TextBox for user input, and Ok and Cancel Buttons. Then double click OK and Cancel buttons to add event handlers. When you want to close form, just add "this.Close();" to end of the event handlers. Don't call Dispose-method here! Also you wan't to set forms DialogResult property to appropriate value, for ok button event handler set it to "DialogResult.OK" and for cancel "DialogResult.Cancel".

    this.DialogResult = DialogResult.OK;
    this.Close();


    You might wan't to add overloaded ShowDialog-method, that takes user message as a parameter. This makes the use of your form more like the use of MessageBox.

    public void ShowDialog(string message)
    {
    //here set user message
    this.messageLabel.Text = message;
    //you also must call the method that actually shows the form as a dialog
    this.ShowDialog();
    }


    "messageLabel" here is the label that shows message to user.

    And finally, add public property, UsetInput that you can call after the closing of the dialog to get the text user has input. Or you can make the ShowDialog method to return string, if you wan't to make the form work like inputbox does, but i don't recommend that. You should always check the dialogresult in you main program before using the values.
    0