naresh gautam
naresh gautam

Reputation: 15

How to perform Click-Event on a hidden button

In my C# form I have two buttons

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

The button1 is hidden at form loading, I want the logic behind button1 to be perfomed when it's hidden too.

Upvotes: 1

Views: 334

Answers (2)

Foxenstein
Foxenstein

Reputation: 84

If your Button is hidden, it seems that you need the functionality behind not or just in special cases. Keeping functionality out of events is often a simple solution to avoid problems in the future.

    private void btn_Reload_Click(object sender, EventArgs e)
    { 
      // reload here - maybe you reload all your employees from a datasource
    }

    private void btn_Reload_With_Calculation_Click(object sender, EventArgs e)
    {
      // you can use functionality here from a another button and call the 
      btn_Reload_Click(this, EventArgs.Empty); // DON'T DO THIS IN MY OPINION
      // ....
    }

Maybe this solution is better even if you need the functionality at other workflows.

    private void btn_Reload_Click(object sender, EventArgs e)
    {
      Reload();
    }

    private void btn_Reload_With_Calculation_Click(object sender, EventArgs e)
    {
      Reload();
      Calculate();
    }

    void Reload() { }

    void Calculate() { }

Upvotes: 2

高鵬翔
高鵬翔

Reputation: 2057

Just let the function outside become another function, then you can call function although you hidden the button1.

private void button1(object sender, EventArgs e)
{
  _button1();
}
private void button2(object sender, EventArgs e)
{
  _button1();
}

//Here is the function
void _button1()
{
    ...
}

Upvotes: 3

Related Questions