Prompt Dialog in Windows Forms
Add reference to Microsoft.VisualBasic
and use this into your C# code:
string input = Microsoft.VisualBasic.Interaction.InputBox("Prompt",
"Title",
"Default",
0,
0);
To add the refernce: right-click on the References in your Project Explorer window then on Add Reference, and check VisualBasic from that list.
There is no such thing natively in Windows Forms.
You have to create your own form for that or:
use the Microsoft.VisualBasic
reference.
Inputbox is legacy code brought into .Net for VB6 compatibility - so i advise to not do this.
You need to create your own Prompt dialog. You could perhaps create a class for this.
public static class Prompt
{
public static string ShowDialog(string text, string caption)
{
Form prompt = new Form()
{
Width = 500,
Height = 150,
FormBorderStyle = FormBorderStyle.FixedDialog,
Text = caption,
StartPosition = FormStartPosition.CenterScreen
};
Label textLabel = new Label() { Left = 50, Top=20, Text=text };
TextBox textBox = new TextBox() { Left = 50, Top=50, Width=400 };
Button confirmation = new Button() { Text = "Ok", Left=350, Width=100, Top=70, DialogResult = DialogResult.OK };
confirmation.Click += (sender, e) => { prompt.Close(); };
prompt.Controls.Add(textBox);
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.AcceptButton = confirmation;
return prompt.ShowDialog() == DialogResult.OK ? textBox.Text : "";
}
}
And calling it:
string promptValue = Prompt.ShowDialog("Test", "123");
Update:
Added default button (enter key) and initial focus based on comments and another question.
The answer of Bas can get you in memorytrouble theoretically, since ShowDialog won't be disposed. I think this is a more proper way. Also mention the textLabel being readable with longer text.
public class Prompt : IDisposable
{
private Form prompt { get; set; }
public string Result { get; }
public Prompt(string text, string caption)
{
Result = ShowDialog(text, caption);
}
//use a using statement
private string ShowDialog(string text, string caption)
{
prompt = new Form()
{
Width = 500,
Height = 150,
FormBorderStyle = FormBorderStyle.FixedDialog,
Text = caption,
StartPosition = FormStartPosition.CenterScreen,
TopMost = true
};
Label textLabel = new Label() { Left = 50, Top = 20, Text = text, Dock = DockStyle.Top, TextAlign = ContentAlignment.MiddleCenter };
TextBox textBox = new TextBox() { Left = 50, Top = 50, Width = 400 };
Button confirmation = new Button() { Text = "Ok", Left = 350, Width = 100, Top = 70, DialogResult = DialogResult.OK };
confirmation.Click += (sender, e) => { prompt.Close(); };
prompt.Controls.Add(textBox);
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.AcceptButton = confirmation;
return prompt.ShowDialog() == DialogResult.OK ? textBox.Text : "";
}
public void Dispose()
{
//See Marcus comment
if (prompt != null) {
prompt.Dispose();
}
}
}
Implementation:
using(Prompt prompt = new Prompt("text", "caption")){
string result = prompt.Result;
}