質問

I am creating List of Panel. Each panel Contain a Label and Button . I have also a Button(button1). I want to change the label text of (panels[0]) when click button1. How can I do this.This is my c# code:

List<Panel> panels = new List<Panel>();    
 private void Panel()
            {

                var x = 0;
                for (int i = 1; i < 5; i++)
                {
                    x += 60;
                    var panel1 = new Panel() { Size = new Size(60, 140), Location = new Point(x, 100), BorderStyle = BorderStyle.FixedSingle };
                    panel1.Name = "pan" + i;


                    Label lbl = new Label();
                    lbl.Name = "lbl" + i;
                    lbl.Text = i.ToString();
                    lbl.Location = new Point(10, 20);
                    panel1.Controls.Add(lbl);

                    Button button = new Button();
                    button.Location = new Point(10, 90);
                    button.Size = new Size(40, 40);
                    button.Text = "Click";

                    panel1.Controls.Add(button);



                    panels.Add(panel1);
                    Controls.Add(panel1);
                }
            }






     private void button1_Click(object sender, EventArgs e)
        {



            foreach (var p in panels)
            {


            }



        }

Output:

enter image description here

But i want, When i Click button1 it will change Label text of zero index panels(I have pointed it using red mark).

enter image description here

Can anyone help me...

役に立ちましたか?

解決

Ok, so you've got a button and a label within a panel. When you click a button of a panel, you wanna do something to the label in the same panel, right ?

So

private void BtnClick(object sender, EventArgs e) {
  var button = (Button)sender;//you've got the button clicked
  var panel = button.Parent;//you've got the panel.
  //var label = panel.Controls.OfType<Label>().FirstOrDefault();//but don't think you get this in c# 3.0
  var label = GetFirstLabel(panel);
  if (label != null)
     label.Text = "something";
}

private Label GetFirstLabel(Control parent) {
   foreach (var control in parent.Controls) {
       if (control is Label) return control as Label;
   }
   return null;
}

Usage

When you add your buttons, you can now do

 Button button = new Button();
 button.Location = new Point(10, 90);
 button.Size = new Size(40, 40);
 button.Text = "Click";
 button.Click += BtnClick;

And this will work on all panels.

他のヒント

If you don't store reference to that Label then you can find it in controls of the first Panel by type for example:

panels[0].Controls.OfType<Label>().First().Text = "New Text";

or by name

panels[0].Controls.OfType<Label>().Single(l => l.Name == "lbl1").Text = "New Text";

you can do this with a simple method:

private void button1_Click(object sender, EventArgs e)
{
    Label l = panels[0].Controls.Find("lbl1", false).FirstOrDefault() as Label;
    l.Text = "TEXT";
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top