Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created Windows Form based application using c#.

I have a button which is 'Cancel' on Main Form.
When user selects 'Cancel' button, it displays a message box (yes/No).
<pre lang="c#">MessageBox.Show(this,"Are you sure to exit the application", "Title", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);


Until user selects either 'Yes' or 'No' message box still appear on the main form.

Now, if user selects 'X' Close window from Task bar, focus is shifting from message box to Main form.

How to show the focus on message box in the above scenario.

Could you please anyone help on this.

Thanks in advance.
Posted
Updated 30-Mar-19 13:53pm
v4

This code (VB.NET , I know) should work like you want :
VB
Protected Overrides Sub OnFormClosing(e As System.Windows.Forms.FormClosingEventArgs)
    Dim myResult As DialogResult = MessageBox.Show("FormClosing-Message", "Caption-Text", MessageBoxButtons.YesNo)

    If myResult = Windows.Forms.DialogResult.No Then
        e.Cancel = True
    End If
    MyBase.OnFormClosing(e)
End Sub


Update to get the MessageBox ALLWAYS TopMost :

VB
Protected Overrides Sub OnFormClosing(e As System.Windows.Forms.FormClosingEventArgs)
    Dim myResult As DialogResult = MessageBox.Show(Me, "FormClosing-Message", "Caption-Text", MessageBoxButtons.YesNo)

    If myResult = Windows.Forms.DialogResult.No Then
        e.Cancel = True
    End If
    MyBase.OnFormClosing(e)
End Sub
 
Share this answer
 
v3
Comments
lukeer 22-Jul-15 3:33am    
That's what OG meant after "AH!" (I think). Thanks for explicating.
Ralf Meier 22-Jul-15 3:53am    
You are right ...
That was explained by OG - but I supposed that the inquirer not realized it in the right way - so I wrote it as code.
sdileep1 22-Jul-15 3:35am    
I tried this too. but no use. Issue still exist by clicking task bar close window icon before doing any action on message box.
Ralf Meier 22-Jul-15 3:40am    
Please post your actual code.
I have tested it and it's working ... (also with the Taskbar-CloseIcon)
When I try this (having fixed your code so it compiles):
C#
DialogResult result = MessageBox.Show(this, "Please confirm", "Are you sure to exit the application", MessageBoxButtons.YesNo);
Or
C#
DialogResult result = MessageBox.Show("Please confirm", "Are you sure to exit the application", MessageBoxButtons.YesNo);
The result is the same: you can't press the "X" button to close the dialog, it's greyed out.
And trying to click the "X" button on the main form doesn't work either - because the dialog box is modal and the main form won't respond until it's closed.

So what exactly are you doing differently - because I suspect your code doesn't look like what you showed us...

"I think he is clicking on the taskbar icon that offers tiny red X to close the app "from outside""

"yes"


AH!

In which case, it's simple: handle the FormClosing event and show the message box from that: you can cancel the close via the FormClosingEventArgs.Cancel property


"
SQL
I have tried that. But, issue is still exist.
In 'Cancel' button event, i have added FormClosingEventArgs.Cancel= false;
Then In Form Closing event, added the Message box to display.

When i clicked on cancel button, message box displayed. Then again i have clicked on task bar icon close window, focus is moving from message box to Main Window.

"


I tried it myself by modding an existing app:
C#
/// <summary>
/// Save the size and location (if the used doesn't
/// override it)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
    {
    DialogResult result = MessageBox.Show("Please confirm", "Are you sure to exit the application", MessageBoxButtons.YesNo);
    if (result != DialogResult.Yes)
        {
        e.Cancel = true;
        return;
        }
    if ((ModifierKeys & Keys.Shift) == 0)
        {
        this.SaveLocation();
        }
    }

I run the app, hover the mouse over the taskbar and click "X" - the message box appears.
If I then hover the mouse over the taskbar again, I get two preview windows and I can switch the focus to the "main" form from the dialog box but I can't do anything with it - All I get is a default beep if I try.

This is a "feature" of windows I suspect, and there is no real way to get rid of it.

So...You will have to work round it.
Bear with me here...

Start by adding a new form to your project - call it MyMessageBox
Set the caption, add a label to prompt the user to close it, and add two buttons: call one "butYes", the other "butNo" and set the text appropriately. Set the DialogResult property for each button to "Yes" and "No" respectively.
Set the form properties "AcceptButton" to "butYes", and "CancelButton" to "butNo".
Set the form properties "ShowInTaskBar" to "false", and "StartPosition" to "CenterParent".

Now, back to your main form, and add two private class variables:
C#
private bool checkClosing = false;
private MyMessageBox closeConfirm = null;

Change your FormClosing event handler:
C#
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
    {
    checkClosing = true;
    closeConfirm = new MyMessageBox();
    DialogResult result = closeConfirm.ShowDialog();
    checkClosing = false;
    closeConfirm = null;
    if (result != DialogResult.Yes)
        {
        e.Cancel = true;
        return;
        }
    }
(Technically, you only need the closeConfirm variable, but...)
Now add a new form event handler: Activated
C#
private void frmMain_Activated(object sender, EventArgs e)
    {
    if (checkClosing && closeConfirm != null)
        {
        closeConfirm.Activate();
        }
    }

Compile and try it.
 
Share this answer
 
v3
Comments
sdileep1 21-Jul-15 6:41am    
yes. I have updated my question above.
Thanks.
I mean to say X Close window from task bar is not greyed out.
Sinisa Hajnal 21-Jul-15 6:44am    
I think he is clicking on the taskbar icon that offers tiny red X to close the app "from outside"
sdileep1 21-Jul-15 6:45am    
Yes.
OriginalGriff 21-Jul-15 6:52am    
Answer updated.
sdileep1 22-Jul-15 1:59am    
I have tried that. But, issue is still exist.
In 'Cancel' button event, i have added FormClosingEventArgs.Cancel= false;
Then In Form Closing event, added the Message box to display.

When i clicked on cancel button, message box displayed. Then again i have clicked on task bar icon close window, focus is moving from message box to Main Window.

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