Click here to Skip to main content
15,884,007 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello all,

On my project on the uninstall process I need the user to reply a question. In order to ask that question I used a message box. But when the message box appears, it appears behind the uninstall process form so the user cant see it. I tried using bringtofront method but it didnt help. The code as follows;

C++
base.Uninstall(savedState);
            Form frm = new Form();
            frm.StartPosition = FormStartPosition.CenterParent;
            frm.TopMost = true;
            frm.Focus();
            frm.BringToFront();
            frm.TopMost = false;
            DialogResult result = MessageBox.Show(frm,"Do you want to remove the database? (This is not reversible)", "Remove Database",
                MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (result == DialogResult.Yes)
            {
                try
                {
                    masterConnection.ConnectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
                    System.Data.SqlClient.SqlCommand Command = new System.Data.SqlClient.SqlCommand("DROP DATABASE AMAZON_PRODUCT_DB", masterConnection);
                    Command.Connection.Open();
                    Command.ExecuteNonQuery();
                    Command.Connection.Close();
                    Command.Dispose();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Unable to remove database: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }


any ideas to fix this?
Posted

It's difficult to help you with this because we can't see all of the applicable code, but...

0) I don't understand why you're creating a new form to show the MessageBox.

1) Why don't you call base.Uninstall(savedState) AFTER asking the user if he wants to delete the database?

 
Share this answer
 
v2
Comments
Orcun Iyigun 29-Dec-10 20:54pm    
I created the form because of "IWindow32 owner" so that I think I can show it on the front of the setup form. Though mbox can be the child and can bring it to front by that. I tried calling the base.uninstall method after asking the user but didnt help.
Sergey Alexandrovich Kryukov 30-Dec-10 0:24am    
Using owner parameter is pretty much useless; besides, if message box goes behind, why would you hope its parent window will not go behind? -- same thing...
As John Simmons correctly put it, it's hard to help, so sorry if my advice won't work out.

There are two things:

#1: The problem is frm.Topmost. This works like "Always on top" and hides whatever is behind even if you activate that windows behind. Please try it first. If you really need frm.Topmost = true;, assign frm.Topmost = false; just for the time you show your dialog and assign it back to true when dialog is already closed.

#2: If it does not help, I would try the following:

C#
using System.Runtime.InteropServices;
//...
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);

[DllImport("user32.dll", SetLastError=true)]
public static extern IntPtr SetActiveWindow(IntPtr hWnd);


I used the combination of the to do the trick. You need hWnd. Therefore, you could use a form instead of MessageBox (why not? one problem of MessageBox is improper activation in some exotic situation like this one). You can make your form acting like the message box you want, event better and use MyForm.ShowDialog().

Will you please try and report back if it worked for you or not?

Thank you.
 
Share this answer
 
Comments
Orcun Iyigun 31-Dec-10 12:08pm    
Sorry I tried but didnt seem to be working. What I have figured out is; when I try uninsalling the application from the visiual studio the messagebox appears in the front but if try uninstalling it from the control panel=> Add/remove programs it appears behind the progress bar. Anyways I will try to figure out something.
Espen Harlinn 28-Feb-11 15:23pm    
Reasonable approach, my 5
Sergey Alexandrovich Kryukov 1-Mar-11 3:41am    
Thank you,
--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