如何在c#中模拟mouse/Button点击

How to Simulate a mouse/Button Click in c#

我写了一个程序,有几千行代码和很多按钮。我现在希望从另一个按钮模拟这段代码。我在下面编写了一个较小的程序来模拟我想做的事情。我看过其他示例,看起来似乎很简单,但是怎么做!

Button1.PerformClick();

不会编译,但是如何模拟按钮点击?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp13
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //Some simulation code clcick button 2/3
            //Button1.PerformClick();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Button1.Clicked");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Button2.Clicked");
        }
    }
}

我想你犯了一个拼写错误。它应该是 button3 而不是 Button3。相同的代码对我有用:

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

    private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Button1.Clicked");
    }

    private void button2_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Button2.Clicked");
    }

    private void button3_Click(object sender, EventArgs e)
    {
        button1.PerformClick();
    }      
}