views:

1428

answers:

3

I'm looking to have a simple custom dialog box, like a message box, that has a label and a TextBox. If there's a simple way to do this, sorry! I'm really not well versed in the dialog stuff.

Thanks for any help, guys!

A: 

I'm assuming you basically want a custom dialog box that returns a string entered by the user. One way is to add a reference to Microsoft.VisualBasic to your project, which gives you access to the InputBox method, which is basically a message box with a text box on it. But that's no fun and I'm not sure it would work on a smartphone anyway.

To roll your own, you just add a form (named CustomDialog) to your project and drag a textbox (textBox1), a label (label1), and a button (labeled "OK") onto it.

To set the label text, add a parameter to the form's constructor like this:

public CustomDialog(string textCaption)
{
    label1.Text = textCaption;
}

To expose the entered text to the caller, add this code to the form:

public override string Text
{
    get
    {
        return textBox1.Text;
    }
}

In the OK button's click event, put this code:

this.DialogResult = DialogResult.OK; // this will close the form, too

To use this dialog from your main form, you create an instance of this form, show it, check to see that the OK button was clicked, and then read its Text property (which returns what the user entered) like so:

using (CustomDialog dialog = new CustomDialog("What is your name"))
{
    if (dialog.ShowDialog(this) == DialogResult.OK)
    {
        string enteredText = dialog.Text;
    }
}

You can get fancier, but those are the basics.

MusiGenesis
+4  A: 
MusiGenesis
A: 

If you want a super-simple but[t] ugly solution, you can include a reference in your project to Microsoft.VisualBasic, which lets you use the VB function InputBox like this:

string s = Microsoft.VisualBasic.Interaction.InputBox("prompt text",
    "title text", "default value", 0, 0);

The dialog takes up the entire screen, but is simple to use. But is incredibly ugly, as I mentioned.

MusiGenesis