How to assign an event function to multiple objects?

Hi,

I am new to event driven programming.

I have some checkboxes set to button-style and ThreeState true. Then I found that the checked state and the indeterminate state looked exactly the same. I came up with a solution: Use CheckStateChanged event to change the color of the button.
1
2
3
4
5
6
7
8
private: System::Void checkbox_CheckStateChanged(System::Object^  sender, System::EventArgs^  e) {
	   if (checkbox->CheckState == CheckState::Checked)
	     checkbox->BackColor = System::Drawing::Color::SlateBlue;
	   else if (checkbox->CheckState == CheckState::Indeterminate)
	          checkbox->BackColor = System::Drawing::SystemColors::Window;
	        else
	          checkbox->BackColor = System::Drawing::SystemColors::Highlight;
}

This function worked well for one checkbox, but given that I have many of them, and I do not want to copy paste and edit the object name over and over again, is there a way to do this efficiently?

Also, I want to know if there is a way to access each checkbox one by one in one form/tab/panel/group/whatever and set some properties conveniently? Say if I have a special checkbox, and if I check this checkbox, other checkboxes will be disabled, if I unchecked this checkbox, other checkboxes will be enabled.

Any help would be appreciated!
Got it somewhere else.

Use safe_cast and assign the event function to multiple objects' event handlers.
Topic archived. No new replies allowed.