Mathlight
Mathlight

Reputation: 6653

Dynamic names for panels

I'm working on a program that is supposed to use with multiple users. I'm getting the users from a database. So far so good...

I'm dynamically adding a panel to the form, which would hold some data. I'm using this piece of code to achieve that:

string panel_name = "email_in_" + user_credentials[counter_snel][0];
Panel new_user_panel = new Panel();
new_user_panel.AutoSize = true;
new_user_panel.Dock = System.Windows.Forms.DockStyle.Fill;
new_user_panel.Location = new System.Drawing.Point(0, 0);
new_user_panel.Name = panel_name;
new_user_panel.Visible = false;

Label new_item = new Label();
new_item.AutoSize = true;
new_item.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
string new_item_text = string.Format("{0,0}\n{1,0}\n{2,0}", user_credentials[counter_snel][1], user_credentials[counter_snel][2],user_credentials[counter_snel][9]);
new_item.Text = new_item_text;
splitContainer2.panel_name.Controls.Add(new_item);

as you will probably notice, this line of code is not working:

splitContainer2.panel_name.Controls.Add(new_item);

How can I achieve this, so when I want to make panel email_in_1 visible, I can use that, and make email_in_8 not visible?

### EDIT 1 ###

the code now looks like this:

string panel_name = "email_in_" + user_credentials[counter_snel][0];
Panel new_user_panel = new Panel();
new_user_panel.AutoSize = true;
new_user_panel.Dock = System.Windows.Forms.DockStyle.Fill;
new_user_panel.Location = new System.Drawing.Point(0, 0);
new_user_panel.Name = panel_name;
new_user_panel.Visible = true;
user_email_in_panels.Add(new_user_panel);
splitContainer2.Panel1.Controls.Add(new_user_panel);

Label new_item = new Label();
new_item.AutoSize = true;
new_item.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
string new_item_text = string.Format("{0,0}\n{1,0}\n{2,0}", user_credentials[counter_snel][1], user_credentials[counter_snel][2],user_credentials[counter_snel][9]);
new_item.Text = new_item_text;
int aantal_panels = 0;
foreach (Panel panel in user_email_in_panels) {
        aantal_panels++;
}
int counter_1 = 0;
foreach (Panel panel in user_email_in_panels) {
        if(counter_1 == (aantal_panels -1)){
               MessageBox.Show("We are here");
               panel.Controls.Add(new_item);
               splitContainer1.Panel1.Controls.Add(panel);
        }
        counter_1++;
}

But somehow, i don't see any labels shown in the form... am i missing something? The messsagebox with the text We are Here is shown, so it come's to the add statement....

and i've got an another question besides my first question. My question is, how can i make the counter of the list better?

### Edit for Sean Vaughn

i've updated it to this:

public class MyPanelClass {
    public string Name {
        get;
        set;
    }

    public bool Visible {
        get;
        set;
    }

    public string YourLabelsText {
        get;
        set;
    }
}

Label new_item = new Label();
new_item.AutoSize = true;
new_item.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
string new_item_text = string.Format("{0,0}\n{1,0}\n{2,0}", user_credentials[counter_snel][1], user_credentials[counter_snel][2], user_credentials[counter_snel][9]);
new_item.Text = new_item_text;

Panel base_panel = new Panel();    //this is your base panel, you don't need to add this line of code because Visual Studio will do that for you.
List<MyPanelClass> myPanelList = new List<MyPanelClass>();    //this will keep records of all of your panels.

MyPanelClass panel_name = new MyPanelClass();
panel_name.Name = "email_in_" + user_credentials[counter_snel][0]; ;
panel_name.Visible = true;
panel_name.YourLabelsText = string.Format("{0,0}\n{1,0}\n{2,0}", user_credentials[counter_snel][1], user_credentials[counter_snel][2], user_credentials[counter_snel][9]);

//Now add the new created panel to the list.
myPanelList.Add(panel_name);

base_panel.Name = myPanelList[counter_snel].Name;
base_panel.Visible = myPanelList[counter_snel].Visible; //You probably don't need this because base_panel will always be visible
new_item.Text = myPanelList[counter_snel].YourLabelsText;

But i still doesn't see anything... Does it matter that it execudes in an public void??? i don't think so, but it want to elliminate all the possibilities...

Upvotes: 1

Views: 2367

Answers (2)

Sean Vaughn
Sean Vaughn

Reputation: 3932

Use a List that will keep track of all the panels.

List<Panel> myPanels = new List<Panel>();

Whenever you need to add a panel, do this:

mypanels.Add(yourPanel);

Now, for the other problem, create a function like this:

private void HideAllOtherPanels(List<Panel> panelList, Int index /*the index of the panel you want to show*/)
{
    foreach(Panel panel in panelList)
        if(panel.Visible) panel.Hide();

    panelList[index].Show();
}

I see you are creating a lot of new panels. My advice, don't do that, you're giving a lot of load to the system.

Instead make a class.

public class MyPanelClass
{
    public string Name
    {
        get; set;
    }

    public bool Visible
    {
        get; set;
    }

    public string YourLabelsText
    {
        get; set;
    }
}

After creating the class, create a base_panel on your form that will change it's contents based on the your intentions. And then, all you need to do is change the panel's content rather than creating a new Panel. Store the other panels data in a List<MyPanelClass>.

So you'll be doing this:

Panel base_panel = new Panel();    //this is your base panel, you don't need to add this line of code because Visual Studio will do that for you.
List<MyPanelClass> myPanelList = new List<MyPanelClass>();    //this will keep records of all of your panels.

MyPanelClass panel_name = new MyClassPanel();
panel_name.Name = "email_in_" + user_credentials[counter_snel][0];;
panel_name.Visible = false;
panel_name.YourLabelsText = string.Format("{0,0}\n{1,0}\n{2,0}", user_credentials[counter_snel][1], user_credentials[counter_snel][2],user_credentials[counter_snel][9]);

//Now add the new created panel to the list.
myPanelList.Add(panel_name);

Now whenever you need to activate a stored Panel, just do this:

base_panel.Name = myPanelList[yourPanelsIndex].Name;
base_panel.Visible = myPanelList[yourPanelsIndex].Visible; //You probably don't need this because base_panel will always be visible
yourLabel.Text = myPanelList[yourPanelsIndex].YourLabelsText;

This code is much less bulky on the machine and is easy to handle too.

Upvotes: 1

Roland Mai
Roland Mai

Reputation: 31097

I'd try something like this to add an item.

var panel = splitContainer2.Controls.FirstOrDefault(p => p is Panel && ((Panel)p).Name == panel_name);
if(panel != nul) 
{
    panel.Controls.Add(new_item);
}

To hide a particular panel given panel_name:

foreach(var control in splitContainer2.Controls)
{
    if(control is Panel)
    {
        ((Panel)control).Visible = ((Panel)control).Name == panel_name;
    }
}

Upvotes: 0

Related Questions