c# add label from form2

前端 未结 2 1741
清歌不尽
清歌不尽 2020-12-22 13:56

Form2 code:

public void button2_Click(object sender, EventArgs e)
        {   
          Form1 form1form = new Form1();
            Label asd = new Label();
         


        
2条回答
  •  北海茫月
    2020-12-22 14:45

    To add all controls in form2 to form1:

    foreach(Control control in form2form.Controls)
    {
        form1form.Controls.Add(control);
    }
    

    Or if you want to just add those two things, there are two options: 1. Make those two Controls public as so

    public class Form2 : Form
    {
        //othercode
        public Button button;
    }
    

    And then add these specific controls by:

    form1form.Controls.Add(form2form.button);
    

    2: Add them using the names, for example: say the name of the button is "button" and the name of the label is "label".

    form1form.Controls.Add(form2form.Controls.Find("button",true).FirstOrDefault();
    

提交回复
热议问题