---
title: "how to access my form's panel in my class"  
description: "how to access my form's panel in my class"  
author: "Mark Devid"  
published: 2013-12-18  
updated: 2013-12-18  
canonical: https://www.mindstick.com/forum/1812/how-to-access-my-form-s-panel-in-my-class  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# how to access my form's panel in my class

I have a [form](https://www.mindstick.com/forum/6/multi-form-mfc-application) and in the form I have a [panel](https://www.mindstick.com/blog/11988/restructuring-corporate-offenses-government-appointed-panel-suggests-in-house-adjudication-system) [named](https://answers.mindstick.com/qa/44918/what-is-a-no-day-yet-named-motion) : mypanel

[now](https://yourviews.mindstick.com/view/81402/it-s-liberals-vs-progressives-in-us-politics-now) I want to write a [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp) and in this class I want to use this:

```
foreach (Control cont in mypanel.Controls)        {            // do sth        }but it is impossible, please help me how to access my form's panel in my classI use this code :foreach (Control cont in mypanel.Controls)            {                if (cont is PictureBox)                {                    string path = @"" + Application.StartupPath + "\\Image\\";                    cont.BackgroundImage = Image.FromFile(path + Rnd.Next(7).ToString() + ".png");                }            }
```

and I have this [Error](https://yourviews.mindstick.com/view/88527/fixing-quickbooks-error-4120-reinstalling-vs-repairing):

"the [name](https://yourviews.mindstick.com/view/87450/how-to-generate-a-brand-name-using-linkedin-comprehensive-guide) 'mypanel' does not Exist in the current [context](https://www.mindstick.com/forum/23245/what-is-context-in-android)"

## Replies

### Reply by Anonymous User

Hi Mark,

Check if the type of control is Panel and cast it.

```
foreach (Control cont in mypanel.Controls){    if(cont is Panel)    {        Panel myPanel
= (Panel)cont;       
MessageBox.Show(myPanel.Name);    }}
```

You need to cast the control into PictureBox since not all control contains the specific property you want to set,

PictureBox pBox = (PictureBox)cont;

pBox.BackgroundImage = Image.FromFile(path + Rnd.Next(7).ToString() + ".png");


---

Original Source: https://www.mindstick.com/forum/1812/how-to-access-my-form-s-panel-in-my-class

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
