Results 1 to 2 of 2

Thread: need help listing possible values of a property

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2007
    Posts
    1

    need help listing possible values of a property

    alright, i have been banging my head against this for days and i think i must be missing something.

    the problem:

    I want to be able to list the possible values of a property of a control in a listbox.

    for example, lets say I throw it a textbox and the property I want to investigate is the "TextMode" property.
    I want the list box to list the possible values of the textbox TextMode possibilites ("SingleLine, MultiLine, Password).

    I am using a barcode control and I really don't want to have to write case statements for all 200+ versions of symbology.

    here is how far i am now:
    Code:
    Type type = TextBox1.GetType();
            
    
            System.Reflection.PropertyInfo[] properties = type.GetProperties();
    
            foreach (System.Reflection.PropertyInfo property in properties)
            {
                                
                propertyList.Items.Add(new ListItem(property.Name));
              
    
            }
    this will list all the available properties
    and this:
    Code:
    string propertyName = "TextMode";
            System.Reflection.BindingFlags flags = System.Reflection.BindingFlags.GetProperty;
            System.Reflection.Binder binder = null;
            object[] args = null;
    
            object result = type.InvokeMember(propertyName, flags, binder, TextBox1, args);
    
            valueText.Text = result.ToString();
    will only give me the current value of that property, not the possible values.


    so.. any help?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: need help listing possible values of a property

    You're talking about a property whose type is an enumeration, so you need to get that enumeration and then get its values:
    C# Code:
    1. Type objectType = this.textBox1.GetType();
    2. Type propertyType;
    3.  
    4. foreach (PropertyInfo prop in objectType.GetProperties())
    5. {
    6.     propertyType = prop.PropertyType;
    7.  
    8.     if (propertyType.IsEnum)
    9.     {
    10.         MessageBox.Show(string.Format("Property: {0}\n\n",
    11.                                       prop.Name) +
    12.                         string.Join(Environment.NewLine,
    13.                                     Enum.GetNames(propertyType)));
    14.     }
    15. }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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