C#一键有2个功能吗? [英] C# one button to have 2 functions?

查看:66
本文介绍了C#一键有2个功能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在尝试让一个按钮为我的C#项目提供两个功能,按一下打开一个表单然后再按一下关闭那个打开的那个,我该怎么办呢?我可以告诉你到目前为止我所做的代码,没什么特别的。



按钮打开我的设置表单,其中包含打开和关闭音乐等设置以及用户的其他功能。



我尝试过:



//以下代码打开游戏设置菜单。

private void SettingsBTN_Click(object sender,EventArgs e)

{

Kid_Merchant_Hero.GameSettings f = new Kid_Merchant_Hero.GameSettings();

f .Show();

}

Hi,

I'm trying to make one button have two functions for my C# project, press it once to open a form and then press it again to close that same for that opened, how can I do this please? I can show you the code I had done so far, it's nothing special.

The button opens my setting form that has setting such as turn music on and off, and other functions for the user.

What I have tried:

//Code below opens the game settings menu.
private void SettingsBTN_Click(object sender, EventArgs e)
{
Kid_Merchant_Hero.GameSettings f = new Kid_Merchant_Hero.GameSettings();
f.Show();
}

推荐答案

第一个问题是你将新的表单实例存储在一个局部变量中,这将是退出处理程序方法时将被丢弃。

首先将其移至类级别:

The first problem is that you are storing the new form instance in a local variable, which will be discarded when you exit the handler method.
Start by moving it to class level:
private Kid_Merchant_Hero.GameSettings gameSettings = null;

然后你可以用它来决定做什么:

Then you can use that to decide what to do:

private void SettingsBTN_Click(object sender, EventArgs e)
{
if (gameSettings == null)
   {
   gameSettings = new Kid_Merchant_Hero.GameSettings();
   gameSettings.FormClosed += gameSettings.FormClosed;
   gameSettings.Show();
   SettingsBTN.Text = "Close";
   }
else
   {
   gameSettings.Close();
   }
}

处理FormClosed事件以清除表单:

Handle the FormClosed event to clear the form:

private void gameSettings_FormClosed(object sender, FormClosedEventArgs e)
    {
    gameSettings = null;
    SettingsBTN.Text = "Open";
    }

你已经完成了。


你可以枚举你创建的所有Windows /表格(WPF和Windows)。表格应用)。然后你可以决定展示,隐藏,丢弃任何一个;或者创建额外的东西而不保留单独的集合。



如何:在应用程序中获取所有Windows Microsoft Docs [ ^ ]
You can enumerate all the Windows / forms you've created (WPF and Windows.Form apps). You can then decide to show, hide, discard any of them; or create extras without maintain a separate collection.

How to: Get all Windows in an Application | Microsoft Docs[^]


我不确定,您的GameSettings对象包含哪些数据,您需要提供更多详细信息,但是有一个标志可以代表你的表格是否开放,如果是,那就做下面的事情:



if(!f.IsOpen)

f.Show();

else

f.Close();



else,您可以创建2个事件处理程序,一个具有打开状态,另一个具有关闭形式逻辑,并且在每个事件处理程序中,分离当前事件并在按钮单击时附加其他事件。

提供更多详细信息,如果没有这些为你工作
I am not sure, what data your GameSettings object holds, you need to provide more details, but is there a flag that can represents if your form is open, if yes then do something like below:

if(!f.IsOpen)
f.Show();
else
f.Close();

else, you can create 2 event handlers, one having open and other having close form logic, and in each of these, detach current event and and attach other event on button click.
Provide more details, if none of these work for you


这篇关于C#一键有2个功能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆