How can I pass the values false or true of a bool, to another form, and from the form, overwrite the bool values in my main form

JESUS EDUARDO CHAVARIN ROCHA 81 Reputation points
2022-08-03T19:51:56.817+00:00

I want to overwrite my bool values in my main form, from another from, I have a button with the action click, this one opens a form, well with this button I want to overwrite a bool of my main form

I already wrote this at the body of the action click: this.bool = true; but, nothing happens

Pd: Im a new one at programing

This is part of my code:

public bool ver1;
public bool reg1;

public void bntReg2_Click(object sender, EventArgs e)
{
frmRegistrar registrar = new frmRegistrar();
ver1 = true;
reg1 = true;
this.Hide();
registrar.ShowDialog();
this.Show();
}

Developer technologies | C++
Developer technologies | .NET | Other
Developer technologies | C#
{count} votes

1 answer

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2022-08-03T20:24:48.847+00:00

    Best way to do this is with an event. For example, in this case a child form is Form2 with a button and checkbox.

    using System;  
    using System.Windows.Forms;  
      
    namespace FormToForm  
    {  
        public partial class Form2 : Form  
        {  
            public delegate void OnPassData(bool value);  
            public event OnPassData PassEvent;  
            public Form2()  
            {  
                InitializeComponent();  
            }  
      
            private void button1_Click(object sender, EventArgs e)  
            {  
                PassEvent?.Invoke(checkBox1.Checked);  
            }  
        }  
    }  
      
    

    Main form, Form1 has a button and checkbox. We create an instance of Form2 and subscribe to the custom event.

    using System;  
    using System.Windows.Forms;  
      
    namespace FormToForm  
    {  
        public partial class Form1 : Form  
        {  
            public bool SomeBool { get; set; }  
            public Form1()  
            {  
                InitializeComponent();  
            }  
      
            private void button1_Click(object sender, EventArgs e)  
            {  
                var f = new Form2();  
                f.PassEvent += OnPassEvent;  
                f.ShowDialog();  
                f.Dispose();  
            }  
      
            private void OnPassEvent(bool value)  
            {  
                checkBox1.Checked = value;  
                SomeBool = value;  
            }  
        }  
    }  
      
    

    Full source

    If you want to pass a value from the main form to the child form here is how

    using System;  
    using System.Windows.Forms;  
      
    namespace FormToForm  
    {  
        public partial class Form2 : Form  
        {  
            private readonly bool _someBool;  
      
            public delegate void OnPassData(bool value);  
            public event OnPassData PassEvent;  
            public Form2()  
            {  
                InitializeComponent();  
            }  
      
            public Form2(bool someBool)  
            {  
                InitializeComponent();  
                _someBool = someBool;  
                checkBox1.Checked = _someBool;  
            }  
      
            private void button1_Click(object sender, EventArgs e)  
            {  
                PassEvent?.Invoke(checkBox1.Checked);  
            }  
        }  
    }  
      
    

    Main form

    using System;  
    using System.Windows.Forms;  
      
    namespace FormToForm  
    {  
        public partial class Form1 : Form  
        {  
            public bool SomeBool { get; set; }  
            public Form1()  
            {  
                InitializeComponent();  
            }  
      
            private void button1_Click(object sender, EventArgs e)  
            {  
                var f = new Form2(checkBox1.Checked);  
                f.PassEvent += OnPassEvent;  
                f.ShowDialog();  
                f.Dispose();  
            }  
      
            private void OnPassEvent(bool value)  
            {  
                checkBox1.Checked = value;  
                SomeBool = value;  
            }  
        }  
    }  
      
    
    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.