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?
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:
Type objectType = this.textBox1.GetType();
Type propertyType;
foreach (PropertyInfo prop in objectType.GetProperties())
{
propertyType = prop.PropertyType;
if (propertyType.IsEnum)
{
MessageBox.Show(string.Format("Property: {0}\n\n",
prop.Name) +
string.Join(Environment.NewLine,
Enum.GetNames(propertyType)));
}
}