Is there any way to spin through the objects in a property grid<p>Hello all,</p> <p>Is there any kind of an items collection buried inside a PropertyGrid? I know there is a SelectedGridItem property, but I need to iterate through them and I have been unsuccessful in finding out a way to do that. </p> <p>Thanks in advance for any ideas and/or suggestions!</p>© 2009 Microsoft Corporation. All rights reserved.Thu, 11 Dec 2008 01:14:42 Za0526e23-12be-4731-b34d-a81aa7fb79d7http://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/a0526e23-12be-4731-b34d-a81aa7fb79d7#a0526e23-12be-4731-b34d-a81aa7fb79d7http://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/a0526e23-12be-4731-b34d-a81aa7fb79d7#a0526e23-12be-4731-b34d-a81aa7fb79d7j2associateshttp://social.msdn.microsoft.com/Profile/en-US/?user=j2associatesIs there any way to spin through the objects in a property grid<p>Hello all,</p> <p>Is there any kind of an items collection buried inside a PropertyGrid? I know there is a SelectedGridItem property, but I need to iterate through them and I have been unsuccessful in finding out a way to do that. </p> <p>Thanks in advance for any ideas and/or suggestions!</p>Mon, 10 Jul 2006 23:10:46 Z2006-11-21T08:13:33Zhttp://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/a0526e23-12be-4731-b34d-a81aa7fb79d7#80d34fe4-a8ba-4fde-8674-54d5964b0668http://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/a0526e23-12be-4731-b34d-a81aa7fb79d7#80d34fe4-a8ba-4fde-8674-54d5964b0668Peter Ritchiehttp://social.msdn.microsoft.com/Profile/en-US/?user=Peter%20RitchieIs there any way to spin through the objects in a property grid<p>The SelectedObjects property contains the objects whose properties are displayed in the grid.</p> <p>If you're not looking for objects in the property grid; but looking to iterate the properties of the object displayed in the property grid, you'd have to use the Type.GetProperties() method on each item in the PropertyGrid.SelectedProperties array.</p>Tue, 11 Jul 2006 07:39:24 Z2006-11-21T08:13:33Zhttp://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/a0526e23-12be-4731-b34d-a81aa7fb79d7#600ec7a9-5c64-4bba-9d5a-8db6ef1efe00http://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/a0526e23-12be-4731-b34d-a81aa7fb79d7#600ec7a9-5c64-4bba-9d5a-8db6ef1efe00joeycalisayhttp://social.msdn.microsoft.com/Profile/en-US/?user=joeycalisayIs there any way to spin through the objects in a property gridif you want to iterate on the items listed in the property grid though (top dropdown), you might want to iterate on the components in the designer surface through IDesignerHost perhaps...<br>Tue, 11 Jul 2006 16:00:40 Z2006-07-11T16:00:40Zhttp://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/a0526e23-12be-4731-b34d-a81aa7fb79d7#ea8c8855-0b89-4eed-aa90-7e0141fe8467http://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/a0526e23-12be-4731-b34d-a81aa7fb79d7#ea8c8855-0b89-4eed-aa90-7e0141fe8467j2associateshttp://social.msdn.microsoft.com/Profile/en-US/?user=j2associatesIs there any way to spin through the objects in a property grid<P>Hello Peter,</P> <P>Thanks for the response. Let's say that I have a class with 4 properties, Prop1, Prop2, Prop3 and Prop4. When the CollectionEditor pops up, the PropertyGrid on the right side will display the 4 properties for the currently selected item (assume only one for our discussion). </P> <P>The PropertyGrid exposes a SelectedGridItem property. I want to iterate all of the GridItems in the PropertyGrid. They obviously correspond to the class items which are listed in the left pane. But I don't know how to create a GridItem from a class property.</P> <P>What I want to do is get the GridItems from the PropertyGrid as if the user clicked on each of them in succession. </P> <P>Hello joey,</P> <P>To this point, I have not been successful in getting a DesignerHost. The various things I have tried have all returned Nothing.</P>Tue, 11 Jul 2006 18:13:06 Z2006-07-11T18:13:06Zhttp://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/a0526e23-12be-4731-b34d-a81aa7fb79d7#10751107-7b8f-464c-9f2d-fa8aec294c63http://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/a0526e23-12be-4731-b34d-a81aa7fb79d7#10751107-7b8f-464c-9f2d-fa8aec294c63DirkProfihttp://social.msdn.microsoft.com/Profile/en-US/?user=DirkProfiIs there any way to spin through the objects in a property grid<p>Hello </p> <p>here a way to  iterate ...</p> <p>But how can I select a Property by set <font size=2>propertyGrid1.SelectedGridItem ???</p></font> <p> </p> <p>        public string getPropertyInfoString(object obj)<br>        {<br>            StringBuilder result = new StringBuilder();</p> <p>            Type t = Openimmo.GetType();<br>            PropertyDescriptorCollection col;<br>            col = TypeDescriptor.GetProperties(t);<br>            getPropertyInfo(ref result, obj, col, 0);<br>            return result.ToString();<br>        }</p> <p><br>        private void getPropertyInfo(ref StringBuilder result, object obj, PropertyDescriptorCollection col, int level)<br>        {<br>            if (col == null)<br>                return;<br>            <br>            foreach (PropertyDescriptor pd in col)<br>            {<br>                if (pd.IsBrowsable == false || pd.IsReadOnly == true || pd.DesignTimeOnly == true)<br>                    continue;</p> <p>                result.Append(&quot;&quot;.PadLeft(level * 2));<br>                result.Append(pd.Name);<br>                result.Append(&quot; = &quot;);<br>                result.Append(pd.GetValue(obj));<br>                result.AppendLine();</p> <p>                object propObj = pd.GetValue(obj);<br>                if (propObj == null)<br>                    continue;</p> <p>                Type propType = propObj.GetType();<br>                if (propType.IsPublic &amp;&amp; propType.IsClass)<br>                {<br>                    if (propObj is IList)<br>                    {</p> <p>                        IList list = (IList)propObj;<br>                        foreach (object item in list)<br>                        {<br>                            getPropertyInfo(ref result, item, TypeDescriptor.GetProperties(item), level + 1);                        <br>                        }<br>                    }<br>                    else<br>                        getPropertyInfo(ref result, propObj, TypeDescriptor.GetProperties(propObj), level + 1);<br>                }</p> <p>            }<br>        }<br></p>Wed, 15 Nov 2006 11:27:24 Z2006-11-21T08:13:28Zhttp://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/a0526e23-12be-4731-b34d-a81aa7fb79d7#6000616c-7764-4bf8-9ed6-9aef8342a2c1http://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/a0526e23-12be-4731-b34d-a81aa7fb79d7#6000616c-7764-4bf8-9ed6-9aef8342a2c1trobinsonhttp://social.msdn.microsoft.com/Profile/en-US/?user=trobinsonIs there any way to spin through the objects in a property grid<p>  <p><font face="Tahoma,Helvetica,Sans-Serif">Better late than never I guess..  Here is one way to do it.</font> <ul> <li><font face="Tahoma,Helvetica,Sans-Serif">if the <strong>SelectedObect</strong> property is <strong>not null</strong> then <strong>SelectedGridItem</strong> property <strong>will always be valid</strong>, even if the SelectedObject contains no browsable properties.</font> <li><font face="Tahoma,Helvetica,Sans-Serif">The GridItem object contains Parent, and GridItem<strong>s</strong> properties.  This gives you a path to every GridItem on the PropertyGrid.</font></li></ul> <p><font color="#2b91af" size=2></font>  <p><font face="Tahoma,Helvetica,Sans-Serif" color="#000000">In the code snippet below are two simple static functions.  One for getting back to the root parent from any GridItem and one for searching back through the GridItem Heiarchy  looking for a GridItem with a specific label.</font> <p><font face=Tahoma></font>  <p><font face=Tahoma>Here's a sample of using the functions:</font> <p><font face="Courier New, Courier, Monospace"><font color="#2b91af" size=2>GridItem</font><font size=2> gridItemFind;</p></font></font><font color="#2b91af" size=2> <p><font face="Courier New, Courier, Monospace">String</font></font><font face="Courier New, Courier, Monospace"><font size=2> sPropName = </font><font color="#a31515" size=2>&quot;PropNameImLookngFor&quot;</font></font><font size=2><font face="Courier New, Courier, Monospace">;</font></p> <p><font face="Courier New, Courier, Monospace">gridItemFind = MyPropertyGrid.SelectedGridItem;</font></p></font><font color="#0000ff" size=2> <p><font face="Courier New, Courier, Monospace">if</font></font><font face="Courier New, Courier, Monospace"><font size=2> (gridItemFind != </font><font color="#0000ff" size=2>null</font></font><font size=2><font face="Courier New, Courier, Monospace">)</font></p> <p><font face="Courier New, Courier, Monospace">{</font></p> <p><font face="Courier New, Courier, Monospace">    gridItemFind = GetParentGridItem(gridItemFind);</font></p> <p><font face="Courier New, Courier, Monospace">    gridItemFind = FindGridItem(sPropName, gridItemFind);</font></p> <p></font><font face="Courier New, Courier, Monospace"><font color="#0000ff" size=2>    if</font><font size=2> (gridItemFind != </font><font color="#0000ff" size=2>null</font></font><font size=2><font face="Courier New, Courier, Monospace">)</font></p> <p><font face="Courier New, Courier, Monospace">        MyPropertyGrid.SelectedGridItem = gridItemFind;</font></p> <p><font face="Courier New, Courier, Monospace">}</font></p></font> <p>  <p>  <p>I Hope this helped, <p>Travis Robinson <p><a title="http://www.picmicrochip.com/" href="http://www.picmicrochip.com/">www.picmicrochip.com</a> <p>  <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div> <p>private static GridItem GetParentGridItem(GridItem gItem)<br>{<br>    // Takes a GridItem as a parameter and find the root(top most) GridItem<br>    GridItem gridItemFind = gItem;<br>    GridItem gridItemLast = null;</p> <p>    while (gridItemFind != null)<br>    {<br>        gridItemLast = gridItemFind;<br>        gridItemFind = gridItemFind.Parent;<br>    }<br>    return gridItemLast;<br>}</p> <p>private static GridItem FindGridItem(string sLabel, GridItem gridItemToSearch)<br>{<br>    /*<br>    // Searches through gridItemToSearch and all of its child nodes<br>    // Returns the first GridItem that has a Label equal to the sLabel parameter.<br>    // NOTE:<br>    // You could use any matching criteria you wanted, the Label property is what<br>    // worked for me because it always equals the property name. You could also<br>    // use the gridItemToSearch.PropertyDescriptor<br>    */<br>    if (gridItemToSearch.Label == sLabel)<br>        return gridItemToSearch;<br>    if (gridItemToSearch.GridItems.Count &gt; 0)<br>    {<br>        for (int iGrid = 0; iGrid &lt; gridItemToSearch.GridItems.Count; iGrid++)<br>        {<br>            GridItem check = FindGridItem(sLabel, gridItemToSearch.GridItems[iGrid]);<br>            if (check != null)<br>                return check;<br>        }<br>    }</p> <p>    return null;<br>}</p></div></div> <p> </p> <p></p> <p> </p>Sun, 27 May 2007 20:12:38 Z2007-05-27T20:12:38Z