C# detect when form becomes unfocused? - c#

Is there a winforms event that fires when the user switches from a form to another window? I.e. Not through minimizing, but just by clicking on another window. How can I detect when the form becomes inactive? Thanks!

Form.Deactivate

Related

How to click button when out of focus

I have two Windows forms on a Program.
E.g) MainForm, SubForm
SubForm have a focus and I tried click a button on the MainForm but I had to click twice because of out of focus.
I think MainForm get focus when first click and make click event when second click so I want to making click event pass the "Getting Focus" process.
How can I do?
Sorry for bad english describing.
Thank you.
=== add photo for describing ===
GIF Image

How do I click the cross button (the cancel button) on the sub form and go to the main form?

I know how to close a sub form and go to main form by clicking "close" button under file menu bar, but I don't know how to cancel a subform and go to main form by clicking the cross button --"cancel" which is located on the upper right corner.
and then back to the main form
Update:
I have tried go to form property, under Event,there is an item called, FormClosing, write a click event funcation name,i.e cancel_Click, and then press enter. Then I can write code under cancel_click, but it rises another problem. Since I want use same code here as the one under "close" on the menu (both can redirect back to the main form), when I close on menu bar, it appears two main form. How can I fix this?
use Form.Closing event and then do what ever you want to do. link
Set the FormClosing event for the sub-form to go to the main form. Set the FormClosing event for the main form to exit the program.

AcceptButton for Different Tabs

I'm new to Visual Studio and am converting a C# console app to VS so that I can give it a GUI. The GUI piece is definitely a learning curve.
I have a main Form1 with Tab1 and Tab2. I can call this.AcceptButton, but it only appears to be at the form level. Is there a way for each tab to have an AcceptButton? I can't call this.Tab1.AcceptButton, however when I am in Tab2, the function of AcceptButton doesn't seem to trigger. Is it because the button doesn't exist on the tab that is in focus?
I could certainly forgo using AcceptButton if there's not a clean way to do this, but it would increase the usability of the application.
Thanks in advance!
You can listen to SelectedIndexChanged event on your TabControl. So whenever tab is change this event will fire and you can use assign another button to AcceptButton.
Have a look at this: Change accept button with tabs

Event when WinForm is moved behind other windows

I have a popup menu which is displayed when the user minimizes the form. However, when a user clicks on a background program such as a document of MS Word my form is behind the document but not minimized or hidden. Therefore, I cannot control this. Is there an Event on VS which can be used?
Try the Form.Deactivate event:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.deactivate%28v=vs.110%29.aspx
Description from MSDN:
Occurs when the form loses focus and is no longer the active form.
You can use Deactivate Event of a WinForm
Private Sub Form1_Deactivate(sender As Object, e As EventArgs) Handles Me.Deactivate
'Your code Here
End Sub
Use the Form.Deactivate event, it's called when the form loses focus.
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.deactivate%28v=vs.110%29.aspx

C# MDI Parent Gain Focus by clicking on MDI Parent background

I want to set focus to an MDI Parent Form when I click on the background of the form. However, the only way I can get it to set focus is when I resize the form.
I have tried using mouse click event, click event, key press event etc to manually set the focus when you click on the MDI Parent but none of these events fire. Is there ANY way to set the focus to the MDI Parent when you click on the background of the form?
That background is a separate control, try to find it in MainForm.Controls and assign it's click event.
You may want to look at the Win32 WM_MDIACTIVATE message. Now that we've discussed a possible solution, the real question can begin:
I think you should look long and hard at what your trying to accomplish. You risk (not necessarily will, but risk) creating a behavior that is abnormal and confusing to users. Why do you want to move the focus? What will you once it gets moved? How will you indicate to the user that this has been done? How will then get out of this state?

Categories

Resources