How to click button when out of focus - c#

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

Related

Main form with button A to open Form A and then disable button A until Form A is closed. Do the same with B

In my Visual Studio project file: <TargetFramework>net6.0-windows</TargetFramework>
I have a Winforms application whose main form has two buttons, button A and button B.
I have coded button A so that it disables itself when clicked, launches form A, and when form A is closed, control returns to the button click event where button A is re-enabled:
btnA.Enabled = false;
FormA frmA = new FormA();
btnA.Enabled = frmA.ShowDialog(this) == DialogResult.OK;
However this does not allow the user to do anything on the main form until frmA is closed.
When button A is clicked, I still want to disable button A so that the user cannot open another form A. But I also want the user to be able to click button B on the main form and have that logic work the same way: disable button B, launch form B, re-enable button B.
What I am trying to do is give the user the ability to launch one form A and one form B from the main form. Can I accomplish that with the pattern I've described? If so, how? (Note: There could be buttons C and D later.) I would gladly consider a different/better approach. Thanks Hedge.
I found a SO solution using Show() and capturing the close event on Form A:
WinForms; detecting form closed from another form
Not much coding involved at all.

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.

Activate MessageBox dialog of the C# Win Form

I have a Tray app.
On clicking its tray-icon, it Show()s one form.
Then that form Show()s one MessageBox.
Then I'd click outside its window to make it lose Focus.
Now again, as it normally happens, when I click the form's window, the MessageBox blinks once & gets the focus.
What I want to do, is that if I click on the tray-icon, the MessageBox should again get the focus.
On the tray-icon click, doing the form.Activate() too wouldn't give focus to the MessageBox window! but activates the form window, keeping the MessageBox afloat defocused over the focused form window.
Can you help me implementing the behavior??
Thank you.
Try this:
notifyIcon.DoubleClick += delegate {
form.Activate();
form.Focus();
MessageBox.Show(form, "text", "caption");
form.WindowState = FormWindowState.Normal;
};

Click textbox to show popup, click outside to close popup

I am designing a WPF usercontrol, and there is a textbox and a popup under the textbox. I want to click the textbox, then the popup shows. If I click outside of the textbox, the popup closes.
Now the problem is how to unfocus the textbox if I click outside the usercontrol area? Is any better way to design this control? Thanks.
On click inside you should show your popup, and to close it ater click on non-user are try to use solutions from link Is there an event handler that is fired when mouse is clicked outside textbox in c# Windows Form application?

C# detect when form becomes unfocused?

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

Categories

Resources