bob digby
bob digby

Reputation: 37

Global variable changing value over another class (C#)

I am new to c# and I am making a program which at the start of it has a menu button and when pressed sets a global bool variable to true. This variable is used in another forum but it changes itself to false even though it is set to true when the button is pressed

In the first forum, the global variable is true (i'm running it in debug mode).

In the second forum, the global variable has become false for an unknown reason.

This is the code for when the button is pressed on forum1 (confirmation is forum2)

private void GButton_Click(object sender, EventArgs e)
    {
        this.Green = true;
        this.Hide();
        Confirmation confirmation = new Confirmation();
        confirmation.Show();
    }

This is the code for when the if statement is ran using the global variable in the other forum (menu is forum1)

 public Confirmation()
    {
        InitializeComponent();
        Menu menu = new Menu();
        if(menu.Green == true)
        {
            //Set properties for green confirmation box
        }

Global bool variable:

public bool Green { get; set; }

How can I fix this? Any help is much appreciated.

Upvotes: 1

Views: 574

Answers (2)

John Alexiou
John Alexiou

Reputation: 29244

The accepted way of passing information between forms, is to set and get properties before and after the form is shown.

For example, if the confirmation status is stored as a checkbox of a menu item

scr1

the do the following in order to show the dialog box that sets or clears this checkmark.

// Set client confirmation box based on current check on menu item
client.Confirmation = confirmationToolStripMenuItem.Checked;
if (client.ShowDialog(this) == DialogResult.OK)
{
    // Set check on menu item based on results of confirmation dialog box
    confirmationToolStripMenuItem.Checked = client.Confirmation;
}

IAgree

Upvotes: 0

haku
haku

Reputation: 4505

What you want to do is:

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

    Confirmation confirmationForm;
    private void btnRed_OnClick(object sender, EventArgs e)
    {
        if (confirmationForm == null)
        {
            confirmationForm = new Confirmation();

            // if you need for the current Menu form to be hidden, 
            // you would need Confirmation form to be aware of it. That way
            // you can make Menu form visible when Confirmation form is
            // closed. You would need to write code in Form_Closed event.
            confirmationForm.Menu = this;

            // since you mentioned background color would be changed, 
            // if thats the only thing, you could just set that property.
            confirmationForm.BackColor = Color.Red;

            // or if you have other bunch of properties that needs 
            // to be set or logic that needs to be run, 
            // you could create a method in Confirmation
            confirmationForm.SetProperties("red");
        }

        // you may want to use ShowDialog(), so that you 
        // wont have multiple instances of confirmation being created.
        confirmationForm.Show();

        // so that it appears in the front.
        confirmationForm.BringToFront();

        this.Hide();
    }
}

In your Confirmation form, you would need to:

public partial class Confirmation : Form
{
    public Form Menu {get; set;}
    public Confirmation()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {

    }

    public void SetProperties(string color)
    {
       // do your logic here 
    }

    private void Confirmation_FormClosed(object sender, FormClosedEventArgs e)
    {
        if (Menu != null)
        {
            Menu.Show();
            Menu.BringToFront();
        }
    }
}

If I understood you correctly, this should help, else let me know in the comment. I will be happy to help you understand this.

Upvotes: 1

Related Questions