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
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
AnttiPosted Dec 6, 2005, 11:53 AM
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.
CJPosted Dec 6, 2005, 10:23 AM
Or do I just add a form?
I'm sorry, I'm not familiar with base collections or anything like that.
Thanks,
cj
AnttiPosted Dec 5, 2005, 7:03 PM
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 ;)