The simplest reliable way is to add a reference to Microsoft.VisualBasic (in Microsoft.VisualBasic.dll) and use the following into your C# or VB.Net code:
[code]
string input1 = Microsoft.VisualBasic.Interaction.InputBox("Prompt Text");
string input2 = Microsoft.VisualBasic.Interaction.InputBox("Prompt", Title
, "Default Response");
[/code]
in the above code, prompt is required where as other arguments like Title and Default Response etc. are optional.
Since above way is the simple but less reliable because that calls classic Visual Basic code whose compatibility and continuity in future are not guaranteed.
Another way that is more reliable but need one time effort is to make your own Prompt class, following code defines the prompt dialogue.
[code]
public static class Prompt
{
public static string Show(string text, string caption)
{
Form prompt = new Form();
prompt.Width = 500;
prompt.Height = 150;
prompt.FormBorderStyle = FormBorderStyle.FixedDialog;
if (caption != null)
prompt.Text = caption;
prompt.StartPosition = FormStartPosition.CenterScreen;
Label textLabel = new Label() { Left = 50, Top = 20, Text = text };
TextBox textBox = new TextBox() { Left = 50, Top = 50, Width = 400 };
Button ok = new Button() { Text = OK
, Left = 375, Top = 80, DialogResult = DialogResult.OK };
ok.Click += (sender, e) => { prompt.Close(); };
prompt.Controls.Add(textBox);
prompt.Controls.Add(ok);
prompt.Controls.Add(textLabel);
prompt.AcceptButton = ok;
return prompt.ShowDialog() == DialogResult.OK ? textBox.Text : null;
}
}
[/code]
and usage is
[code]
string promptValue = Prompt.ShowDialog("Enter your value here", "0");
[/code]
Posted Article in Programming