none
Winforms-how to know the control type

    Question

  • Hi,

    I have created the function as :
    private void SetProperties(Control cntrl, Dictionary<string, Object> propColl)

     

    {  for (int i = 0; i < propColl.Count(); i++)
                {
                   
      if (propColl.ContainsKey("font"))
                    {
                        int t = Int32.Parse((string) propColl["FontSize"]);
                        System.Drawing.Font fnt = new Font((string) propColl["font"], t,
                                                             ( propColl.ContainsKey("FontStyle")
                                                             ? (FontStyle) propColl["FontStyle"]
                                                             : FontStyle.Regular
                                                              )
                                                           );
                        cntrl.Font = fnt;
                    }
                    if (propColl.ContainsKey("ForeColor"))
                    {
                       cntrl.ForeColor = (Color) propColl["ForeColor"];

                    }
                    if (propColl.ContainsKey("BackColor"))
                    {
                      System.Drawing.ColorConverter colConvert = new ColorConverter();
                        cntrl.BackColor =(Color) propColl["BackColor"];

                    }

                    if (propColl.ContainsKey("FlatStyle"))
                    {
                        if (cntrl.GetType() == typeof (Button))
                        {
                            ((Button) cntrl).FlatStyle = (FlatStyle) propColl["FlatStyle"];
                        }
                        if (cntrl.GetType() == typeof (Label))
                        {
                            ((Label) cntrl).FlatStyle = (FlatStyle) propColl["FlatStyle"];
                        }
                        if (cntrl.GetType() == typeof (RadioButton))
                        {
                            ((RadioButton) cntrl).FlatStyle = (FlatStyle) propColl["FlatStyle"];
                        }
                        if (cntrl.GetType() == typeof (CheckBox))
                        {
                            ((CheckBox) cntrl).FlatStyle = (FlatStyle) propColl["FlatStyle"];
                        }
                        if (cntrl.GetType() == typeof (GroupBox))
                        {
                            ((GroupBox) cntrl).FlatStyle = (FlatStyle) propColl["FlatStyle"];
                        }

                    }

                }
         
                if (propColl.ContainsKey("BorderStyle"))
                {
                    if (cntrl.GetType() == typeof (Label))
                    {
                        ((Label) cntrl).BorderStyle = (BorderStyle) propColl["BorderStyle"];
                    }


                }
       
                if (propColl.ContainsKey("TextAlign"))
                {
                    if (cntrl.GetType() == typeof (Label))
                    {
                        ((Label) cntrl).TextAlign = (ContentAlignment) propColl["TextAlign"];
                    }
                    if (cntrl.GetType() == typeof (Button))
                    {
                        ((Button) cntrl).TextAlign = (ContentAlignment) propColl["TextAlign"];

                    }
                    if (cntrl.GetType() == typeof (CheckBox))
                    {
                        ((CheckBox) cntrl).TextAlign = (ContentAlignment) propColl["TextAlign"];

                    }
                    if (cntrl.GetType() == typeof (RadioButton))
                    {
                        ((RadioButton) cntrl).TextAlign = (ContentAlignment) propColl["TextAlign"];

                    }

                }

    I am calling above function like:
    SetProperties(ctrls, prop);

    where ctrls is any type of control like textbox, radiobutton ,checkbox..etc


    I believe this is not efficient code.
    I have to repeated statement for differnet controls
      if (cntrl.GetType() == typeof (RadioButton))
                    {
                        ((RadioButton) cntrl).TextAlign = (ContentAlignment) propColl["TextAlign"];

                    }

    Is there any way to acheive this easily ??

    Thanks,
    Rao

    Wednesday, December 02, 2009 1:13 AM

Answers

  • Hi,

    You can use reflection.

    Here is the code.

            private void SetProperties(Control cntrl, Dictionary<string, Object> propColl)
            {
                foreach (KeyValuePair<string, object> kvp in propColl)
                {
                    PropertyInfo pi = cntrl.GetType().GetProperty(kvp.Key);
                    if (pi != null)
                    {
                        pi.SetValue(cntrl, kvp.Value,null);
                    }
                }
            }


    • Marked as answer by NevadaVegas Wednesday, December 02, 2009 7:51 AM
    Wednesday, December 02, 2009 4:39 AM

All replies

  • Hi,

    You can use reflection.

    Here is the code.

            private void SetProperties(Control cntrl, Dictionary<string, Object> propColl)
            {
                foreach (KeyValuePair<string, object> kvp in propColl)
                {
                    PropertyInfo pi = cntrl.GetType().GetProperty(kvp.Key);
                    if (pi != null)
                    {
                        pi.SetValue(cntrl, kvp.Value,null);
                    }
                }
            }


    • Marked as answer by NevadaVegas Wednesday, December 02, 2009 7:51 AM
    Wednesday, December 02, 2009 4:39 AM
  • Thank you very much..

    Its working..
    Wednesday, December 02, 2009 7:52 AM