views:

152

answers:

2

Hi all,

Can anyone explain me the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace web.frmcolor
{
  public class FormEx : Form
  {
    /// <summary>
    /// Set the default color for the designer
    /// </summary>
    static FormEx()
    {
      _globalBackgroundColor = default(Color?);
    }

    private static void InvalidateForms()
    {
      try
      {
        for (int i1 = 0; i1 < Application.OpenForms.Count; i1++)
        {
          try
          {
            FormEx frm = (Application.OpenForms[i1] as FormEx);
            if (frm != null)
            {
              frm.Invalidate(true);
              frm.Refresh();
            }
          }
          catch 
          { 
            //Should never happen
          }
        }
      }
      catch
      {
        //this will catch if the form count changes
      }
    }

    private static Color? _globalBackgroundColor;
    /// <summary>
    /// Sets the background color for all forms
    /// </summary>
    public static Color? GlobalBackgroundColor
    {
      get { return FormEx._globalBackgroundColor; }
      set 
      {
        if (FormEx._globalBackgroundColor != value)
        {
          FormEx._globalBackgroundColor = value;
          InvalidateForms();
        }
      }
    }

    public override Color BackColor
    {
      get
      {
        return (_globalBackgroundColor == null ? base.BackColor : (Color)_globalBackgroundColor);
      }
      set
      {
        base.BackColor = value;
      }
    }

    /// <summary>
    /// Create a new colored form
    /// </summary>
    public FormEx()
      : base()
    {
    }

    private void InitializeComponent()
    {
        this.SuspendLayout();
        // 
        // FormEx
        // 
        this.ClientSize = new System.Drawing.Size(292, 266);
        this.Name = "FormEx";
        this.Load += new System.EventHandler(this.FormEx_Load);
        this.ResumeLayout(false);

    }

    private void FormEx_Load(object sender, EventArgs e)
    {

    }


  }
}

Since I am a beginner I am not able to understand how the above coding works. I found this coding while browsing through internet.

Can anybody help me with the explanation for the coding?

Thanks in advance!

+2  A: 

Basically, provide you with a very quick way to change the background of all windows in the application to a common color.

The important part is the private static ... and the public static ...

To change the background of all open form you do:

FormEx.GlobalBackgroundColor = ...some color here..

It will go through each windows belonging to the application and change their the background colors (basically Invalidate will force it to repaint itself).

Jimmy Chandra
Thanks for your reply.But why we are using the question mark :_globalBackgroundColor = default(Color?);In the above coding wat does the question mark indicate?
Sheetal
that denoting that its a nullable type
Umair Ahmed
Ah... hehe it's a nullable.. So it means that particular variable might be null or contain a value of the type Color
Jimmy Chandra
See: http://msdn.microsoft.com/en-us/library/1t3y8s4s(VS.80).aspx
Jimmy Chandra
See http://en.csharp-online.net/Nullable_type
Umair Ahmed
+3  A: 

The ? means that Color should be Nullable. SInce Color is an Enum, it normally isn't nullable, it is a value type (Check this out for an explanation of Value Types and Reference Types). Adding the ? means that just in this piece of code the variable can be set to null. An explanation of Nullable Types can be found here. Furhtermore the default(Color?) statement initializes the variable to the default value of Color?, which is probably white, or because of the ?, null

Colin
Thanks for your prompt reply. Then what is the need to use for loop?
Sheetal
The for loop goes through all Open Forms it can find in the current Application context, then sets the already open Forms' Background Color to the newly selected one. FormEx is a class derived from the standard .NET Framework Windows.Forms.Form class.
Colin
frm.Invalidate and frm.Refresh are called to force what is called a RePaint, that means the Form is rendered again (so the new BackGroundColor will be shown.)
Colin