4 Replies - 486 Views - Last Post: 17 May 2017 - 01:58 PM

#1 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 242
  • Joined: 29-October 16

Button Will Only Change checkBox->Checked Property to True, Not Fa

Posted 12 May 2017 - 08:41 AM

Hey all!
Haven't been around here much lately as I've been making YUUGE progress on my powershell/C++/.NET project. It's working great and doing EXACTLY what I want it to do. Basically I use C++/.NET to output a .ps1 script then have it autoexec after it's been built.

However, I have a very n00bish problem I've encountered and I for the life of me cannot figure out what the problem is here.

So here's my code:
private: System::Void button3_Click_1(System::Object^  sender, System::EventArgs^  e) 
{

	if (checkBox99->Checked == true)
	{
		checkBox99->Checked = false;
	}
	if (checkBox99->Checked == false)
	{
		checkBox99->Checked = true;
	}
}



for some reason when I click the button it will only change the state to true but when I click it again it will not change the property back to false. WTF? I don't get why it would be doing this. Does anyone have any ideas as to why this would be the case? No idea where my logic went wrong here.

Thanks, in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: Button Will Only Change checkBox->Checked Property to True, Not Fa

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13566
  • View blog
  • Posts: 54,122
  • Joined: 12-June 08

Re: Button Will Only Change checkBox->Checked Property to True, Not Fa

Posted 12 May 2017 - 08:48 AM

Why would there be two separate if conditions?

Think about it, and walk through the scenarios.

04    if (checkBox99->Checked == true)
06        checkBox99->Checked = false;

08    if (checkBox99->Checked == false)
10        checkBox99->Checked = true;


Checkbox is true.
Line 4 happens, and condition is true..
Line 6 happens.. now checkbox is false.
Line 8 happens, and condition is true..
line 10 happens and checkbox is now true again.

Checkbox is false
Line 4 happens, and condition is false
Line 8 happens, and condition is true..
line 10 happens and checkbox is now true again.

Wouldn't it make sense to use an if/else and not two independent if conditions?
Was This Post Helpful? 1
  • +
  • -

#3 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 242
  • Joined: 29-October 16

Re: Button Will Only Change checkBox->Checked Property to True, Not Fa

Posted 12 May 2017 - 09:03 AM

AHHH how did I miss that.. DERP. lol. Thanks Modi xD
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5928
  • View blog
  • Posts: 20,267
  • Joined: 05-May 12

Re: Button Will Only Change checkBox->Checked Property to True, Not Fa

Posted 13 May 2017 - 08:57 AM

You could use if-else-if structure, but for your usage, it may simpler to do: myBool = !myBool;
Was This Post Helpful? 1
  • +
  • -

#5 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 242
  • Joined: 29-October 16

Re: Button Will Only Change checkBox->Checked Property to True, Not Fa

Posted 17 May 2017 - 01:58 PM

Interesting. I've seen the Bools a lot... still haven't delved into them. There's so much I don't know!!! But keeping busy staying pragmatic. I know people say that C++/.NET are not good to learn but I actually love C++/.NET // it's really all I need for what I'm doing which is a general assistance tool for my work. Basically I've coded a program that allows me to take notes in little windows, move files around with POWERSHELL except in a GUI/winforms environment, it's got a relaxing screensaver... I was able to do all that in a few months and I love how C++ runs on just about everything, once compiled.

Bit of a digression there.. but just sayin' that there's SOOOOO many functions that I still need to delve into and that bool is one of them. If you have a good resource for me to research that it would be appreciated.

And also... lol.... OP is EMBARASSING haha.... I was like how tf did I miss that :donatello: :bananaman:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1