hide form when user clicks elsewhere - c#

i have application with one form, which i developed in c#. I would like to hide the form when a user clicks somewhere else then on the form or on the notification icon (in system tray). How could i do that?

I am not 100% sure you can do it with the standard event exposed by Visual Studio in the designer but if you could attach to the DeActivate event, or form lost focus, then you can call form.Hide

Related

Notify when dialog box appears

I need to disable a button from a third-party winforms application.
I got the buttons handle using Windows API and WinSpy++, and i have no problem disabling it.
That button is in a DialogBox control. My application is also a winforms app.
How can i be notified when the dialog box appear, so i can disable that button?
Is there an event that can fire when it appears or any workaround?
Thanks guys!

WPF WebBrowser control. need to prevent it from closing within an app

I have a WPF where I am implementing a WebBrowser control and I have a list of objects that when i select them it changes the url binding for the webBrowser control and all is well.
However, if the web page I am on tries to close the Webpage, a dialog pops up where you can hit yes or no to close the "window" (the WebBrowser control)
If the user hits "yes" then the web browser control is closed and exceptions are thrown when you try to select something else in the list because the window doesn't exist anymore.
The webbrowser control does not have a method for closing exposed where i can get to it and I was able to override the "Window.Close()" method but that also prevented me from closing the main app window.
I have tried inheriting from the WebBrowser class but it is sealed so I can't inherit to add event handlers or override methods such as "Close()" or events such as "OnClosing()"
I have no control over the website because it is a third party product.
Also, the WebBrowser control is not cancelling navigation when exiting.
so my question is
A. Is it possible to intercept the dialog and always answer "no"?
or
B. Is there another way to prevent the WebBrowser control from closing even if the person says "yes" to the dialog?
So, I ended up putting the control in it's separate view and then constructing and destroying the view as a dialogue
Once I had to solve a similar situation. It was a Delphi application, with an embedded TWebBrowser component, which is essentially the same thing as the WebBrowser of .NET. The software automatically clicked on elements of the webpage, and there was a case when after the click a confirm dialog popped up. The simplest way to press the OK button automatically was to call FindWindow with the parameters of the confirm dialog, and use the window handle to send it an Enter key press by calling SendInput. I think in your case an Escape key press is needed.

How to close two forms simultaneously on the load of third form?

There are three forms in my project, first is welcome form, second is server form and third is home page.
When a specific button is clicked on welcome form, server form is loaded without closing the welcome form. Now on the click of any specific button on server form, home page should be loaded and both welcome for and server form should be closed or hidden.
When I tried this.Close(); Home_Page hp=new Home_Page(); hp.Show(); method on the click of the button of server form, only server form got hidden but Welcome form still runs in the background.
What should be done to close both Welcome form and server form on the load of Home_Page?
use events and delegates to get the job done. What you can do is, make the Welcome form subscribe for an exit event from server form. Now in server form intern subscribes to home page. So upon clicking some button in home page, raise exit event there, which calls Server forms exit events and then raise event on welcome form, so close the welcome form and then close server form.
May not be the best solution, but i am open for suggestions.
I have no idea if this will work for silverlight/wpf if your problem is related to it.
Solution of above given question is found.
On the click of button on server form, this.Hide(); should be used to hide server form and then Welcome.ActiveForm.Hide(); can be used to hide the welcome form. Then create an object of Home_Page and show that form using Home_Page hp=new Home_Page(); hp.Show();. ActiveForm makes the Welcome form the current active form and then it can be hidden.

How can I temporarily prevent a form from getting focus/activating?

my application opens n forms and the user can freely switch back and forth among these forms.
When the user decides to confirm the operations performed on one of the forms, I would like to block the other ones until this process (which can potentially open MessageBoxes and/or other forms) comes to an end.
It is not enough to disable the forms, since the user can't do anything on them, but the Activated event is fired, and this is exactly what I want to avoid.
I tried to set ControlStyles.Selectable to false to all these forms, but it doesn't work.
Just in order to make it clearer, the forms cover the whole screen, so the users activate them clicking on the taskbar. This is the situation where opening a modal form and having the confirm code executed there does not prevent the Activated event to be fired.
Try to use Form.ShowDialog() method.
You can use Form.ShowDialog Method method to display a modal dialog box in your application. When this method is called, the code following it is not executed until after the dialog box is closed.

Menu and Main app forms, correct way?

I have a main Form app. OnLoad it displays with Docstyle=Fill the main menu which is done by user control. If the user selects a choice in that Menu control, it fires an event (with one parameter Choice) which main forms reacts on.
If the choice is run the app, it closes the user control (dipose) and call method starting the app. If the choice is to quit, it calls Application.Exit. Is that alright form programmers point of view?
If you don't have to save any data and you just like to quit the application, it's the best way to do like you described. If you're using other windows you could use the collection Application.OpenForms and close all open windows.

Categories

Resources