简体   繁体   English

从C#中的另一个表单调用按钮

[英]Invoke button from another Form in C#

I'm trying to invoke button from a different Form than it was created. 我试图从与创建表单不同的表单调用按钮。 I have application with two forms: 我有两种形式的申请:

  • Main 主要
  • Second 第二

The "Second" Form is invoked from the "Main" Form with the code: “第二”表单是通过“主”表单中的代码调用的:

Second run_second_form = new Second();
run_second_form.Show();

On the "Main" Form I have a button "Button1". 在“主”表单上,我有一个按钮“ Button1”。 Is it possible this button to be invoked from the "Second" Form? 是否可以从“第二个”表单中调用此按钮?

I can easily invoke "Button1" from the "Main" Form where it is created, with the code: 我可以使用以下代码从创建它的“主”表单中轻松调用“ Button1”:

Button1.PerformClick();

but I'm not able to do it from the "Second" Form. 但我无法通过“第二”表格进行操作。 I tried with: 我尝试过:

Main.Button1.PerformClick();

but it says "The name "Button1" does not exists in the current context". 但显示“名称” Button1”在当前上下文中不存在”。

Button1 is not visible by the other form as it's modifier is not public or internal but this isn't how you want to do this anyway. Button1在其他窗体中不可见,因为它的修饰符不是公共的或内部的,但这无论如何都不是您想要的方式。

I'd have the second form fire an event, and have the first form have an event handler to the instance of the second form. 我会让第二个表单触发一个事件,让第一个表单对第二个表单的实例具有一个事件处理程序。

That way, there's no bidirectional dependence created between the two forms. 这样,两种形式之间就不会产生双向依赖性。

Here's an example: 这是一个例子:

Event Class: 活动类别:

public class ActionToCallEvent : EventArgs
{
    private ActionToCallEvent() {}
}

In Form 2: 在表格2中:

public static event EventHandler<ActionToCallEvent> ActionToCall; 

private static void OnActionToCall(EventArgs e) 
{
   if (ActionToCall != null)
      ActionToCall(this, e);
}

You'll be calling OnActionToCall in Form2 when you need to use the method from 1. 需要使用1中的方法时,将在Form2中调用OnActionToCall。

In Form 1, when instantiating the Form2 instance: 在Form 1中,当实例化Form2实例时:

Form2 form2 = new Form2();
form2.ActionToCall += Form2EventHandler;

And this is the method in Form1 to capture the event handling: 这是Form1中捕获事件处理的方法:

private void Form2EventHandler(object sender, ActionToCallEvent e)
{
    // Call Your Code Here!
}

So, the first thing is that controls, when added to forms using the designer, are added as "Private". 因此,第一件事是,当使用设计器将控件添加到表单时,控件将添加为“私有”。 For the control to be accessible outside of the the for Main you need change the accessibility. 为了使控件在Main之外可以访问,您需要更改可访问性。

辅助功能

Change it from "Private" to either "Internal" (preferred if the two forms are in the same assembly) or "Public" if they are not. 将其从“专用”更改为“内部”(如果两种形式在同一程序集中,则为首选);如果不是,则将其更改为“公共”。

Then you should be able to access the Button1 control on main form. 然后,您应该能够访问主窗体上的Button1控件。

The only thing that you don't show is how you keep a reference to Main to be able to call Main.Button1.PerformClick() . 您唯一没有显示的是如何保留对Main的引用以能够调用Main.Button1.PerformClick()

After changing the accessibility in the designer, here's the code I used to test this: 在设计器中更改了可访问性之后,这是我用来测试此代码的代码:

public partial class Second : Form
{
    public Second()
    {
        InitializeComponent();
    }

    internal Main Main { get; set; }

    private void button1_Click(object sender, EventArgs e)
    {
        if (this.Main != null)
        {
            this.Main.Button1.PerformClick();
        }
    }
}

public partial class Main : Form
{
    public Main()
    {
        InitializeComponent();
    }

    private void Main_Load(object sender, EventArgs e)
    {
        Second run_second_form = new Second();
        run_second_form.Main = this;
        run_second_form.Show();
    }

    private void Button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Clicked on Main");
    }
}

That worked for me. 那对我有用。

Having said all this though, I think Ctznkane525's solution is probably a better one for what you need. 综上所述,我认为Ctznkane525的解决方案可能是您所需的更好解决方案。 It's generally better to avoid passing references to forms around like this. 通常最好避免像这样传递对表单的引用。 Main should just respond to an event from Second . Main应该只响应Second的事件。

Well, firstly you probably want to move the logic out of the button click event into it's own method. 好吧,首先,您可能希望将逻辑从按钮单击事件移到它自己的方法中。 Secondly, you really just need to pass a reference from the Main form to the Second form. 其次,您实际上只需要将引用从Main表单传递给Second表单。

There are a few options on this. 有一些选择。

 Second run_second_form = new Second();
 run_second_form.Show(this); //Makes the main form the owner of second_form

Then in your second form 然后以第二种形式

Mainform mainForm = (MainForm)this.Owner;
mainform.Button1.PreformClick

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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