Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear All,

I have created an windows based application.

If user click 'X' button on the top of window it will display a message box (Yes\No) to confirm the exit application.
When click on 'Close window' from task bar, focus moves to the Main UI instead of message box.

How to get the focus on message box instead of main UI, when 'close window' click from task bar?
Posted
Comments
Sergey Alexandrovich Kryukov 19-Dec-14 1:16am    
What is that, WPF, Forms, anything else? Always indicate what UI library you are using. The problem is not clear. If you show something modal, focus is always on that dialog or window.
—SA

Any time you use 'MessageBox.Show the modal dialog box put up by WinForms has exclusive focus, and it is closed (and disposed) when the user clicks one of its Buttons. When the MessageBox Dialog is closed, Application Focus should return to the last Window/Control that had Focus before the MessageBox was shown.

If you want to get the MessageBox that enables cancelling closing the Application to show when user chooses 'Close Window' in the TaskBar, here's an example; in this example, if the Main Form is minimized when the 'Close is canceled, then I reset the WindowState to 'Normal, and bring the Main Form to the front of the z-order and invoke Focus on it:
C#
using System.Windows.Forms;

namespace YourNameSapce
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        // this assumes you "connected" the MainForm to this EventHandler at design-time
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if
            (
                MessageBox.Show("Exit the Application ?",
                    "Exit ?",
                    MessageBoxButtons.OKCancel,
                    MessageBoxIcon.Warning,
                    MessageBoxDefaultButton.Button1)
                == DialogResult.Cancel
            )
            {
                // cancel
                e.Cancel = true;

                if (this.WindowState == FormWindowState.Minimized)
                {
                    this.WindowState = FormWindowState.Normal;
                }

                this.BringToFront();
                this.Focus();
            }
        }
    }
}
 
Share this answer
 
v2
As you don't present any code for how you show the message box, it is difficult to give an exact answer.

Here are a few things you can try.

1. Provide your main form as the owner of the MessageBox[^].
Usually like this:
C#
MessagtBox.Show(this, "Hello World");


2. Explore the different MessageBox options[^].
Try with MessageBoxOptions.DefaultDesktopOnly
 
Share this answer
 
Comments
[no name] 19-Dec-14 1:17am    
Are you having the form property as TopMost=True ?
sdileep1 19-Dec-14 4:07am    
No.
[no name] 19-Dec-14 8:02am    
A messagebox is a dialogform, how come you having this kind of issue.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900