3 Replies - 324 Views - Last Post: 29 September 2017 - 09:31 AM

#1 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 242
  • Joined: 29-October 16

Best and/or Easiest Way to Trigger One Click Event From Another

Posted 21 September 2017 - 07:49 AM

Hey all,
hope you have all been well. I am wondering what you would recommend if I want to trigger one click event with another?

Thanks in advance!
Is This A Good Question/Topic? 0
  • +

Replies To: Best and/or Easiest Way to Trigger One Click Event From Another

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13567
  • View blog
  • Posts: 54,133
  • Joined: 12-June 08

Re: Best and/or Easiest Way to Trigger One Click Event From Another

Posted 21 September 2017 - 07:59 AM

Huh?
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5928
  • View blog
  • Posts: 20,272
  • Joined: 05-May 12

Re: Best and/or Easiest Way to Trigger One Click Event From Another

Posted 21 September 2017 - 08:25 AM

In general doing that is not recommended. You should instead refactor the code so that the code in the click event is exposed and can be called.

C# example to make code more readable:
void btnFoo_Click(object sender, EventArgs e)
{
    MessageBox.Show("Foo clicked!");
    btnBar.PerformClick();
}

void btnBar_Click(object sender, EventArgs e)
{
    MessageBox.Show("Bar clicked!");
}


to
void DoFoo()
{
    MessageBox.Show("Foo clicked!");
}

void DoBar()
{
    MessageBox.Show("Bar clicked!");
}

void btnFoo_Click(object sender, EventArgs e)
{
    DoFoo();
    DoBar();
}

void btnBar_Click(object sender, EventArgs e)
{
    DoBar();
}


Was This Post Helpful? 1
  • +
  • -

#4 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 242
  • Joined: 29-October 16

Re: Best and/or Easiest Way to Trigger One Click Event From Another

Posted 29 September 2017 - 09:31 AM

View PostSkydiver, on 21 September 2017 - 08:25 AM, said:

    btnBar.PerformClick();




Hey thanks Skydiver! I ended up using this. Just had to look up the C++ version of it. I know, I know, I'm a bad boy. LOL just trying to be as pragmatic as possible here. The performclick works fine for what I'm doing. The only reason I didn't end up doing what you had recommended was that the code is already designed inside of the buttons so there is no reason to copy paste, also the program structure would have to be changed. I do appreciate the help and as always, here is the C++ code:

private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) 
{
	button1->PerformClick();
}



Clicking button 2 will perform the same clickarg effictively as clicking button 1 would do normally. I think that I am going to eventually use a timer trigger so clicking the button will trigger a timer and then it will click button1 after a second. The reason for this is that I am generating powershell scripts then using shellexec to run immediatly after they are created. The script that button2 uses is dependent on button1 having already been clicked. The problem is that when they both are clicked at the exact same moment the first script has not been created yet so when it tries to run there is no .ps1 file generated and thus nothing happens. So I think that I will use a timer to run the second script. Button will be clicked, first script is generated, 1 second passes, then the dependent script runs a second after.

Thanks again skydiver, fellow hexagoner
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1