Deo
Deo

Reputation: 781

How can i add Events to hard coded forms in c#?

I've tried to create a form that will create buttons based on the number of items in database

my int count is equal to the number of count in my database

I'd like to add events or functions to the created label, that when i click the label it will display in the MesssageBox the name of the label below is my code,

int Count;
        List<Panel> pnl = new List<Panel>();
        List<Label> tasklevel = new List<Label>();
        List<Label> taskStatus = new List<Label>();
        List<Label> taskname = new List<Label>();
        List<PictureBox> image = new List<PictureBox>();


for (int i = 0; i < Count; i++)
        {
            Panel pan = new Panel();
            pan.Name = "panel" + i;
            pan.BackColor = Color.White;
            pan.Dock = DockStyle.Top;
            pan.Padding = new Padding(10, 0, 10, 10);
            pan.Height = 80;
            pnl.Add(pan);

            Label lbl = new Label()
            {
                Name = "lbl" + i,
                ForeColor = Color.Black,
                //Dock = DockStyle.Left,
                AutoSize = false,
                Width = 0,
                BackColor = Color.Silver,
                TextAlign = ContentAlignment.MiddleLeft,
                Padding = new Padding(10, 10, 10, 10),
                Text = "Designing",
            }; tasklevel.Add(lbl);

            Label lblname = new Label()
            {
                Name = "lblname" + i,
                ForeColor = Color.Black,
                Dock = DockStyle.Left,
                AutoSize = false,
                Width = 0,
                BackColor = Color.Gray,
                TextAlign = ContentAlignment.MiddleLeft,
                Padding = new Padding(10, 10, 10, 10),
                Text = "Designing",
            }; taskStatus.Add(lblname);

            Label tskname = new Label()
            {
                Name = "tskName" + i,
                ForeColor = Color.FromArgb(31, 31, 31),
                Font = new Font("Segoe UI", 11, FontStyle.Regular),
                Dock = DockStyle.Left,
                AutoSize = false,
                Width = 200,
                BackColor = Color.Gainsboro,
                TextAlign = ContentAlignment.MiddleCenter,
                Padding = new Padding(10, 10, 10, 10),
                Text = "Task Name",
            }; taskname.Add(tskname);

            PictureBox picBox = new PictureBox()
            {
                Name = "picBox" + i,
                Dock = DockStyle.Fill,
                AutoSize = false,
                Image = DreametryMIS.Properties.Resources.f1,
                SizeMode = PictureBoxSizeMode.StretchImage,
                BackColor = Color.FromArgb(240,240,240),
                Padding = new Padding(10, 10, 10, 10),
            }; image.Add(picBox);

            pan.Controls.Add(picBox);
            pan.Controls.Add(lblname);
            pan.Controls.Add(lbl);
            pan.Controls.Add(tskname);
            FlowPanel.Controls.Add(pan);
        }

I'm new at c#, I hope some can help me

Upvotes: 0

Views: 117

Answers (3)

Pradip Patil
Pradip Patil

Reputation: 1

TableCell cel2 = new TableCell();
Label lbl2 = new Label();
lbl2.Text = s;
cel2.Controls.Add(lbl2);
tr.Cells.Add(cel2);
TableCell cel3 = new TableCell();
DropDownList ddlcountry = new DropDownList();
ddlcountry.ID = s;
cel3.Controls.Add(ddlcountry);
tr.Cells.Add(cel3);
table.Rows.Add(tr);
form1.Controls.Add(table);
DropDownList ddl1 = (DropDownList)form1.FindControl("CountryId");
DropDownList ddlstate = (DropDownList)form1.FindControl("StateId");
ddl1.AutoPostBack = true;
ddl1.SelectedIndexChanged += new EventHandler(ddl1_SelectedIndexChanged);
protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
{
}

Upvotes: -1

josep
josep

Reputation: 41

You can too:

lblname.Click += delegate { nameOfUrFunction (lblname) };

private void nameOfUrFunction (Label lbl)</code>
{

}

Upvotes: 1

Ren&#233; Vogt
Ren&#233; Vogt

Reputation: 43936

Implement an event handler like this:

public void label_Clicked(object sender, EventArgs e)
{
    Label label = sender as Label;
    if (label == null) return;
    MessageBox.Show("Name: " + label.Name + " Text: " + label.Text);
}

You can now add this handler to the Click event of your labels, for example:

lblname.Click += label_Clicked;
tskname.Click += label_Clicked;

When one of the labels is clicked now, the method label_Clicked gets called with the label as argument for sender. So by casting the sender to Label you can easily access the clicked label's properties.

Note that you cannot add the handler inside the label's object initializer as you did with the other properties, but need to do it in an extra statement like I showed.

Upvotes: 1

Related Questions