Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

this.TopMost = true not working?

Tags:

c#

.net

winforms

I'm very new to C# and still trying to get my head round it (with help of some very patient friends).

I have an issue with setting a new windows form's TopMost property to true. I have two (almost) identical forms; 1 which works OK and one which doesn't.

Both of the forms have the TopMost property set to true.

  • Form1 shows the window and when I try to click behind it, the form flashes a few times and issues a windows beep.
  • Form2 also shows the form but when I click behind it, the form greys out (or loses focus) and I can click away on the main form.

I've searched for an answer to this issue and found an answer which suggested putting this.TopMost = true; in the form's load event but that didn't work.

The only thing I have changed which may or may not have had an effect is that Form1 was created with .NET 4.5 set in the properties and before creating Form2, I changed this to .NET 3.5 (client profile). I've tried changing it back but it hasn't helped. Before I delete and create Form2 again, does anyone have any ideas?

Many thanks in advance. (If you need any more information, please just let me know)

like image 977
Kungfauxn00b Avatar asked May 31 '13 16:05

Kungfauxn00b


4 Answers

It may help you;

frm.TopLevel = true;
frm.TopMost = true;
like image 164
Furkan Ekinci Avatar answered Oct 17 '22 22:10

Furkan Ekinci


The DIY-way to do it - works 100%!

public static class User32
{
    public const int SW_HIDE = 0;
    public const int SW_SHOW = 5;
    public const int SW_SHOWNORMAL = 1;
    public const int SW_SHOWMAXIMIZED = 3;
    public const int SW_RESTORE = 9;

    [DllImport("user32.dll")]
    public static extern bool SetForegroundWindow(IntPtr hWnd);
    [DllImport("user32.dll")]
    public static extern bool AllowSetForegroundWindow(uint dwProcessId);
    [DllImport("user32.dll")]
    public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
}

User32.AllowSetForegroundWindow((uint)Process.GetCurrentProcess().Id);
User32.SetForegroundWindow(Handle);
User32.ShowWindow(Handle, User32.SW_SHOWNORMAL);
like image 40
Martin.Martinsson Avatar answered Oct 17 '22 23:10

Martin.Martinsson


TopMost is a property that is used to make sure one window is always shown above all others within an application. Microsofts example was a find and replace tool.

The difference you are finding is that Form1 was created as a modal dialog through the use of ShowDialog. Show dialog makes sure that your form must be closed before all other windows in the application can be used again. For example; using a form to gain user data to enter into a parent forms database.

Show is used when you don't mind if your user has finished with their dialog or not, such as allowing your user the chance to use some utility (e.g timer, stopwatch) that will assist within the main function of a program.

The only visual difference I can think of when using different .Net frameworks, is different windows dialogs such as the OpenFileDialog, that have been updated throughout the framework

like image 15
Sayse Avatar answered Oct 17 '22 23:10

Sayse


This link from Microsoft confirm that could be a Bug in Windows 7 and Windows Server 2008 R2 I've faced it in a Windows 7 Embedded system and the provided patch fix the problem so please consider to take a look :)

http://support.microsoft.com/kb/2587473/en-us

Hope it help!

like image 3
Guillermo Gutiérrez Avatar answered Oct 17 '22 23:10

Guillermo Gutiérrez