Solved

Doesn't seem logical

Posted on 2014-09-04
8
224 Views
Last Modified: 2014-09-10
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
Comment
Question by:VaughanMx
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 27

Expert Comment

by:d-glitch
ID: 40303476
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
 

Author Comment

by:VaughanMx
ID: 40303654
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
 
LVL 27

Expert Comment

by:d-glitch
ID: 40303743
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
 
LVL 16

Accepted Solution

by:
HooKooDooKu earned 500 total points
ID: 40303967
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Closing Comment

by:VaughanMx
ID: 40305285
Thanks HooKooDooKu, your suggestion works - maybe one day a more elegant solution will appear, but till then, thank you.
0
 
LVL 35

Expert Comment

by:mccarl
ID: 40308970
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
 

Author Comment

by:VaughanMx
ID: 40313967
Thanks mccari- I didn't know that - it's exactly what I wanted to do
0
 
LVL 35

Expert Comment

by:mccarl
ID: 40314201
You're welcome! :)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now