Showing posts with label Windows Forms. Show all posts
Showing posts with label Windows Forms. Show all posts

Monday, December 20, 2010

C# form topmost in application scope only

I was creating a little "Find" dialog box for my recent treeview application. I wanted the box to be like in Visual Studio itself, in the fact that the find box is topmost for anything in Visual Studio, but not over the rest of the programs that are running.

In the C# form designer, you can set the form to have "Topmost = true", which places the form on top of every window in the entire Windows scope. Not quite what I wanted...

To get it to be topmost, you need to call the Show() method when displaying the form, passing to it a reference to the parent form that it will always be on top of. Like so:

FindForm frm = new FindForm();

frm.Show(this);


(FindForm is my search dialog, and this is the parent form calling it)

Now it's topmost in the application scope but not in the Windows scope!

Saturday, August 14, 2010

Windows forms styles in C#

Yesterday I was writing a nice little MDI form, and noticed something. My forms look like they were from the 80s. With blocked off group boxes instead of rounded and buttons from 3.1 days instead of the XP style they were supposed to be. On compilation and at runtime, they'd look nothing like they did in the designer.

So there were 2 ways you can get around this. Number 1 is a bit more effort, but it involves making a .exe.manifest file with the same name as your executable. In this manifest file you have some XML (http://www.dotnet247.com/247reference/msgs/15/79151.aspx, or http://www.codeproject.com/KB/cs/xpstyleui.aspx) which dictates how the controls appear. Then you have to change the "FlatStyle" property of each supported control to System.

That's great, it worked well, but it was fucking complicated.

Number 2 is much better. Call Application.EnableVisualStyles() before you launch the form. In my case, I called it before the parent MDI container was launched, and it fixed every internal form as well.

        [STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
And no having to change each control's style separately.