Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WinForm
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 2:41am
Edited 25-Feb-12 2:49am
v2
Rate this: bad
good
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.
  Permalink  
Rate this: bad
good
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.
  Permalink  
Comments
ready to learn at 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)



Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 28 Feb 2012
Copyright © CodeProject, 1999-2014
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