none
How to get the attributes of the ToolStripMenuItem by it's name? RRS feed

  • 問題

  • Hello,

    If I know the TooStripMenuItem's name, how can I retrieve its attributes ?

    For example,

    The ToolStripMenuItem's Name = "helpToolSttipMenuItem".

    How can I just use the name in String and get its instance or all its attributes in Reflection ?

    Please help or show me a simple example ?

    Best regards,
    Eric

    2007年5月18日 下午 02:15

所有回覆

  • HI,

     

    Assume there is a TextBox object name:txtTest, then you can do like the following

     

    Type t=txtTest.GetType();
       foreach (PropertyInfo pi in t.GetProperties())
       {
           string s = "";
           if (pi.GetValue(txtTest, null) != null)
           {
               listBox1.Items.Add(String.Format("Name={0}, Value={1}", pi.Name, pi.GetValue(txtTest, null).ToString()));
           }
           else
           {
               listBox1.Items.Add(String.Format("Name={0}, Value={1}", pi.Name, "null"));
           }
       }

     

    tihs

    2007年5月18日 下午 03:20
  • Hello,

    Thanks for your help. But how about if I only know the name of "txtTest" as a String but not the instance name. How can I use reflection get those informations ?

    Best regards,
    Eric

    2007年5月19日 上午 01:26
  • HI,

     

    If the name you said in String is Object Name, the code is OK

     

    tihs

    2007年5月21日 上午 01:24
  • hi

     

    If you only know the name of the control, so, you must query all control inside your form, include the control in the panel in the form. compare the name of each control, found it then using above code to return the data you want to have.

     

    please note that Reflection impact the application performance. If you can, maybe hold an reference of the control is a good idea.

     

    2007年6月13日 下午 04:13