Loop through checkboxes in MSVC++ 2010 Express

Hi, all. I'm trying to make a simple form in Visual C++ 2010 Express for a side-project I'm developing. I just finished a console-based C++ course in school last semester, but I haven't had any Windows-based classes yet. I'm just trying to get my feet wet and try some things out on my own.

So I was wondering: is there a way to loop through the checkboxes on a form? I have 37 numbered checkboxes and I want to switch their states if a "check for all" checkbox is checked or unchecked. Right now I just have all of their values changed in two sets of 37 long lines, but I'd like to condense this into two loops (or just one if I can dynamically switch the state based on its current state).

Here's what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
private: System::Void cbCheckForAll_CheckedChanged(System::Object^  sender, System::EventArgs^  e)
{
	if ( this->cbCheckForAll->Checked == true )
	{
		//Surely this can be done with a for loop somehow... but how?
		this->checkBox1->Checked = true;
		//...
		this->checkBox37->Checked = true;
	}
	else if ( this->cbCheckForAll->Checked == false )
	{
		//Surely this can be done with a for loop somehow... but how?
		this->checkBox1->Checked = false;
		//...
		this->checkBox37->Checked = false;
	}
}


I know how to do a simple for loop, and I was thinking that maybe I could somehow use a 1 to 37 loop to concatenate the number counter of the loop onto the end of a "checkBox" string. But then how do I then check that concatenated string's name as if it were the name of a control on the form?

Thanks in advance for any help you can provide, folks.
Put the checkboxes you have into a vector, and iterate over it. And rather than hard coding true and false you can set the to the Checked state of your checkforall checkbox.

PS: Any reason you use this ugly CLI stuff?
closed account (3hM2Nwbp)
Hanst has the right idea, but with C++/CLI you'll probably want to use a System::Collections::Generic::List to store the managed controls.

PS: Any reason you use this ugly CLI stuff?


A drag 'n drop GUI builder is always attractive.
Last edited on

A drag 'n drop GUI builder is always attractive.


Has nothing to do with CLI though. wxSmith (Code::Blocks plugin) for wxWidgets and ResEdit for windows resource scripts are two things that work with real C++.
Thanks, guys! I'll have to try that vector idea tomorrow and let you know how it works. As for the CLI stuff, I'm incredibly new to Windows Forms programming. All of the stuff I've learned and created so far with VC++ Express was console based. I just picked the new project option that had a drag-and-drop GUI, as Lieber suggested. To be honest, I didn't understand most of it and just ignored it for the time being under the assumption that it did what it was supposed. I figure I'll hopefully understand it all soon, but for now I was just trying to bite off one piece at a time, you know?

So what's this wxSmith, Code::Blocks, wxWidgets, and ResEdit stuff you speak of, hanst? The only things I've ever used to create my code thus far has been the native VC++ IDE and Notepad++. But I'm also open to new and easier ways!
http://en.wikipedia.org/wiki/WxWidgets
http://en.wikipedia.org/wiki/Code::Blocks

ResEdit is a program to create resource scripts which can be compiled to native windows resources: http://en.wikipedia.org/wiki/Resource_%28Windows%29

The problem with C++/CLI is, that it's not C++ - just as C++ is not C with two pluses at the end. It's a completely different language.
Topic archived. No new replies allowed.