public static class Prompt
{
public static string ShowDialog(string text, string caption)
{
Form prompt = new Form();
prompt.Width = 435;
prompt.Height = 122;
prompt.FormBorderStyle = FormBorderStyle.FixedDialog;
prompt.Text = caption;
prompt.StartPosition = FormStartPosition.CenterScreen;
Label textLabel = new Label() { Left = 10, Top=10, Width=400, Text=text };
TextBox textBox = new TextBox() { Left = 10, Top=30, Width=400 };
Button confirmation = new Button() { Text = "Ok", Left=300, Width=100, Top=52 };
confirmation.Click += (sender, e) => { prompt.Close(); };
prompt.Controls.Add(textBox);
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.AcceptButton = confirmation;
prompt.ShowDialog();
return prompt.ShowDialog() == DialogResult.OK ? textBox.Text : null;
}
}
I created this class, so that if the user clicks the ok button it returns the value of the textbox, but if they click close it returns null. It works, the only problem is that both the red x and ok button need to be clicked twice in order for the form to actually close. How can I fix this?