在WindowsForms中,我有一个独立的类Indicators.cs和一种形式的[[Food。在课堂上,我有一个[[variable,我正在更改其形式。
当我打开窗体,更改变量时,关闭窗体并再次打开它(不关闭程序),然后variable的值就旧了。为什么这样?Form:
namespace WF
{
public partial class Computer : Form
{
Indicators indicators = new Indicators();
public Computer()
{
if (indicators.isComputerAlreadyRunning == false)
indicators.isComputerAlreadyRunning = true;
}
}
}
Indicators.cs
namespace WF
{
class Indicators
{
public Indicators()
{
this.isComputerAlreadyRunning = false;
}
public bool isComputerAlreadyRunning;
}
}
public FoodFormResult ShowForm()
{
return new FoodFormResult(
ShowDialog() == DialogResult.OK,
_indicators);
}
每次创建表单类(例如new FoodForm())时,表单中的所有现有值都会丢失。
以及当您需要调用该变量时:
//Your main method example
static class Program
{
internal static bool AreYouOKAY = false;
// or public static bool as your needs
static void Main ()
{
........
}
/// And in your form
Program.AreYouOKAY = true;
// After your form closed.. from another form just call as above
if(Program.AreYouOKAY)
{
MessageBox.Show (" Yeah! I'm Ok!");
}