全局变量在另一个类上的更改值(C#)

时间:2018-11-15 22:44:50

标签: c#

我是C#的新手,我正在制作一个程序,该程序的开头有一个菜单按钮,按下后会将全局bool变量设置为true。此变量已在其他论坛中使用,但即使在按下按钮时将其设置为true,它也会更改为false

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.

这是在forum1(确认为forum2)上按下按钮时的代码

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

这是使用另一个论坛(菜单为forum1)中的全局变量运行if语句时的代码

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

全局布尔变量:

public bool Green { get; set; }

我该如何解决?我们非常感谢您的任何帮助。

2 个答案:

答案 0 :(得分:1)

您想要做的是:

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();
    }
}

在确认表单中,您需要:

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();
        }
    }
}

如果我正确地理解了您的意见,这应该会有所帮助,否则请在评论中让我知道。我很乐意帮助您了解这一点。

答案 1 :(得分:0)

在表单之间传递信息的公认方法是在显示表单之前和之后设置和获取属性。

例如,如果确认状态存储为菜单项的复选框

scr1

执行以下操作以显示设置或清除此复选标记的对话框。

// 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