Search code examples
c#buttoncontrolspanel

How to call control on a Panel that is inside usercontrol


i'm trying to solve this, with no sucess.

I'm building a code that needs to acess some controls, those are Inside of usercontrol and inside A panel. the thing is, i have more than thousand of buttons, it's madness to do this at hand, the line counts will be insanly high.

That said, i managed to build a method to do this, but i have one obstacle that i cant get rid of:

this.usercontrol.panel1.Controls[buttoninsidepanel].InvokeRequired

I have alot of panels too, each panel has it's own buttons.

i Don't know how to create a "variable" that will act as the "panel1" in the example.

i tried :

usercontrol.Controls[panel].Controls[buttoninsidepanel].Invoke

where [panel] refeers to a string that receive the name of the panel that i want to get the control.

it throws :" Object reference not set to an instance of an object".

Any way to replace the panel name with a variable that i can change dynamically, the same way i'm doing with the buttons?

Thanks in advance!


Solution

  • Use the Control.ControlCollection.Find method like this:

    ((Panel) usercontrol.Controls.Find("panel1",true)[0])