Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

First here is my Code.

C#
private void Form1_Load(object sender, EventArgs e)
{
    foreach (var button in Controls.OfType<System.Windows.Forms.Control>())
    {
        button.Click += button_Click;

    }

}

    private void button_Click(object sender , EventArgs e)
    {
        Console.WriteLine("the Control with the name "+ this.Controls[((System.Windows.Forms.Control)(sender)).Name].Name+ "was clicked");

    }



Every Control in the Control Class print its name if it was clicked.

This works fine but when a Control e.g. a button or pictureBox is in a GroupBox it doesnt give me the name anymore if i click on it. When i click on the Groupbox itself, it gives me the name of the GroupBox. It seems that i have no access to the Controls names anymore.

Actually my question is about how can i get the name of the clicked Control and the associated GroupBox if the Controls are in a GroupBox. I wanted to mention that problem anyway.

I need it in the same way like the code above. I dont want to implement for every Control a seperate Method.

I would highly appreciate it if somebody can give me a suggestion.

Thank you.

What I have tried:

Google, MSDN, looking about common issues at CodeProject.com
Posted
Updated 9-Nov-16 7:13am
Comments
[no name] 9-Nov-16 13:05pm    
GroupBox has its own controls collection. You would need to check it instead of the forms controls collection.

1 solution

This is because the controls in a container such as a groupbox or panel belong to the container's controls collection, not the form's control's collection. You need to recurse through the container's collections if you want to get them all
C#
private void SetClickHandler(Control.ControlCollection controls) {
	foreach (var button in controls.OfType<Control>()) {
		button.Click += button_Click;
		if (button.Controls.Count > 0)
			SetClickHandler(button.Controls);
	}
}
private void Form1_Load(object sender, EventArgs e) {
	SetClickHandler(Controls);
}
 
Share this answer
 
v5
Comments
Member 12341536 9-Nov-16 14:51pm    
Hi,

thank you very much for your answers.

To Midi_Mick:
I replaced your code with my code, but it is still giving me only the names of the controls outside the groupbox/panel. Do you have an idea what it could be?

Thank you.
Midi_Mick 9-Nov-16 17:11pm    
Did you do what I did first up, and in the foreach, forget to change "Controls.OfType" to "control.Controls.OfType"? Or perhaps the braces around the outside of the button.Click+= and the if statement?
Ralf Meier 10-Nov-16 3:47am    
@Mick:
Perhaps you should not ask for ContainerControl - better you ask if the ControlsCollection of this Control has a Count-Value > 0. Also the type for the SetClickHandler-method could be Control instead of ContainerControl ...
Midi_Mick 10-Nov-16 4:32am    
You are right. GroupBox doesn't inherit from ContainerControl. Who'd have thunk. Will update my solution to suit. Thank you.
Ralf Meier 10-Nov-16 6:08am    
You are welcome ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900