tags:

views:

286

answers:

3

I have this for loop:

int iPanelNumber = 1;

foreach (string[] Persons in alItems)
{
     Panel pPanelContainer = new Panel();
     pPanelContainer.Width = contentPanel.Width;
     pPanelContainer.Height = 50;
     pPanelContainer.BackColor = Color.FromArgb(
         Convert.ToInt32(aWhiteContentBackgroundColors[0]),
         Convert.ToInt32(aWhiteContentBackgroundColors[1]),
         Convert.ToInt32(aWhiteContentBackgroundColors[2]));

     pPanelContainer.Name = "PanelContainer" + iPanelNumber.ToString();
     pPanelContainer.Visible = false;
     pPanelContainer.Location = new Point(0, 0);
}

So as you can see, i have given the panels i create the name "PanelContainer1", "PanelContainer2" etc...

But how can i reach these panels?

I certainly could not reach them by writing:

PanelContainer1.visible = true;

Anyone got an idea?

Thanks in advance

+2  A: 

Easiest way is probably to add a List<Panel> field to your class and store references to all panels in that list, e.g:

class MyClass
{
  private List<Panel> _panels = new List<Panel>();

  void MethodWhichCreatesThePanels()
  {
    //..
    foreach (string[] Persons in alItems)
    {
      Panel pPanelContainer = new Panel();
      _panels.Add(pPanelContainer);
      ...
    }  
  }

Then you can access each panel later using an index:

Panel aPanel = _panels[i];
M4N
Alright!I'll try that.Thanks!
A: 

Martin's answer is pretty much what you are looking for, but it appears you are in confusion over what the .Name property of the panel control does.

What it doesn't do is set the name of the variable.

What it does do is the following (from MSDN: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.name.aspx)

The Name property can be used at run time to evaluate the object by name rather than type and programmatic name. Because the Name property returns a String type, it can be evaluated in case-style logic statements (Select statement in Visual Basic, switch statement in Visual C# and Visual C++).

Lee
A: 

You can't reference the Panel by its name because there's no field in the form (or local variable) with that name. Those fields are defined in the partial class file for the Form that the Form Designer generates; they aren't (and can't be) created at runtime.

This doesn't mean you can't access it by its name; you just can't access it by using its name as a variable name in your code.

The most obvious way to do this is to add the Panel to its containing control's Controls collection after creating it. Since you've set Visible to false, this won't have any visible effect on your form. So in your code, you'd add something like

contentPanel.Add(p);

You can then reference it by name:

contentPanel.Controls["PanelContainer1"].Visible = true;

If for some reason you don't want to add it to the Controls collection yet (there are plenty of reasons you might not), the next approach is to create an instance of a collection class of some kind and add the Panel to that collection. Since you want to be able to reference it by name, the most obvious choice would be a dictionary, e.g.:

Dictionary<string, Panel> panels = new Dictionary<string, Panel>;
...
panels.Add(p.Name, p);

And again, you can then reference it by name:

panels["PanelContainer1"].Visible = true;

...though in this case, the Panel wouldn't actually become visible, because it's not in the Controls collection of a visible container control.

Just as an aside: if it's within your power to do so, you should put an end to using type prefixes on your variable names. There are still shops that use this convention, but it's been generally abandoned by the community of C# programmers.

Robert Rossney
ah, thanks for a good answer.is it more common to just use "_" before the variablename?
Since the IDE can generally tell you what type a variable is, it's usually not useful to embed the type in its name. This is a small piece of a very big subject: see, for instance, http://msdn.microsoft.com/en-us/library/xzf533w0(VS.71).aspx.
Robert Rossney