raw_hitt
raw_hitt

Reputation: 989

Can we create multiple events during runtime in C# for controls which are created during runtime

Here I am creating few controls during run time with help of following code which is working fine and good.But along with that I have to perform certain tasks on events of these controls which are generated during run time

 private void btnExtra_Click(object sender, EventArgs e)
            {
                AddNewLabel();
            }

            int count = 1;
            public System.Windows.Forms.Label AddNewLabel()
            {
                System.Windows.Forms.Label lbl = new System.Windows.Forms.Label();
                lbl.Name = "LabelX" + this.count.ToString();
                lbl.ForeColor = Color.Black;
                lbl.Font = new Font("Sego UI", 8, FontStyle.Bold);
                lbl.Top = count * 25;
                lbl.Left = 100;
                lbl.Text = "Label 1 " + this.count.ToString();
                lbl.BringToFront();
                panel4.Controls.Add(lbl);
                count = count + 1;
                return lbl;
            }

Upvotes: 2

Views: 169

Answers (4)

Siddharth Sharma
Siddharth Sharma

Reputation: 21

All you have to do is similarly you can add more properties as well

 mylabel.DoubleClick += mylabel_DoubleClick; //This is the name of your function

Upvotes: 1

To Generate Multiple events during runtime

 public System.Windows.Forms.Label AddNewLabel()
        {
            System.Windows.Forms.Label lbl = new System.Windows.Forms.Label();
            lbl.Name = "LabelX" + this.count.ToString();
            lbl.Top = 85;
            lbl.Left = 100;
            lbl.Text = "Label"+count;
            lbl.AutoSize = true;
            panel4.Controls.Add(lbl);
            //multiple events
            lbl.MouseMove += Control_NewEvent1;
            lbl.MouseDown += Control_NewEvent2;
            count = count + +;
            lbl.BringToFront();
            return lbl;
        }

//Events
private void Control_NewEvent1(object sender, EventArgs e)
{
         ((Label)sender).ForeColor = Color.White;
}
private void Control_NewEvent2(object sender, EventArgs e)
{
          ((Label)sender).BackColor = Color.Black;
}

Upvotes: 0

Wheels73
Wheels73

Reputation: 2890

Manually create the event, the wire up the handler.

private void lblMyLabel_Click(object sender, EventArgs e)
{
            //Add Code here.
}

        public System.Windows.Forms.Label AddNewLabel()
        {
            int count = 1;
            System.Windows.Forms.Label lbl = new System.Windows.Forms.Label();
            lbl.Name = "LabelX" + count;
            lbl.ForeColor = Color.Black;
            lbl.Font = new Font("Sego UI", 8, FontStyle.Bold);
            lbl.Top = count * 25;
            lbl.Left = 100;
            lbl.Text = "Label 1 " + count;
            lbl.BringToFront();
            //Wire relevant event handler here
            lbl.Click += lblMyLabel_Click;
            panel4.Controls.Add(lbl);
            count ++;

            return lbl;
        }

Upvotes: 1

Pradip Meghapra
Pradip Meghapra

Reputation: 148

can you try this

public System.Windows.Forms.Label AddNewLabel()
{
    System.Windows.Forms.Label lbl = new System.Windows.Forms.Label();
    lbl.Name = "LabelX" + this.count.ToString();
    lbl.ForeColor = Color.Black;
    lbl.Font = new Font("Sego UI", 8, FontStyle.Bold);
    lbl.Top = count * 25;
    lbl.Left = 100;
    lbl.Text = "Label 1 " + this.count.ToString();
    lbl.DoubleClick += Lbl_DoubleClick;
    lbl.BringToFront();
    panel4.Controls.Add(lbl);
    count = count + 1;
    return lbl;
}
private void Lbl_DoubleClick(object sender, EventArgs e)
{
    ((Label)sender).Text = "Double Click";
}

Upvotes: 1

Related Questions