Access Of Public Method Between Forms

前端 未结 4 1405
借酒劲吻你
借酒劲吻你 2020-12-06 03:45

I am trying to get access to Form1’s public method on another form Form2 as below. I have a textbox6 control on form1 and there is public method to bind it. But

4条回答
  •  鱼传尺愫
    2020-12-06 04:07

    Also you may use events:

        public Form1()
        {
            InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.ButtonClickAction += f2_ButtonClickAction;
            f2.Show();
        }
    
        void f2_ButtonClickAction()
        {
            amount_sum();
        }
    

    Form2:

        public Form2()
        {
            InitializeComponent();
        }
    
        public event Action ButtonClickAction;
    
        private void button1_Click(object sender, EventArgs e)
        {
            Action a = ButtonClickAction;
            if (a != null)
                a();
            this.Close();
        }
    

提交回复
热议问题