Click here to Skip to main content
13,055,852 members (49,345 online)
Rate this:
Please Sign up or sign in to 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 25-Feb-12 1:41am
Updated 25-Feb-12 1:49am
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

The best way to do that is to pass a reference for the control to the class in question, like this:
public class MyClass
    public void DoSomething(ref ListBox listbox)
        ... do something with the listbox contents

And in your form:
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.
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?
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 28 Feb 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100