Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,sorry to ask such a simple question,How can I access a panel_which is on my form_ from a class?
Posted
Updated 25-Feb-12 1:49am
v2

Enumerate all controls in "Controls" collection of the form. Check type for "Panel". You may need some way to identify the panel if there are more than one panel in the direct descendents. Normally, identify each control using the "Name" property.
 
Share this answer
 
The best way to do that is to pass a reference for the control to the class in question, like this:
C#
public class MyClass
{
    public void DoSomething(ref ListBox listbox)
    {
        ... do something with the listbox contents
    }
}

And in your form:
C#
myClass.DoSomethingWith(ref listBox1);

Of course, if you don't want to modify the control at all, you don't need the ref key word.
 
Share this answer
 
Comments
ready to learn 25-Feb-12 8:09am    
Thanks a lot, It works well. But I need to access the control int the class body , not in it's function.Is there a way to do it?
#realJSOP 25-Feb-12 8:26am    
That's generally frowned upon because the data object shouldn't care about the control that presents it. However, if you insist on doing this (and you really should avoid it), you can just create a property in the class of the appropriate type, and set that property to the instance of the control in question.

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