Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return a value only when "ok" is clicked

I have created a custimazibale prompt

public static class Prompt
{
    public static string ShowDialog(int columnnumber, string columnname)
    {
        Form prompt = new Form();
        prompt.Width = 500;
        prompt.Height = 150;
        prompt.FormBorderStyle = FormBorderStyle.FixedDialog;
        prompt.Text = columnname;
        prompt.StartPosition = FormStartPosition.CenterScreen;
        Label textLabel = new Label() { Left = 50, Top = 20 };
        ComboBox comboBox = new ComboBox() { Left = 50, Top = 50, Width = 400 };
        comboBox.Items.AddRange(new string[] { "a","b","c" });
        comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
        comboBox.SelectedItem = columnname;
        Button confirmation = new Button() { Text = "Ok", Left = 350, Width = 100, Top = 80 };
        confirmation.Click += (sender, e) => { prompt.Close(); };
        textLabel.Text = "Colonne " + (columnnumber + 1).ToString() + " : " + columnname;
        prompt.Controls.Add(comboBox);
        prompt.Controls.Add(confirmation);
        prompt.Controls.Add(textLabel);
        prompt.AcceptButton = confirmation;
        prompt.ShowDialog();
        prompt.AcceptButton = confirmation;

        return comboBox.Text;
    }
}

then I call it in my main form when a header is clicked

private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
     dt.Columns[e.ColumnIndex].ColumnName = Prompt.ShowDialog(e.ColumnIndex, dataGridView1.Columns[e.ColumnIndex].Name);
}

The problem is my text change even if the button close is clicked. But I want it to change only when the user click the button "OK".

like image 769
raz_user Avatar asked Jan 18 '26 16:01

raz_user


2 Answers

You could evaluate the DialogResult and return null if it is not OK:

public static class Prompt
{
    public static string ShowDialog(int columnnumber, string columnname)
    {
        using (Form prompt = new Form())
        {
            // other code
            return prompt.DialogResult == DialogResult.OK ? comboBox.Text : null;
        }
    }
}

and then in your other method:

private void dataGridView1_ColumnHeaderMouseClick(object sender, EventArgs e)
{
    var result = Prompt.ShowDialog(e.ColumnIndex, 
                                   dataGridView1.Columns[e.ColumnIndex].Name);
    if (result != null)
        dt.Columns[e.ColumnIndex].ColumnName = result;
}

And inside your prompt you should set the DialogResult accordingly:

confirmation.Click += (sender, e) =>
    {
        prompt.DialogResult = DialogResult.OK;
        prompt.Close();
    };

HINT: Instead of result != null you could also use !String.IsNullOrWhiteSpace(result) to only update the column name if something was entered.

like image 70
Christoph Fink Avatar answered Jan 21 '26 05:01

Christoph Fink


I would go for something like this:

using(Form prompt = new Form())
{
    //Initialize the components of your form

    DialogResult result = prompt.ShowDialog();
    if(result == DialogResult.OK)
    {
       //return whatever it is you want to return
    }
}

Inside your form you can set the DialogResult via prompt.DialogResult = DialogResult.OK and some more options (DialogResult.Cancel, DialogResult.Retry etc.)

like image 24
Paul Weiland Avatar answered Jan 21 '26 04:01

Paul Weiland