calling method on one form from another form, fill combobox on Form1 if a button on Form2 is clicked

后端 未结 2 796
醉酒成梦
醉酒成梦 2021-01-21 15:40

I want to fill a combobox on Form1 , when the OK button on Form2 is clicked.

First, the Load Form2 button on Form1 is clicked to display Form2. Then, Form2 appears, and

2条回答
  •  不要未来只要你来
    2021-01-21 15:52

    Updated

    You can try passing Form1 instance in the constructor of Form2

    Example:

     public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2(this);
            form2.Show();
        }
    
        internal void TestMethod()
        {
            throw new NotImplementedException();
        }
    }
    
    public partial class Form2 : Form
    {
        private Form1 form1;
    
        public Form2()
        {
            InitializeComponent();
        }
    
        public Form2(Form1 form1)
            : this()
        {
            // TODO: Complete member initialization
            this.form1 = form1;
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            form1.TestMethod();
        }
    }
    

    Updated

    try this:

    public  FORM2(SqlConnection connfromFORM3, FORM1 form1)
    {
        this.form1 = form1;
        Form2Conn = connfromFORM3;
        InitializeComponent();
    }
    

提交回复
热议问题