简体   繁体   中英

c# call button event that is on another winform

i want when i click on button1 on form1 to call button2 that is on form2 and execute the code that is under button2 event.

The following code won't work:

button2.PerformClick();

The error i get is "button2 does not exist in current context", so i tried to set modifiers to public, also click event to set on public void... no luck. form2.button2.PerformClick(); also doesn't work.

You should put the code that you want to call into a public method on Form2 and then call that method from form1.

If you need a specific instance of form2 to call the method from then you could store the Handle property from form2 somewhere and then get the appropriate form as follows.

var myForm = Form.FromHandle(myForm2Handle);
myForm.MyPublicMethod();

You could then call this from the Button1 click event.

You've got an architecture problem if you're reaching between forms and executing button click event code. You should really have an event system in place for this.

I'd suggest that Form2 would have a listener set up for an event on Form1:

public class Form2{
   public Form2{
       // get your form instance
       Form1 MyForm1Instance = new Form1();
       // hook up the event
       MyForm1Instance.SomeEvent += new EventHandler(MyHandler);
   }
   public void MyHandler(){
       // handle event here; run your button_click code or whatever
   }
}

...and Form1 would simply need to fire "SomeEvent" when you click the appropriate button.

try this and see if it works, it works for me. in the first form create your method

public void DoSomething(parameters)
{
  //code to handle those parameters
}

in the calling or second form use this in the event you want to call form1 from

Form1 f1 = new Form1();
        try
        {
            f1.DoSomething(arguments);
        }
        catch (Exception)
        {
            catch the exceptions
        }
        f1.Show();

comment if it works for u and mark it as an answer.

I think your problem is that you are trying to call instance method without object creation. Here is sample code to demostrate to you possible way of method calling:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new TestForm1());
        }
    }

    public partial class TestForm1 : Form
    {
        private System.Windows.Forms.Button button1;

        public void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Hello! Im TestForm1 and Im going to call TestForm2's code!");

            // You must create TestForm2 because of button1_Click is not a static method!!!
            TestForm2 form2 = new TestForm2();

            form2.button1_Click(this, new EventArgs());
        }

        public TestForm1()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(12, 12);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(88, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);

            this.Controls.Add(this.button1);
            this.Name = "TestForm1";
            this.Text = "TestForm1";
            this.ResumeLayout(false);
        }
    }

    public partial class TestForm2 : Form
    {
        private System.Windows.Forms.Button button1;

        public void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Hello! Im TestForm2");
        }

        public TestForm2()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(12, 12);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(88, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);

            this.Controls.Add(this.button1);
            this.Name = "TestForm2";
            this.Text = "TestForm2";
            this.ResumeLayout(false);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM