Reputation: 11
I am clicking a button to hide one window and open another.
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
}
The text property of a label will change in the new form depending on what button is clicked. This is done by assigning a value to a public variable depending on what button is clicked.
public class backgroundProgram
{
public int moduleNumber;
}
This is the code that changes the text on the label according to the variable:
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;
}
}
So far the variable background.moduleNumber is resetting to 0 before it enters the switch/case, so i only get the default case back every time. Any ideas?
Edit: Just added a watch to the variable and it gets wiped on this line.
private System.ComponentModel.IContainer components = null;
This lies in moduleScreen.Designmer.cs
code showing my new backgroundProgram(); location
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;
}
}
} }
Upvotes: 0
Views: 1578
Reputation: 5353
From the comments and the post I deduce that you have a sitation like this:
public partial class MainForm : Form
{
static backgroundProgram = new backgroundProgram(); //first declaration
private void gamemodButton_Click(object sender, EventArgs e)
{
backgroundProgram.moduleNumber = 1;
//rest..
}
}
And then in the other class
public partial class moduleScreen : Form
{
static backgroundProgram = new backgroundProgram(); //second declaration
public void moduleScreen_Shown (Object sender, EventArgs e)
{
switch(backgroundProgram.moduleNumber)
{
//...
}
}
}
However, although the variables have been declared static
, each class holds one different static instance of the objects for itself. In MainForm
and moduleScreen
you are refering to "backgroundProgram", which are different objects from the point of view of these two classes. You must either pass your object from one Form
to another or you can just use a seperate class in which you declare the property static
from the beginning.
Passing the reference would go like:
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)
{
//...
}
}
}
Or, with a static field:
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
{
//...
}
}
}
And in that other class:
private void gamemodButton_Click(object sender, EventArgs e)
{
BackgroundProgram.moduleNumber = 1; //Refer to that same static field..
//..
}
Upvotes: 0
Reputation: 77
Not entirely possible to say form the given code, but from what I see it looks as though you are creating a new moduleScreen every time the button is pressed
Upvotes: 1