Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am creating a Windows Application using a custom Messagebox from a tutorial that I found here on codeproject(A Custom Message Box[^])

I have a window that is opened from a main window. The main window must remain in the background at all times. The second window has a button. When I click the button, it displays the messagebox. Once I click on one of the buttons on the messagebox it pushes my main window behind any open application on my computer. What is causing this and how do I keep this from happening?

Any help would be appreciated.

Here is the code on the second window:

C#
try
{
    //_viewModel.SendSMSToRecipient(txtRecipient.TextBoxText, txtMessage.Text);
    DialogResult result = MsgBox.Show("Message sent successfully! Would you like to send another?", "Success!", MsgBox.Buttons.YesNo, MsgBox.Icon.Info,
                                       MsgBox.AnimateStyle.FadeIn);

    if (result == DialogResult.No)
    {
        this.Close();
    }
}
catch (Exception ex)
{
    MessageBox.Show("An Unexpected Error Occurred! Details: " + ex.ToString(), "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}


The code for the messagebox can be found with the link provided at the top.
Posted
Comments
Philippe Mori 25-Apr-15 20:40pm    
Is the second windows displayed modally or modeless and does it has an owner?

1 solution

You cannot just say "the main window must remain in the background at all times". In some cases, it must not, otherwise it would be, in the pretty unlikely cases when you succeed, a direct violation of expected UI style. And remember that your application is not along on the desktop.

Your form can always stay on the back in Z-order relative to other forms in just two cases:
  1. All other forms are only shown in modal state via the call to Form.ShowDialog. This is not the most flexible solution; and overusing of those modal forms can become very irritating, but this is the most stable and legitimate design. Please see: https://msdn.microsoft.com/en-us/library/c7ykbedk(v=vs.110).aspx[^].
  2. All other forms could use always-on-top style, which is achieved using the property Form.Topmost. This solution can be recommended only in extremely rare cases. Please see: https://msdn.microsoft.com/en-us/library/system.windows.forms.form.topmost%28v=vs.110%29.aspx[^].

    One of such rare cases would be a virtual keyboard, which is a pretty exotic and relatively rarely used kind of product. In my past answer, I explained the reason why it could be useful: Application focus getting and losing[^].

    In most other cases, Topmost is best avoided.


Also, it could be a very good idea to develop application using only one main form, not counting very small of modal forms of limited use. Instead of using other non-modal form, you implement similar behavior, where some panels or tab pages play the role of other forms. You can even develop docked UI like that in Visual Studio, but in very many cases, simple TabControl does a good job.

And finally, (sight), lets get to the multiple non-modal form. A main form, if activated, must hide other forms under its bounding rectangle. And so should do any other form. Not doing so would be a serious abuse.

At the same time, it's good to make sure that none of the windows not belonging to your application can appear between you forms in Z-order. This is done by having owner-owned relationship to bind all the non-modal form. It would be quite enough to make the main form an owner of all other non-modal forms. Please see:
https://msdn.microsoft.com/en-us/library/system.windows.forms.form.owner(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.form.ownedforms(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.form.addownedform(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.form.removeownedform(v=vs.110).aspx[^].

Also, it would be good (at least in most typical cases), to allow only the main form (owner) to appear in the taskbar. This is how: https://msdn.microsoft.com/en-us/library/system.windows.forms.form.showintaskbar(v=vs.110).aspx[^].

—SA
 
Share this answer
 
v2
Comments
Christopher Smit 25-Apr-15 4:36am    
What I meant with at the top at all times, was that when the second form is closed the main form should not be hidden behind open applications on my computer. When I use the default message box this does not happen, but when I use my custom message box, the form is hidden.
Sergey Alexandrovich Kryukov 25-Apr-15 5:42am    
It should not be closed by other applications. This is solved by having the forms owned by main form. Read the solution carefully, and you will get it. Set this.Owner = MainForm for all other forms.
Are you going to accept it formally?
—SA
Philippe Mori 25-Apr-15 20:38pm    
It might be that the custom message box that you are using is not properly implemented. Have you tried to specify the owner. Specifying the owner really help to avoid issue like that. Maybe the custom message box was only tested when it is the only form or maybe it select the wring owner when not specified and the application has multiple forms. If something goes wrong, then probably that it assume that the main application is the owner but since it already display a modal form, that last one should really be the owner of the message box. By having the proper owner, it will works properly.

Have you tryed to display another form instead? Thy might give an hint if it is your code that it os incorrect or custom message box code. Knowing that would be really useful to help focus in the correct direction.
Sergey Alexandrovich Kryukov 25-Apr-15 20:52pm    
Ownership really solves such problems.
—SA

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