Changing the FlatStyle property at runtime
I wrote a couple of functions that will change the FlatStyle property of all form controls to System automatically at runtime (to give my applications a Windows XP look).
I had written it in VB actually and converted it into C#. The C# code does not work. My application compiles without any errors but the FlatStyle property does not change. I would appreciate it if anyone could help me debug this code.
Code:
// Put these two static methods in any of your classes to use them.
using System;
using System.IO;
using System.Windows.Forms;
public static void XPStyle(Form FRM)
{
if (Environment.OSVersion.Version.Major > 4 &&
Environment.OSVersion.Version.Minor > 0 &&
File.Exists(Application.ExecutablePath + ".manifest"))
{
for (int x = 0; x < FRM.Controls.Count; x++)
{
if (FRM.Controls[x].GetType() == typeof(ButtonBase))
{
((ButtonBase) FRM.Controls[x]).FlatStyle = FlatStyle.System;
}
if (FRM.Controls[x].GetType() == typeof(GroupBox))
{
((GroupBox) FRM.Controls[x]).FlatStyle = FlatStyle.System;
}
if (FRM.Controls[x].GetType() == typeof(Label))
{
((Label) FRM.Controls[x]).FlatStyle = FlatStyle.System;
}
RecursiveXPStyle(FRM.Controls[x]);
}
}
}
private static void RecursiveXPStyle(Control CTR)
{
for (int x = 0; x < CTR.Controls.Count; x++)
{
if (CTR.Controls[x].GetType() == typeof(ButtonBase))
{
((ButtonBase) CTR.Controls[x]).FlatStyle = FlatStyle.System;
}
if (CTR.Controls[x].GetType() == typeof(GroupBox))
{
((GroupBox) CTR.Controls[x]).FlatStyle = System;
}
if (CTR.Controls[x].GetType() == typeof(Label))
{
((Label) CTR.Controls[x]).FlatStyle = System;
}
if (CTR.Controls.Count > 0)
{
RecursiveXPStyle(CTR.Controls[x]);
}
}
}