How to do two things with one click in Windows Form

There is another (floating) window in my main form. This floating window works like a popupwindow in that it closes when the user clicks somewhere else outside of this window. This is due to a deactivation event. But what I want to do is if the user clicks on another control (say a button), I want to both close this floating point window and then activate that button with one click. Currently, the user needs to double-click (one to deactivate the window and again to activate the desired button). Is there a way to do this with one click?

0


source to share


3 answers


foreach(Control c in parentForm.Controls)
{
   c.Click += delegate(object sender, EventArgs e)
              {
                  if(floatyWindow != null && floatyWindow.IsFloating)
                  {
                       floatyWindow.Close();
                  }
              };
}

      



And then add the handlers as usual. This additional handler can close the floating window. Make sure the floating point window is not a dialog either, as this will prevent the controls of the parent form from being clicked.

+2


source


I had a bit of a hackish solution. In the Deactivate event, fire another custom event on your main form. Then when the main form handles the custom event, enumerate through your control (this.Controls) and find the control under the mouse checking all their bindings, then call Focus (). You may need to sort one by one with the smallest surface area, or you can have a separate "focus-capable" control list for this purpose.

Another way could be to switch focus to your main form right after the OnMouseLeave of the floating window or OnMouseHover of your main window, but keep the floating windows on top, not focus. Move the global mouse down the main form and close the floating point window.



These are just theories, not proven ones.

0


source


I also had this problem where the client wanted pop-ups all over the app. I used an approach similar to that described in this article:

http://www.vbaccelerator.com/home/NET/Code/Controls/Popup_Windows/Popup_Windows/article.asp

Sample code is available here:

http://www.vbaccelerator.com/home/NET/Code/Controls/Popup_Windows/Popup_Windows/Popup_Form_Demonstration.asp

Expanding this a bit, we've created floating windows similar to the ones VS uses when you get a runtime error while debugging your code.

At the very least, reading the code can give you some insight, however, a more complex solution may be a simpler solution.

0


source







All Articles