Click here to Skip to main content
15,902,189 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi, i have write a class which generate lebels dynamically by a loop. But i am having problem to add those lebels to the MainForm. Can anyone give me some hints how to do this?
Posted

this.Controls.Add(<your label="" object="" here="">);
 
Share this answer
 
Label label = new Label();
label.Text = "Some Text";
this.Controls.Add(label);
 
Share this answer
 
prionkor wrote:
when i write this code in MainForm.cs it work fine. But when i try in class it shows error


Because unless the class is a container control (inheriting from the required base class) then it will not be able to display controls.


prionkor wrote:
I also tried MainForm.Controls.Add(levels);


This won't work as you are accessing the MainForm class and not it's instance.

Your other class should raise an event with a suitable instance of a class dreived from System.EventArgs that has a property of type Control[], List<Control> or ReadOnlyCollection<Control>. MainForm can then subscribe to your class's event and pass the controls via the event back to that instance
 
Share this answer
 
This is very rough but shows you how you can do it either using a method or an event. If FormMain needs to request the labels then use the by method approach. If the class needs to tell the form that it has labels ready then use the by event way.
C#
using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;

namespace LabelsGenerator
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
            Shown += new EventHandler(FormMain_Shown);
        }

        void FormMain_Shown(object sender, EventArgs e)
        {
            LabelMaker labelMaker = new LabelMaker();
            Controls.AddRange(labelMaker.MakeLabelsByReturn(2));
            labelMaker.LabelsMade += new EventHandler<LabelsMadeEventArgs>(labelMaker_LabelsMade);
            labelMaker.MakeLabelsByEvent(2);
        }

        void labelMaker_LabelsMade(object sender, LabelsMadeEventArgs e)
        {
            Controls.AddRange(e.Labels);
        }
    }

    public class LabelMaker
    {
        public event EventHandler<LabelsMadeEventArgs> LabelsMade;

        public Label[] MakeLabelsByReturn(int howMany)
        {
            return MakeLabels(howMany).ToArray();
        }
        public void MakeLabelsByEvent(int howMany)
        {
            OnLabelsMade(new LabelsMadeEventArgs(MakeLabels(howMany)));
        }
        private List<Label> MakeLabels(int howMany)
        {
            List<Label> result = new List<Label>(howMany);
            if (howMany > 0)
            {
                for (int i = 0; i < howMany; i++)
                    result.Add(new Label());
            }
            return result;
        }
        protected virtual void OnLabelsMade(LabelsMadeEventArgs e)
        {
            EventHandler<LabelsMadeEventArgs> eh = LabelsMade;
            if (eh != null)
                eh(this, e);
        }
    }

    public class LabelsMadeEventArgs : EventArgs, IEnumerable<Label>
    {
        private List<Label> labels;

        public LabelsMadeEventArgs(IEnumerable<Label> collection)
        {
            labels = new List<Label>(collection);
        }

        public Label this[int index]
        {
            get { return labels[index]; }
        }
        public Label[] Labels
        {
            get { return labels.ToArray(); }
        }

        IEnumerator<Label> IEnumerable<Label>.GetEnumerator()
        {
            return (labels as IEnumerable<Label>).GetEnumerator();
        }
        IEnumerator IEnumerable.GetEnumerator()
        {
            return (labels as IEnumerable).GetEnumerator();
        }
    }
}
 
Share this answer
 
Yes i tried that before

this.Controls.Add(levels)

but when i write this code in MainForm.cs it work fine. But when i try in class it shows error:

VB
Error   3   'motion.ClassName' does not contain a definition for 'Controls' and no extension method 'Controls' accepting a first argument of type 'motion.ClassName' could be found (are you missing a using directive or an assembly reference?) 


I also tried MainForm.Controls.Add(levels);

but does not works;
 
Share this answer
 
Thanks! Can you give me an example. It is getting harder for me :( I am new in OOP.
 
Share this answer
 

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