简体   繁体   English

如何从C#中的另一个类调用Button_Click?

[英]How to call Button_Click from another Class in c#?

I have a class Class1 : 我有一个Class1类:

public partial class Class1 {
     public void Button1_Click(object sender, RibbonControlEventArgs e) 
     {
          //do something
     }
}

and a second class Class2 : 第二类Class2

public partial class Class2 {
     //some Code
     //Call Button1_Click from Class1
     // some more code
}

How can I call the Button1_Click of Class1 in Class2? 如何在Class2中调用Class1的Button1_Click?

You can pass Class1 into Class2 via the constructor eg 您可以通过构造函数将Class1传递给Class2,例如

class Class1
{
    Class2 c2 = new Class2(this);
}

class Class2
{
    Class1 C2;

    public Class2(Class1 _class1){
        C2 = _class1;
    }
}

and then for your button press simply do 然后按下按钮即可

public void PressClass1Button()
{
     C2.Button1_Click();
}

I'm sorry I haven't checked the actual syntax as I'm at work but this is the basic concept I normally use for this kind of problem. 对不起,我在工作时尚未检查实际语法,但这是我通常用于解决此类问题的基本概念。

It's a messy/simple solution and probably not the best! 这是一个混乱/简单的解决方案,可能不是最好的!

Hope it helps. 希望能帮助到你。

you need to delegate it , firing up event that will auto call the button click 您需要委托它,触发事件,该事件将自动调用按钮click

example : 例如:

private void delegate_You_Create(object sender, RibbonControlEventArgs args)
{
   Button1_Click(sender, args);
}

you can read about delegate here 您可以在此处了解有关委托的信息

A button click event is there to do what it says on the tin - handle the button click. 按钮单击事件可以执行其在锡盒上的指示-处理按钮单击。 The enclosed functionality is simply you saying "I'd like to do this when this happens". 随附的功能只是您说“发生这种情况时我想这样做”。

For example 例如

public void Button1_Click(object sender, RibbonControlEventArgs e) 
{
  // Do the stuff I want to do when Button1 is clicked
}

Extending this example to use a second button that does exactly the same you could add a second click event and duplicate the code like this 扩展此示例以使用功能完全相同的第二个按钮,您可以添加第二个单击事件并复制这样的代码

public void Button2_Click(object sender, RibbonControlEventArgs e) 
{
  // Do the stuff I want to do when Button1 is clicked
}

However, a better approach, avoiding the duplication, would be to pull the button stuff out into a method and then do this 但是,避免重复的更好方法是将按钮内容拉出到方法中,然后执行此操作

public void Button1_Click(object sender, RibbonControlEventArgs e) 
{
  DoButtonStuff();
}

public void Button2_Click(object sender, RibbonControlEventArgs e) 
{
  DoButtonStuff();
}

private void DoButtonStuff()
{
    // Do the stuff I want to do when my buttons are clicked
}

Extending this further you could make DoButtonStuff public and call it from elsewhere as you would with any other method. 进一步扩展此功能,可以使DoButtonStuff公开,并像使用任何其他方法一样从其他位置调用它。

This approach is better because you're capturing the thing you're interested in - the DoButtonStuff - and not the thing that caused it to happen - the button click. 这种方法更好,因为您捕获的是您感兴趣的东西DoButtonStuff而不是导致它发生的东西-按钮单击。


EDIT - In response to your static question. 编辑-针对您的静态问题。

You have a few options - 您有几种选择-

You could make DoButtonStuff static then do something like this 您可以将DoButtonStuff静态,然后执行类似的操作

// Class1
static void DoButtonStuff() { ... }

// Class2
Class1.DoButtonStuff();

You could make DoButtonStuff public then instantiate Class1 inside Class2 then do something like this 您可以将DoButtonStuff公开,然后在Class2中实例化Class1,然后执行类似的操作

// Class1
public void DoButtonStuff() { ... }

// Class2
Class1 myClass1 = new Class1();

myClass1.DoButtonStuff();

You could pull DoButtonStuff out into a separate class and call it from there something like this 您可以将DoButtonStuff拉到一个单独的类中,然后从那里调用它

// MethodClass
public void DoButtonStuff() { ... }

// Class1
MethodClass myMethodClass = new MethodClass();
myMethodClass.DoButtonStuff();

You could do the same in Class2. 您可以在Class2中执行相同的操作。

The first option is probably the simplest and may well be the best for you. 第一个选择可能是最简单的,也可能是最适合您的选择。

The second option is rarely a good idea because it couples your two classes in a way that they almost certainly shouldn't be. 第二个选项很少是一个好主意,因为它以几乎肯定不应该的方式将两个类耦合在一起。

The third option is probably the best for more complex problems, particularly if DoButtonStuff doesn't involve updating the UI - eg a save operation. 对于更复杂的问题,第三个选项可能是最好的,特别是在DoButtonStuff不涉及更新UI的情况下,例如保存操作。

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

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