0

I want to catch the click event of the button which is clicked on Windows Form Application lets say in Application A and I want to catch it in Application B which is also a Windows Form Application.

I have created both the Applications and added the button in Application A. Now when the button is clicked in Application A, I want to catch the event in Application B.

Please guide.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Faran Saleem
  • 404
  • 1
  • 7
  • 31
  • 1
    Possible duplicate of [Listen for events in another application](https://stackoverflow.com/questions/17878/listen-for-events-in-another-application) – Sinatr Jun 13 '19 at 07:51

3 Answers3

2

There isn't a "quick" way to share .NET events between different processes. You need to catch the event in the same application which raised it, implement an inter-process communication mechanism (WCF, socket, ...) between Application A and application B and use it to send the event data from A to B.

See this question and related answers for more details: Listen for events in another application

MoNsTeR
  • 63
  • 4
0

You can use SetWinEventHook or a WH_MOUSE_LL hook (P/Invoke)

(tested on Windows 10, with VS 2015)

(also IUIAutomationEventHandler but more complicated)

Castorix
  • 1,465
  • 1
  • 9
  • 11
  • Available in .Net: [Automation.AddAutomationEventHandler](https://learn.microsoft.com/en-us/dotnet/api/system.windows.automation.automation.addautomationeventhandler). It can be added to a Form/Window, specifying the whole SubTree of descendants elements, along with an event type. – Jimi Jun 13 '19 at 07:48
0

I can explain you, how you may catch the Button Click event a little bit otherwise.

You can create the instance of your 2nd Form in your first one or in a reachable Class, then calling a Public Method in your 2nd Form or in your class, which does what you want.

Example Form 1:

var _secondForm = new Form2();

private void Form1_Load(object sender, EventArgs e)
{
    _secondForm.Open();
}

private void button1_Click(object sender, EventArgs e)
{
    MethodInSecondForm()
}

and your Second Form:

public void MethodInSecondForm()
{
    // Event here
}

You can create this instance in a Class, too and make it easier.

LoaStaub
  • 63
  • 14