我有一个 Windows 窗体项目,其中 1 个窗体 (FormMain) 和 3 个用户控件(TitleScreen、MainScreen 和 InGameMenu)充当不同的屏幕。MainScreen 中包含一个 Label,lblDate,它的 Text 属性需要在项目的其他地方(TitleScreen 和 InGameMenu)进行修改,但是因为我无法将其设为静态,我无法将其设为静态公共属性来访问它。我听说事件可能会完成工作,但我不知道该怎么做。请帮我弄清楚如何从另一个类修改 lblDate.Text 。我不在乎在这一点上它是如何完成的。这真让我抓狂。
在 ScreenReferenceHolder.cs 中
class ScreenReferenceHolder
{
#region Field Region
private static FormMain formMain;
private static UserControls.TitleScreen titleScreen;
private static UserControls.MainScreen mainScreen;
private static UserControls.InGameMenu inGameMenu;
#endregion
#region Property Region
public static FormMain FormMain
{
get
{
if (formMain == null)
formMain = new FormMain();
return formMain;
}
}
public static UserControls.TitleScreen TitleScreen
{
get
{
if (titleScreen == null)
titleScreen = new UserControls.TitleScreen();
return titleScreen;
}
}
public static UserControls.MainScreen MainScreen
{
get
{
if (mainScreen == null)
mainScreen = new UserControls.MainScreen();
return mainScreen;
}
}
public static UserControls.InGameMenu InGameMenu
{
get
{
if (inGameMenu == null)
inGameMenu = new UserControls.InGameMenu();
return inGameMenu;
}
}
在 Program.cs 中
Application.Run(ScreenReferenceHolder.FormMain);
在 FormMain.cs 中
public FormMain()
{
InitializeComponent();
this.Controls.Add(ScreenReferenceHolder.TitleScreen);
this.Controls.Add(ScreenReferenceHolder.MainScreen);
this.Controls.Add(ScreenReferenceHolder.InGameMenu);
ScreenReferenceHolder.MainScreen.Visible = false;
ScreenReferenceHolder.InGameMenu.Visible = false;
}
#endregion
我试过把它放在 MainScreen.cs 中,但没有任何东西可以访问它。
public string LabelDate
{
get { return lblDate.Text; }
set { lblDate.Text = value; }
}
最后,在 MainScreen.Designer.cs 中是标签
private void InitializeComponent()
{
this.lblDate = new System.Windows.Forms.Label();
}
private System.Windows.Forms.Label lblDate;