Results 1 to 4 of 4

Thread: Changing the FlatStyle property at runtime

  1. #1

    Thread Starter
    Junior Member husain's Avatar
    Join Date
    Jul 2002
    Location
    United Arab Emirates
    Posts
    31

    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]);
        }
      }
    }

  2. #2
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    There is no need for that, just include the manifest into the exe, and set the control properties to system.
    Dont gain the world and lose your soul

  3. #3

    Thread Starter
    Junior Member husain's Avatar
    Join Date
    Jul 2002
    Location
    United Arab Emirates
    Posts
    31
    Originally posted by DevGrp
    There is no need for that, just include the manifest into the exe, and set the control properties to system.
    That is a nice reply

    I could do that, but I want to do it programatically.

  4. #4
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Dude first rule of programming. KISS, keep it small simple.
    Dont gain the world and lose your soul

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width