1

I have written a program that amounts to some thousand lines of code and many buttons. I now wish to simulate this code from another Button. I've write a smaller program below to simulate what I want to do. I've looked at other examples it seems as if it would be quite simple to do but HOW!

Button1.PerformClick();

Wont compile, But how to Simulate a button click?

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");
        }
    }
}
4
  • Do you want to simulate mouse click event or run event click event handler only? Commented Jun 20, 2017 at 4:24
  • Do you want to simulate mouse click even. Yes I think? I would like to reuse code rather than go from 1 Thousand lines to 5 thounds, and this is at runtime pls Commented Jun 20, 2017 at 4:28
  • 1
    Where's your button1 coming from? Did you have a typo? Show as the code for your designer.cs Commented Jun 20, 2017 at 4:28
  • Me Human input will triger button 3 I would then like to have buton 1 code execute and Buttion 2 code Commented Jun 20, 2017 at 4:31

1 Answer 1

2

I think you made a spelling mistake. It should be button3 instead of Button3. The same code worked for me:

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();
    }      
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.