Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

How should I put this code in a class so I could use it in the mainform, because I have literly no idea. The comments are in dutch, so don't mind them.

Basically what I want to do here is to make a class that I can use for making panels and show them in the mainform. I have a list that contains all the names for the panels. Below is the code.

paneel.Controls.Clear();
            int xstart = 5;
            int ystart = 5;
            int klassenOmlaag = splitContainer.Panel2.Width / 300;
            for (int i = 0; i < films.Count; i++)
            {
                Panel klassen = new Panel(); // Nieuw paneel aanmaken.
                klassen.Name = films[i];
                klassen.Size = new Size(225, 350); // Groote van het paneel.
                klassen.Location = new Point(xstart, ystart); // Waar het paneel moet starten.
                klassen.BackColor = Color.White; // Kleur van het paneel.
                klassen.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; // Stijl.
                paneel.Controls.Add(klassen);
                if (i == klassenOmlaag)
                {
                    xstart = 5;
                    ystart = ystart + 355;
                    klassenOmlaag = klassenOmlaag + splitContainer.Panel2.Width / 230;
                }
                else
                {
                    xstart = xstart + 230;
                }
                Label klasLabel = new Label();
                klasLabel.Text = films[i];
                klasLabel.Location = new Point(5, 5);
                klassen.Controls.Add(klasLabel);

Thanks in advance.
Posted
Updated 30-Mar-11 21:35pm
v2

Hi, You should create a class with a function that you pass the panel to, then you can use this panel to create your controls on.

On the main form, you can either create an instance of this class, or alternatively you can build the class using singleton pattern and then access it from anywhere.

Here when using an instance of the class in main form
public class MyControlManager()
{

   public void CreateControls(Panel paneel)
   {
     //here place your code
   }

}


Here when using an instance of the class in main form

public class MyControlManager()
{
   private static MyControlManager _thisObj = null;

   public static MyControlManager Instance
   {
      get
      {
        if (_thisObj == null)
        {
           _thisObj = new MyControlManager();
        }

        return _thisObj;

      }
   }


   public void CreateControls(Panel paneel)
   {
     //here place your code
   }
}
 
Share this answer
 
v2
Put the above code in a method and just call this method whenever you want to have this panel that has defined properties with a label inside it.

This method can be in your mainform or the utility class (any other class with proper access specifier).
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 31-Mar-11 14:31pm    
I guess this is all needed to resolve the confusion :-)
My 5.
Recommended as answer.
--SA
To be honest, look at your code, it is not going to be too easy: It looks like you refer to a number of controls, the location of which I do not know:
paneel
splitContainer
There may be more in the full version of your code, I don't know.
The normal way to do this kind of thing, would be to create a Custom Control and let it handle all the internals - the main form is then just responsible for handling the control as a single object.

However, that is a bigger subject than I want to go into here: Google will help with tutorials.
 
Share this answer
 
Comments
kZR 31-Mar-11 3:35am    
The 'paneel' and 'splitContainer' controls are both located in the MainForm. I tried to make a refrence to the MainForm (MainForm m = new MainForm();) and put m for the 'paneel' and 'splitContainer' but that doesn't work. I will look into tutorials of how to create a Custom Control. Thanks. :)
OriginalGriff 31-Mar-11 4:01am    
No, that won't work! :laugh:
When you create a new MainForm, you get just that: a new instance, not the instance your panels and so forth are located on!
Eduard Keilholz 31-Mar-11 3:46am    
[dutch]
Ik zou zelf ook niet zo snel Nederlandse termen in code gebruiken. Nederlandse software schrijven kan natuurlijk altijd, maar de code ook in het Nederlands zou ik zelf niet zo snel doen. Ieder zijn eigen voorkeur natuurlijk...
[/ducth]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900