0

I have an installer for my C# project, which requires the user to enter a license before continuing with the install. I get the license from the user by instantiating a form and then calling ShowDialog on it.

My problem is that the user may click on something else during the install. This means that the dialog may be hidden, and the installation will not proceed, until the user discovers the dialog on the taskbar. I would like to display the license dialog topmost, but I don't know how to do it. Can I somehow make the install application topmost from my BeforeInstall event handler? The installer runs with administrator rights.


Edit: It seems I was too hasty in accepting an answer. I now have code such as the following in my BeforeInstall event handler:

using (var licenseDlg = new LicenseDialog())
{
    licenseDlg.TopMost = true;
    var result = licenseDlg.ShowDialog();
    ...

The behavior is the following:

  • I try to install my project
  • I click on the UAC to allow it to install
  • I maximize a cmd window.

If I don't do anything else, then at some point the license window will pop up over my cmd window. However, if I type anything at the cmd prompt, at a rate of about 1 click per second, then the license window will not pop up, it will only show on the task bar. I would like to always have the license window pop up, even when there is activity in other windows.

Boris
  • 5,094
  • 4
  • 45
  • 71
  • @Koch: It is a hand-crafted installer. I have a custom event handler for BeforeInstall, where I show the install dialog. – Boris Jan 03 '12 at 15:00
  • possible duplicate? http://stackoverflow.com/questions/6213498/custom-installer-in-net-showing-form-behind-installer – rene Jan 03 '12 at 15:03
  • @rene: similar. My dialog shows on top of the installer, when I bring the installer on top. – Boris Jan 03 '12 at 15:09
  • Thanks all, the solution seems very obvious now. – Boris Jan 03 '12 at 15:11
  • You are sure you're not falling into this trap: http://blogs.msdn.com/b/oldnewthing/archive/2011/03/10/10138969.aspx – rene Jan 03 '12 at 15:12
  • @rene: Good point. I thought the default behavior would be to let the the latest application to set itself topmost, be the topmost, and I could live with that. – Boris Jan 03 '12 at 16:10

4 Answers4

3

When you're opening a form in the BeforeInstall event, simply set TopMost to true.

var licenseForm = new Form
                  {
                      TopMost = true
                  };
Matthias
  • 15,919
  • 5
  • 39
  • 84
  • Probably you can also call `form.ShowDialog()`. Additionally, this would suspend the installer form. – Matthias Jan 03 '12 at 15:02
  • Thanks, I do call `ShowDialog()`. – Boris Jan 03 '12 at 15:10
  • @Koch: I am sorry. I was too hasty to accept. Since everyone agreed on the solution, I thought it was the correct one, and accepted without trying it out. I have amended my question to show that just setting `TopMost = true;` does not quite solve my problem. So I have unaccepted your answer so others won't be confused, if they have my problem, and the accepted answer doesn't solve it. – Boris Jan 11 '12 at 12:10
2

Just need set Window.Topmost = true

oldmonk
  • 739
  • 4
  • 10
2
this.TopMost = true;

in the load method

Volker Mauel
  • 514
  • 3
  • 21
1

Use the TopMost property !

http://msdn.microsoft.com/en-us/library/system.windows.forms.form.topmost.aspx

Guillaume Slashy
  • 3,554
  • 8
  • 43
  • 68