|
Hi,
I have a TextBox, a Button and a PropertyGrid. The PropertyGrid shows the properties of the TextBox. By pressing the Button, I want to know all properties, which have not the default-value (bold entries). Is there any way to get all properties, which have not the default-value?
Thanks, Dennis.
|
| a__DENNIS__ Wednesday, September 26, 2007 1:37 PM |
This might be quite tricky to get right, PropertyGrid allows lots of doodads with custom TypeConverters and TypeDescriptors. This seemed to work pretty well though:
private void Form1_Load(object sender, EventArgs e) { propertyGrid1.SelectedObject = textBox1; PropertyDescriptorCollection props = TypeDescriptor.GetProperties(propertyGrid1.SelectedObject); foreach (PropertyDescriptor prop in props) { if (!prop.IsBrowsable) continue; if (prop.ShouldSerializeValue(propertyGrid1.SelectedObject)) Console.WriteLine(prop.Name); } }
|
| nobugz Wednesday, September 26, 2007 4:18 PM |
GetValue() works:
propertyGrid1.SelectedObject = textBox1; PropertyDescriptorCollection props = TypeDescriptor.GetProperties(propertyGrid1.SelectedObject); foreach (PropertyDescriptor prop in props) { if (!prop.IsBrowsable) continue; if (prop.ShouldSerializeValue(propertyGrid1.SelectedObject)) { object value = prop.GetValue(propertyGrid1.SelectedObject); Console.WriteLine("{0} = {1}", prop.Name, value.ToString()); } }
However, you get an "object". You can't do much with it until you start making assumptions about the object type. We could possibly get somewhere when you explain what you're trying to accomplish. |
| nobugz Friday, September 28, 2007 1:41 PM |
This might be quite tricky to get right, PropertyGrid allows lots of doodads with custom TypeConverters and TypeDescriptors. This seemed to work pretty well though:
private void Form1_Load(object sender, EventArgs e) { propertyGrid1.SelectedObject = textBox1; PropertyDescriptorCollection props = TypeDescriptor.GetProperties(propertyGrid1.SelectedObject); foreach (PropertyDescriptor prop in props) { if (!prop.IsBrowsable) continue; if (prop.ShouldSerializeValue(propertyGrid1.SelectedObject)) Console.WriteLine(prop.Name); } }
|
| nobugz Wednesday, September 26, 2007 4:18 PM |
I doesn't matter which properties I've changed, your code only returns the Strings "Expanded" and "Selected". I've taken a screenshot of my PropertyGrid: ScreenshotIn this example the code should return "LeftImageAlignment", "Visible" and "Enabled", because these properties were changed by me (their font is bold - because they are not set to the default-value). Thanks, Dennis. |
| a__DENNIS__ Thursday, September 27, 2007 6:43 AM |
Well, that's mighty odd. Bummer that the screenshot doesn't show Expanded and Selected properties. Does the sample code work for you on a text box? Anything special about the class? Any TypeConverters? |
| nobugz Thursday, September 27, 2007 9:35 AM |
No, it doesn't work on a TextBox, too. I've added the standard controls from the Toolbox to my form.
|
| a__DENNIS__ Friday, September 28, 2007 7:52 AM |
I just checked it again. The sample code I posted works like a charm, it lists any property that is shown in bold in the PG. And accurately follows changes I make in the Properties Windows. I couldn't begin to guess why it wouldn't work for you. |
| nobugz Friday, September 28, 2007 8:10 AM |
My project is written in VB.Net. Few minutes ago I have testet your code in a project which is written in C# and it works perfectly!
So I opened my vb-project again and deleted the old code. After that I have added your code again after written it into vb. Now it works perfect in vb, too! So it seems like I have written it not correctly when i added your code first time to my project.
Thank you for your help!
|
| a__DENNIS__ Friday, September 28, 2007 8:57 AM |
I have just another question. With the help of your code I can get all bold entries of the propertygrid. Now I want to know the value of each bold entry! I already tried the method GetValue(), but I don't know how to use this method correctly.
Thanks, Dennis. |
| a__DENNIS__ Friday, September 28, 2007 12:44 PM |
GetValue() works:
propertyGrid1.SelectedObject = textBox1; PropertyDescriptorCollection props = TypeDescriptor.GetProperties(propertyGrid1.SelectedObject); foreach (PropertyDescriptor prop in props) { if (!prop.IsBrowsable) continue; if (prop.ShouldSerializeValue(propertyGrid1.SelectedObject)) { object value = prop.GetValue(propertyGrid1.SelectedObject); Console.WriteLine("{0} = {1}", prop.Name, value.ToString()); } }
However, you get an "object". You can't do much with it until you start making assumptions about the object type. We could possibly get somewhere when you explain what you're trying to accomplish. |
| nobugz Friday, September 28, 2007 1:41 PM |
It works fine, thank you 
I try to write the changed properties into a database and when I start up the form later again, the changed properties should be loaded.
|
| a__DENNIS__ Friday, September 28, 2007 1:59 PM |
Now I have two Strings in my database (the attribute name and its value). Now I wanted to use prop.SetValue() to set the value of an attibute. Can you explain me how to use this method? I have to set the parameters component (as Object) and value (as Object), but I have only two Strings.
Thanks, Dennis.
|
| a__DENNIS__ Monday, October 08, 2007 7:07 AM |