变量正在重置,我不知道为什么? C#

时间:2016-04-20 17:57:12

标签: c# winforms

我点击一个按钮隐藏一个窗口并打开另一个窗口。

private void gamemodButton_Click(object sender, EventArgs e)
{
    background.moduleNumber = 1;
    this.Hide();
    moduleScreen showForm = new moduleScreen();
    showForm.Show();
    MessageBox.Show(background.moduleNumber.ToString()); //for checking that the variable was applied
}

标签的text属性将根据单击的按钮在新表单中更改。这是通过根据单击的按钮为公共变量赋值来完成的。

public class backgroundProgram
{
    public int moduleNumber;
}

这是根据变量更改标签上文本的代码:

private void moduleScreen_Shown(Object sender, EventArgs e)
{
    switch (background.moduleNumber)
    {
        case 1:
            moduleLabel.Text = "Game Design 1 - CGP1005M";
            break;
        case 2:
            moduleLabel.Text = "Algorithms and Complexity - CMP 1124M";
            break;
        case 3:
            break;
        case 4:
            break;
        case 5:
            break;
        case 6:
            break;
        case 7:
            break;
        default:
            MessageBox.Show("Nope");
            break;
    }
}

到目前为止,变量background.moduleNumber在进入switch / case之前重置为0,所以我每次都只返回默认的case。有什么想法吗?

编辑:只需在变量中添加一个监视器,就可以在此行上删除它。

  private System.ComponentModel.IContainer components = null;

这位于moduleScreen.Designmer.cs

代码显示我的新backgroundProgram();位置

namespace ModNote
{
public partial class moduleScreen : Form
{
    static backgroundProgram background = new backgroundProgram();
    public moduleScreen()
    {

        InitializeComponent();
        Shown += moduleScreen_Shown;
    }

    public void moduleScreen_Shown (Object sender, EventArgs e)
    {

        switch (background.moduleNumber)
        {
            case 1:
                moduleLabel.Text = "Game Design 1 - CGP1005M";
                break;
            case 2:
                moduleLabel.Text = "Algorithms and Complexity - CMP 1124M";
                break;
            case 3:
                break;
            case 4:
                break;
            case 5:
                break;
            case 6:
                break;
            case 7:
                break;
            default:
                MessageBox.Show("Nope");
                break;
        }


        }

} }

2 个答案:

答案 0 :(得分:1)

不完全可以说出给定代码的形式,但从我看到的内容看起来好像每次按下按钮时都在创建一个新的moduleScreen

答案 1 :(得分:0)

从评论和帖子中我推断出你有这样的引用:

public partial class MainForm : Form
{
    static backgroundProgram = new backgroundProgram(); //first declaration

    private void gamemodButton_Click(object sender, EventArgs e)
    {
        backgroundProgram.moduleNumber = 1;
        //rest..
    }
}

然后在另一个班级

public partial class moduleScreen : Form
{
    static backgroundProgram = new backgroundProgram(); //second declaration

    public void moduleScreen_Shown (Object sender, EventArgs e)
    {
       switch(backgroundProgram.moduleNumber) 
       {
          //...
       }
    }
}

但是,尽管变量已声明为static,但每个类为自身保存一个不同的对象静态实例。在MainFormmoduleScreen中,您引用的是“backgroundProgram”,从这两个类的角度来看,它们是不同的对象。您必须将对象从一个Form传递到另一个{或者1},或者您可以使用一个单独的类,在该类中从头开始声明属性static

传递引用将如下:

public partial class MainForm : Form
{
    static backgroundProgram = new backgroundProgram(); //first declaration

    private void moduleScreen_Shown(Object sender, EventArgs e)
    {
        backgroundProgram.moduleNumber = 1;
        moduleScreen showForm = new moduleScreen();
        showForm.backgroundProgram = backgroundProgram; //give it this object
        //rest..
    }
}

public partial class moduleScreen : Form
{
    public backgroundProgram; //no initialization. null from the beginning, set later.

    public void moduleScreen_Shown (Object sender, EventArgs e)
    {
       switch(backgroundProgram.moduleNumber) 
       {
          //...
       }
    }
}

或者,使用静态字段:

public class BackgroundProgram
{
    public static int moduleNumber; 
}

public partial class moduleScreen : Form
{
    //no own static fields of that object
    public void moduleScreen_Shown (Object sender, EventArgs e)
    {
       switch(BackgroundProgram.moduleNumber) //Refer to the static field of that class
       {
          //...
       }
    }
}

在另一个班级:

    private void gamemodButton_Click(object sender, EventArgs e)
    {
        BackgroundProgram.moduleNumber = 1; //Refer to that same static field.. 
        //..
    }