views:

41

answers:

3

i am using .net win forms i need to set some common properties globally to my win forms like css in web application

ex

form background color=red 
button width =100
Text box width=200
font family=arial

how to do this?

+1  A: 

Use the App.Config file or the Settings tab in project properties.

rdkleine
+2  A: 

how about create a base form that all the other forms inherit. On the base form you can set the common look and feel. then if it is necessary to overwrite the common properties you can do so.

EDIT something like this for the base form.

public partial class BaseForm : Form
{
    private Font _font = new Font("Arial", 10);
    private Color _backColor = Color.Red;
    public BaseForm()
    {
        InitializeComponent();
    }

    public override Font Font
    {
        get { return _font; }
        set { _font = value; }
    }
    public override Color BackColor
    {
        get { return _backColor; }
        set { _backColor = value; }
    }
}

and this for the form that you want to display

public partial class Form1 : BaseForm
{
    public Form1()
    {
        InitializeComponent();
    }
}
Nathan Fisher
which property i have to use to set baseform class for a form..can u please elaborate... i am new in this
Ayyappan.Anbalagan
A: 

You could create a static class to store them - maybe in a Dictionary perhaps.

Something like this could work:

public static class GlobalData
    {
        private static Dictionary<string, object> settings;

        private static void SetDefaults()
        {
            settings = new Dictionary<string, object>();
            settings.Add("BackgroundColour", "Red");
            settings.Add("Width", 100);
        }

        public static Dictionary<string, object>  FormSettings
        { 
            get {

                if (settings ==null)
                {
                    SetDefaults();
                }
                return settings;

            }

        }



    }

EDIT:

You could you use it like this:

this.Width = Convert.ToInt32(GlobalData.FormSettings["Width"].ToString());
Barry
thaks ...where i have to call this function.and what parameter i have to pass from there
Ayyappan.Anbalagan
@Ayyappan.Anbalagan - I have updated my answer, thanks Barry
Barry