Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 248
  • Last Modified:

Doesn't seem logical

void __fastcall TForm1::CheckBox17Click(TObject *Sender)
{
   CheckBox17->State = cbChecked;
   CheckBox18->State = cbUnchecked;    
}
//---------------------------------------------------------------------------

void __fastcall TForm1::CheckBox18Click(TObject *Sender)
{
   CheckBox18->State = cbChecked;
   CheckBox17->State = cbUnchecked;  
 }

This code is in effect a closed loop.
The intention is to set box 17 to a checked state, and display it, and therefor Box 18 must be unset, and that to be displayed. And vice-versa.

There must be a 'right' way to do this?
0
VaughanMx
Asked:
VaughanMx
  • 3
  • 2
  • 2
  • +1
1 Solution
 
d-glitchCommented:
I don't think there is anything wrong with this at all.

When you click Box17, it is checked and Box18 is unchecked.
When you click Box18, it is checked and Box17 is unchecked.
The last action takes precedence.

Remember that a user may make a mistake or change his mind several times before he moves on.
This code seems logically correct and efficient to me.
It is proper event driven programming.
0
 
VaughanMxAuthor Commented:
What seems to be happening is that CheckBox18->State = cbUnchecked;  ,
when called from within the void __fastcall TForm1::CheckBox17Click(TObject *Sender) ,
actually calls void __fastcall TForm1::CheckBox18Click(TObject *Sender).

That description is a bit messy, so I hope you will persevere with it.

The checkboxes continuously call each other until the system runs out of memory.

I've obviously made a mistake here.
0
 
d-glitchCommented:
I do understand your problem now, but I don't know how to fix it.
It is not really a logic problem.

On a click event, you want to call a function that makes two assignment statements.
The assignment statement should not call another function.
There is obviously a lot of additional code for setting up the form.  The problem is in there somewhere.

What language are you using?  I can't tell if it's C, Java, or Python.  
And the only one I might be able to help you with is  Python.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
HooKooDooKuCommented:
It might not be an elegant solution, but you could simply add a traffic cop.  Something like this:
bool bChecking = False;
void __fastcall TForm1::CheckBox17Click(TObject *Sender)
{
  if( ! bChecking )
  {
    bChecking = TRUE;
    CheckBox17->State = cbChecked;
    CheckBox18->State = cbUnchecked;    
    bChecking = FALSE;
  }
}
void __fastcall TForm1::CheckBox18Click(TObject *Sender)
{
  if( ! bChecking )
  {
    bChecking = TRUE;
    CheckBox18->State = cbChecked;
    CheckBox17->State = cbUnchecked;  
    bChecking = FALSE;
  }
}

Open in new window

0
 
VaughanMxAuthor Commented:
Thanks HooKooDooKu, your suggestion works - maybe one day a more elegant solution will appear, but till then, thank you.
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
I realise that this question has already had an answer accepted, but I was just curious... The code that you give seems to basically just give you the functionality of what TRadioButton gives you for free, is there any reason why you didn't just use those?
0
 
VaughanMxAuthor Commented:
Thanks mccari- I didn't know that - it's exactly what I wanted to do
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
You're welcome! :)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now