gpt4 book ai didi

c# - 如何从不同的表单启用表单上的按钮?

转载 作者:太空宇宙 更新时间:2023-11-03 16:02:45 24 4
gpt4 key购买 nike

我正在制作一个带有菜单的游戏,该菜单可以为每个级别打开新表单。当你完成一个级别时,它应该通过一个消息框祝贺你(确实如此),然后在不同的菜单表单上启用下一个级别的按钮。所以我访问了设计器并将按钮公开并尝试:

new LevelMenu().button2.Enabled = true;

但这没有用。我也试过这样做:

public event Action levelCompleted;
//then down lower i did (after it is declared that you won the level):
if (levelCompleted != null)
levelCompleted();
W1L1.levelCompleted += () => LevelMenu.button2.Enabled = true;

但这给我的错误是:非静态字段、方法或属性“The_Levels.W1L1.levelCompleted”需要对象引用

我正在使用的表单是“W1L1”,“LevelMenu”是菜单表单。提前致谢

最佳答案

您基本上需要引用您的 LevelMenu 实例。因此,当您创建“W1L1”表单时,您可以将 LevelMenu 传递给它。

public class W1L1
{
private readonly LevelMenu _levelMenu;

public W1L1(LevelMenu levelMenu)
{
this._levelMenu = levelMenu;
}

//Where you want to enable the button
this._levelMenu.button2.Enabled = true;
}

从架构的角度来看,这不是最佳解决方案,但它确实有效。

但是,如果您创建一种更面向对象的方式来启用下一级按钮,那就更好了。

关于c# - 如何从不同的表单启用表单上的按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20601603/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com