-1

Form1 has the following code :

 private void deleteMem_Click(object sender, EventArgs e)
        {
            bool delete = false;
            choiceCheck choiceCheck = new choiceCheck(ref delete,"Supprimer le membre", "Êtes-vous sûr de vouloir supprimer le membre (" + CDMDisplay.Text + ") " + FNDisplay.Text + " " + LNDisplay.Text, "Supprimer", "Annuler");
            choiceCheck.ShowDialog();

            if (!delete) return;
...

and Form2 has :

        public bool accept;
        public choiceCheck(ref bool accept, string title, string message, string yesText, string noText)
        {

            InitializeComponent();
            this.Text = title;
            this.message.Text = message;
            this.yesButton.Text = yesText;
            this.noButton.Text = noText;
            this.accept = accept;
        }
        private void yesButton_Click(object sender, EventArgs e)
        {
            accept = true;
            this.Close();
        }

        private void noButton_Click(object sender, EventArgs e)
        {
            accept = false;
            this.Close();
        }

Form2 is like MessageBoxButtons.YesNo but as a Form. I want to pass ref of delete to Form2 and have it alter its value.

I could make 'bool delete' a public variable and then alter it from Form2 as a solution, but is there any way to alter variable delete without declaring it public ?

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Salmy
  • 47
  • 4
  • 2
    ShowDialog returns a DialogResult value. This value is obtained from the DialogResult property of the buttons. So, if you just ask for a yes-no answer there is no need to pass a ref variable to the form. Just look at the DialogResult returned by ShowDialog and you will be able to understand which button has been pressed. – Steve Jan 21 '23 at 20:36
  • You don't need to use a ref variable here. Just add a public property that returns `accept`. (Or use the existing `DialogResult` property) – PMF Jan 21 '23 at 20:38
  • Yes that will work, albeit is a bit confusing having everything on the same line. See here: [Using DialogResult correctly](https://stackoverflow.com/questions/16846573/using-dialogresult-correctly) – Steve Jan 21 '23 at 20:49

1 Answers1

-1

That is not at all how this pattern is supposed to work. You need to either use DialogResut to return a yes/no, or have a property available with the result when the form closes.

MainForm.cs

For example, here is the main form which calls the confirmation form and depending on the DialogResult value it branches one way or another.

public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        ConfirmForm dlg = new ConfirmForm()
        {
            Title = "Confirm Membership",
            Message = "By accepting this membership you ...",
            YesText = "Confirm",
            NoText = "Cancel",
        };

        if (dlg.ShowDialog() == DialogResult.Yes)
        {
            // accepted
            label1.Text = "Accepted";
        }
        else
        {
            // canceled
            label1.Text = "Canceled";
        }
    }
}

ConfirmForm.cs

and here is the confirmation form with the logic to handle setting the text labels and setting the proper value in DialogResult

public partial class ConfirmForm : Form
{
    public ConfirmForm()
    {
        InitializeComponent();
    }
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        this.AcceptButton = yesButton;
        this.CancelButton = noButton;

        yesButton.Click += (s, ev) =>
        {
            DialogResult = DialogResult.Yes;
            this.Close();
        };
        noButton.Click += (s, ev) =>
        {
            DialogResult = DialogResult.No;
            this.Close();
        };
    }
    public string Message { 
        get => messageLabel.Text; 
        set => messageLabel.Text = value; }
    public string YesText
    {
        get => yesButton.Text;
        set => yesButton.Text = value;
    }
    public string NoText { 
        get => noButton.Text; 
        set => noButton.Text = value; }
    public string Title { 
        get => Text; 
        set => Text = value; }
}
John Alexiou
  • 28,472
  • 11
  • 77
  • 133