On / Off Darkmode Button [closed]

问题描述 投票:0回答:2
我可以单击一次,但是此后,它卡在了Darkmode上,我无法切换回去

public static bool i = false; private void BtnDark_Click(object sender, EventArgs e) { i = true; if (i == true) // Darkmode { //... i = false; } else if (i == false) // Whitemode { //... i = true; } }

c# visual-studio switch-statement
2个回答
0
投票
这是因为您总是在方法开始时设置i = true。此变量应在方法之外定义(并且应给它一个适当的名称)

public static bool i = false; private void BtnDark_Click(object sender, EventArgs e) { //i = true; JUST REMOVE THIS LINE if (i) // Darkmode { //... i = false; } else // Whitemode { //... i = true; } }

或类似这样:

if (i = !i) //assign and compare at the same time { //... } else { //... }


0
投票
您执行这样的操作。

public static bool i = false; //changed the method name to be more descriptive for the event private void BtnToggleDarkMode_Click(object sender, EventArgs e) { i = !i; // toggle the boolean. //If true if(i) { //Do sth } else{ //do sth else } }

© www.soinside.com 2019 - 2024. All rights reserved.