1111
1212namespace VE3NEA
1313{
14- internal class PropertyGridEx : PropertyGrid
14+ internal class PropertyGridEx : PropertyGrid
15+ {
16+ protected override void OnHandleCreated ( EventArgs e )
1517 {
16- protected override void OnHandleCreated ( EventArgs e )
17- {
18- base . OnHandleCreated ( e ) ;
19- GetPropertyGridView ( ) . MouseClick += grid_MouseClick ;
20- }
21- private void grid_MouseClick ( object sender , MouseEventArgs e )
22- {
23- var entry = GetEntryAtPoint ( e . X , e . Y ) ;
24- if ( entry == null || ! ( entry . Value is VisibleSettings ) ) return ;
18+ base . OnHandleCreated ( e ) ;
19+ GetPropertyGridView ( ) . MouseClick += grid_MouseClick ;
20+ }
21+ private void grid_MouseClick ( object sender , MouseEventArgs e )
22+ {
23+ var entry = GetEntryAtPoint ( e . X , e . Y ) ;
24+ if ( entry == null || ! ( entry . Value is VisibleSettings ) ) return ;
2525
26- var settings = ( VisibleSettings ) entry . Value ;
27- bool oldValue = settings . Visible ;
28- settings . Visible = ! settings . Visible ;
26+ var settings = ( VisibleSettings ) entry . Value ;
27+ bool oldValue = settings . Visible ;
28+ settings . Visible = ! settings . Visible ;
2929
30- Refresh ( ) ;
30+ Refresh ( ) ;
3131
32- var args = new PropertyValueChangedEventArgs ( entry , entry ) ;
33- OnPropertyValueChanged ( args ) ;
34- }
32+ var args = new PropertyValueChangedEventArgs ( entry , entry ) ;
33+ OnPropertyValueChanged ( args ) ;
34+ }
3535
3636
37- // access item's property by name
38- internal static string GetItemProperty ( GridItem item , string propertyName )
39- {
40- var flags = BindingFlags . Instance | BindingFlags . Public | BindingFlags . GetProperty ;
41- return ( string ) item . GetType ( ) . GetProperty ( propertyName , flags ) . GetValue ( item ) ;
42- }
37+ // access item's property by name
38+ internal static string GetItemProperty ( GridItem item , string propertyName )
39+ {
40+ var flags = BindingFlags . Instance | BindingFlags . Public | BindingFlags . GetProperty ;
41+ return ( string ) item . GetType ( ) . GetProperty ( propertyName , flags ) . GetValue ( item ) ;
42+ }
4343
4444 // https://stackoverflow.com/questions/4086105/
45- public void ExpandTopLevelProperties ( bool twoLevels = false )
45+ public void ExpandTopLevelProperties ( GridItem ? root = null , bool twoLevels = false )
4646 {
47- //Invoke((System.Windows.Forms.MethodInvoker)delegate
48- {
49- GridItem root = GetRoot ( ) ;
50-
51- if ( root != null )
52- foreach ( GridItem item in root . GridItems )
53- if ( item . GridItemType == GridItemType . Property )
54- {
55- item . Expanded = true ;
56- if ( twoLevels && item . Expandable )
57- foreach ( GridItem subitem in item . GridItems )
58- subitem . Expanded = true ;
59- }
60- }
61- //);
47+ root ??= GetRoot ( ) ;
48+ root . Expanded = true ;
49+
50+ if ( root != null )
51+ foreach ( GridItem item in root . GridItems )
52+ if ( item . GridItemType == GridItemType . Property )
53+ {
54+ item . Expanded = true ;
55+ if ( twoLevels && item . Expandable )
56+ foreach ( GridItem subitem in item . GridItems )
57+ subitem . Expanded = true ;
58+ }
6259 }
6360
64- private Control GetPropertyGridView ( )
65- {
66- foreach ( Control control in Controls )
67- if ( control . GetType ( ) . Name == "PropertyGridView" )
68- return control ;
61+ private Control GetPropertyGridView ( )
62+ {
63+ foreach ( Control control in Controls )
64+ if ( control . GetType ( ) . Name == "PropertyGridView" )
65+ return control ;
6966
70- return null ;
71- }
67+ return null ;
68+ }
7269
73- private GridItem GetRoot ( )
74- {
75- GridItem root = SelectedGridItem ;
76- while ( root ? . Parent != null ) root = root . Parent ;
77- return root ;
78- }
70+ private GridItem GetRoot ( )
71+ {
72+ GridItem root = SelectedGridItem ;
73+ while ( root ? . Parent != null ) root = root . Parent ;
74+ return root ;
75+ }
7976
80- private GridItem GetChildByName ( GridItem parent , string fullName )
77+ private GridItem GetChildByName ( GridItem parent , string fullName )
78+ {
79+ if ( parent == null ) return null ;
80+
81+ foreach ( GridItem item in parent . GridItems )
82+ if ( item . GridItemType != GridItemType . Property )
83+ continue ;
84+ else if ( GetItemProperty ( item , "HelpKeyword" ) == fullName )
85+ return item ;
86+ else
8187 {
82- if ( parent == null ) return null ;
83-
84- foreach ( GridItem item in parent . GridItems )
85- if ( item . GridItemType != GridItemType . Property ) continue ;
86- else if ( GetItemProperty ( item , "HelpKeyword" ) == fullName )
87- return item ;
88- else
89- {
90- var result = GetChildByName ( item , fullName ) ;
91- if ( result != null ) return result ;
92- }
93-
94- return null ;
88+ var result = GetChildByName ( item , fullName ) ;
89+ if ( result != null ) return result ;
9590 }
9691
97- internal GridItem GetItemByFullName ( string fullName )
98- {
99- return GetChildByName ( GetRoot ( ) , fullName ) ;
100- }
92+ return null ;
93+ }
10194
102- private GridItem GetEntryAtPoint ( int x , int y )
103- {
104- var grid = GetPropertyGridView ( ) ;
105- var flags = BindingFlags . Instance | BindingFlags . NonPublic ;
106- var FindPosition = grid . GetType ( ) . GetMethod ( "FindPosition" , flags ) ;
107- if ( FindPosition == null ) return null ;
95+ internal GridItem GetItemByFullName ( string fullName )
96+ {
97+ return GetChildByName ( GetRoot ( ) , fullName ) ;
98+ }
10899
109- var point = ( Point ) FindPosition . Invoke ( grid , new object [ ] { x , y } ) ;
100+ private GridItem GetEntryAtPoint ( int x , int y )
101+ {
102+ var grid = GetPropertyGridView ( ) ;
103+ var flags = BindingFlags . Instance | BindingFlags . NonPublic ;
104+ var FindPosition = grid . GetType ( ) . GetMethod ( "FindPosition" , flags ) ;
105+ if ( FindPosition == null ) return null ;
110106
111- Point invalidPoint = new Point ( int . MinValue , int . MinValue ) ;
112- if ( point == invalidPoint || point . X != 2 ) return null ;
107+ var point = ( Point ) FindPosition . Invoke ( grid , new object [ ] { x , y } ) ;
113108
114- var GetGridEntryFromRow = grid . GetType ( ) . GetMethod ( "GetGridEntryFromRow" , flags ) ;
115- return ( GridItem ) GetGridEntryFromRow . Invoke ( grid , new object [ ] { point . Y } ) ;
116- }
109+ Point invalidPoint = new Point ( int . MinValue , int . MinValue ) ;
110+ if ( point == invalidPoint || point . X != 2 ) return null ;
111+
112+ var GetGridEntryFromRow = grid . GetType ( ) . GetMethod ( "GetGridEntryFromRow" , flags ) ;
113+ return ( GridItem ) GetGridEntryFromRow . Invoke ( grid , new object [ ] { point . Y } ) ;
114+ }
117115
118116 internal void ExpandAndSelect ( GridItem gridItem )
119117 {
@@ -130,39 +128,39 @@ internal void ExpandAndSelect(GridItem gridItem)
130128
131129
132130
133- //--------------------------------------------------------------------------------------------------------------
134- // VisibleSettings
135- //--------------------------------------------------------------------------------------------------------------
136- // expandable settings with a Visible property controlled by a checkbox
137- internal class VisibleSettings
138- {
139- [ Browsable ( false ) ]
140- public bool Visible { get ; set ; }
131+ //--------------------------------------------------------------------------------------------------------------
132+ // VisibleSettings
133+ //--------------------------------------------------------------------------------------------------------------
134+ // expandable settings with a Visible property controlled by a checkbox
135+ internal class VisibleSettings
136+ {
137+ [ Browsable ( false ) ]
138+ public bool Visible { get ; set ; }
141139
142- internal VisibleSettings ( bool value ) { Visible = value ; }
140+ internal VisibleSettings ( bool value ) { Visible = value ; }
143141
144- public override string ToString ( ) { return "Visible" ; }
145- }
142+ public override string ToString ( ) { return "Visible" ; }
143+ }
146144
147145
148146
149147
150- //--------------------------------------------------------------------------------------------------------------
151- // checkbox in the property grid
152- //--------------------------------------------------------------------------------------------------------------
153- // https://stackoverflow.com/questions/37659850
154- public class CheckboxEditor : UITypeEditor
155- {
156- public override bool GetPaintValueSupported ( ITypeDescriptorContext context )
157- { return true ; }
148+ //--------------------------------------------------------------------------------------------------------------
149+ // checkbox in the property grid
150+ //--------------------------------------------------------------------------------------------------------------
151+ // https://stackoverflow.com/questions/37659850
152+ public class CheckboxEditor : UITypeEditor
153+ {
154+ public override bool GetPaintValueSupported ( ITypeDescriptorContext context )
155+ { return true ; }
158156
159- public override void PaintValue ( PaintValueEventArgs e )
160- {
161- var rect = e . Bounds ;
162- rect . Inflate ( 1 , 1 ) ;
163- var settings = ( VisibleSettings ) e . Value ;
164- var checkmark = ( settings . Visible ) ? ButtonState . Checked : ButtonState . Normal ;
165- ControlPaint . DrawCheckBox ( e . Graphics , rect , ButtonState . Flat | checkmark ) ;
166- }
157+ public override void PaintValue ( PaintValueEventArgs e )
158+ {
159+ var rect = e . Bounds ;
160+ rect . Inflate ( 1 , 1 ) ;
161+ var settings = ( VisibleSettings ) e . Value ;
162+ var checkmark = ( settings . Visible ) ? ButtonState . Checked : ButtonState . Normal ;
163+ ControlPaint . DrawCheckBox ( e . Graphics , rect , ButtonState . Flat | checkmark ) ;
167164 }
165+ }
168166}
0 commit comments