SamKr
6/11/2015 - 12:41 PM

Input Dialogbox

Input Dialogbox

public static DialogResult ShowInputDialog(string message, ref string input, bool password = false)
{
    try
    {
        var size = new Size(400, 70);
        var inputBox = new Form { FormBorderStyle = FormBorderStyle.FixedDialog, ClientSize = size, Text = message, StartPosition = FormStartPosition.CenterScreen };

        var textBox = new TextBox { Size = new Size(size.Width - 10, 23), Location = new Point(5, 5), Text = input };

        if (password) { textBox.PasswordChar = Convert.ToChar("*"); }

        inputBox.Controls.Add(textBox);

        var okButton = new Button
        {
            DialogResult = DialogResult.OK,
            Name = "okButton",
            Size = new Size(75, 23),
            Text = "&OK",
            Location = new Point(size.Width - 80 - 80, 39)
        };
        inputBox.Controls.Add(okButton);

        var cancelButton = new Button
        {
            DialogResult = DialogResult.Cancel,
            Name = "cancelButton",
            Size = new Size(75, 23),
            Text = "&Cancel",
            Location = new Point(size.Width - 80, 39)
        };
        inputBox.Controls.Add(cancelButton);

        inputBox.AcceptButton = okButton;
        inputBox.CancelButton = cancelButton;

        var result = inputBox.ShowDialog();
        input = textBox.Text;
        return result;
    }
    catch 
    {
        input = "";
        return DialogResult.Cancel;
    }
}