0

I was trying to create an overlay experience by using 2 forms. ie There is a form Form1 in the background which would be a bit transparent and it would open a model window Form2 using ShowDialog which would show the overlay.

This all works fine, but I wanted to close both forms when anyone clicks the Form1. I tried to add a click event handler for Form1 but that doesnt get fired when Form2 is open and we click on Form1. Instead what we get is window's way of telling us that this window is currently behind the scenes.

Is there any way of firing any event or getting any notification when Form1 is clicked.

Arun Satyarth
  • 434
  • 1
  • 4
  • 12
  • 3
    If your form2 is modal, you can't do anything in form1 until form2 is closed – T.S. Mar 24 '17 at 04:02
  • 1
    When we click on Form1, windows makes the form blink to tell us that the Form1 is unavailable right now. So I guess there must be some event firing. Would there be any way to subscribe to and get notified of that event? – Arun Satyarth Mar 24 '17 at 04:08
  • maybe this would help you. http://stackoverflow.com/questions/5701276/is-there-any-way-to-detect-a-mouseclick-outside-a-user-control – Muj Mar 24 '17 at 05:50
  • If Form1 is blinking, when clicked on when Form2 is displayed, then Form2 is a modal and Form1 will not accept any events until Form2 is closed. Instead you could add a close button on Form2 that closes only Form2 and a Quit button which quits the application in return closes both Form2 and Form1. – athar13 Mar 24 '17 at 07:11
  • 1
    If you don't need Form2 to be modal, then you can use Show method, but first you need to set the Form2's Owner to Form1. This way Form2 will be closed if you close Form1. You can also set Form2 to be topmost if you need it always on top of Form1. – VuVirt Mar 24 '17 at 07:44

1 Answers1

0

I think of a wrapper. You can create a base Form named FrmBase then Inherit both forms from FrmBase. Then you can override OnKeyPress method in the base form.

    protected override void OnKeyPress(KeyPressEventArgs e)
    {
      //You're code goes here.
       base.OnKeyPress(e);
    }
Amir Pourmand
  • 519
  • 6
  • 17