简体   繁体   English

将按钮单击事件功能移动到visual studio中的其他类? - C#

[英]Move button click event function in to other class in visual studio? - C#

How can I have a custom class ex. 我怎样才能拥有自定义课程。 named Inputs and have all my winForm button clicks and Ui elements events in it rather then having it in the main form class? 命名Inputs并在其中包含所有我的winForm按钮单击和Ui元素事件,而不是在主窗体类中使用它?

Currently I would have this in the main form class, but I want to move it to another class. 目前我会在主窗体类中使用它,但我想将它移动到另一个类。

    private void button1_Click(object sender, EventArgs e)
    {
      // Do something
    }

Well there is a couple different ways you could do this. 那么你有几种不同的方法可以做到这一点。 But you could skip the entire auto generated Click handlers and define your own as to avoid another level of Method calling. 但是您可以跳过整个自动生成的Click处理程序并定义自己的处理程序以避免另一级别的Method调用。 A very naive way to do it would be something like this: 一个非常天真的方式是这样的:

public class FormInputHandler
{
    private Form1 _form1;

    public FormInputHandler(Form1 form1)
    {
        _form1 = form1;
        _form1.Controls["button1"].Click += Button1ClickHandler;
    }

    private void Button1ClickHandler(object sender, EventArgs e)
    {
        // Do stuff
    }
}

Granted I wouldn't necessarily do this exactly. 当然,我不一定会这样做。 But I think it demonstrates the point. 但我认为这证明了这一点。 It's kind of what you are trying to do. 这就是你想要做的事情。 This way you don't have to have the all the Handler Methods in your Form1 class file. 这样,您不必在Form1类文件中拥有所有Handler方法。 Then to use something like this just do: 然后使用这样的东西只做:

(Inside your private void InitializeComponent() Method in your Form1.Designer.cs ) (在Form1.Designer.cs private void InitializeComponent()方法内)

private void InitializeComponent()
    {
        // 
        // button1
        // 

        // Generated button1 stuff goes here.

        // 
        // Form1
        // 

        // Generated Form1 stuff goes here

        // Call this at the end so that 
        // everything is already added to the form.
        AttachInputHandler();
    }

    #endregion

    private void AttachInputHandler()
    {
        this._inputHandler = new FormInputHandler(this);
    }

    private System.Windows.Forms.Button button1;
    private FormInputHandler _inputHandler;

Here is a solution based on @Spool 's answer, that works the way I originally imagined it. 这是一个基于@Spool的答案的解决方案,它按照我最初想象的方式工作。

(However I am not sure it's the actually solution I will use) : (但我不确定这是我将使用的实际解决方案)

In main Form1 class: 在主Form1类中:

using System.Windows.Forms;

namespace InputFormTest
{
    public partial class mainForm : Form
    {
        public mainForm()
        {
            InitializeComponent();
            InitializeInputs();
        }

        void InitializeInputs()
        {
            InputHandler.mainForm = this;
            InputHandler.debugBox = debugBox;
            InputHandler.mainForm.Controls["button1"].Click += InputHandler.OnButton1Click;
            InputHandler.mainForm.Controls["button2"].Click += InputHandler.OnButton2Click;
            ((NumericUpDown)InputHandler.mainForm.Controls["numUpDown1"]).ValueChanged += InputHandler.OnNumUpDown1Changed;
        }
    }
}

And the InputHandler Class: 和InputHandler类:

using System;
using System.Windows.Forms;

namespace InputFormTest
{
    public static class InputHandler
    {
        public static Form mainForm;
        public static RichTextBox debugBox;

        public static void OnButton1Click(object sender, EventArgs e)
        {
            debugBox.AppendText("Button1 Clicked \n");
        }

        public static void OnButton2Click(object sender, EventArgs e)
        {
            debugBox.AppendText("Button2 Clicked \n");
        }

        public static void OnNumUpDown1Changed(object sender, EventArgs e)
        {
            debugBox.AppendText($"Num value: {((NumericUpDown)sender).Value} \n");
        }
    }
}

WinForm截图

Download VS Project 下载VS Project

I think this is what you are looking for: 我认为这就是你要找的东西:

Create your input class and define your method (the parameters and the return value have to be acceptable to the event!) 创建input类并定义方法(参数和返回值必须是事件可接受的!)

static class input
{    
    public static void button1_Click(object sender, EventArgs e) 
    {
        System.Windows.Forms.MessageBox.Show("test");
    }
}

Then go to your From1 or whatever your MainForm is. 然后转到你的From1或任何你的MainForm

And then add it (I recommend in the InitializeComponent(); method or in the constructor) 然后添加它(我建议在InitializeComponent();方法或构造函数中)

    private void InitializeComponent()
    {
        this.button1 = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(171, 50);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(75, 23);
        this.button1.TabIndex = 1;
        this.button1.Text = "button1";
        this.button1.UseVisualStyleBackColor = true;

        // Here you add the event handler
        this.button1.Click += input.button1_Click;

        //...
    }

And you're done! 而且你已经完成了!

Hope this helps. 希望这可以帮助。

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

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