For detailed usage of the PropertyGrid control, please refer to the literature:
1. C # PropertyGrid Control application Experience
2. C # Custom PropertyGrid properties
First define a control to be displayed in the drop-down box:
usingSystem; usingSystem.Windows.Forms; namespaceSimon.WinForms.Examples.PropertyGrid { Public classEditorcontrol:usercontrol { PublicEditorcontrol () { This. Label1 =NewSystem.Windows.Forms.Label (); This. Label1. AutoSize =true; This. Label1. Location =NewSystem.Drawing.Point ( +, -); This. Label1. Name ="Label1"; This. Label1. Size =NewSystem.Drawing.Size ( A, A); This. Label1. TabIndex =0; This. Label1. Text ="Name:"; This. ComboBox1 =NewSystem.Windows.Forms.ComboBox (); This. combobox1.formattingenabled =true; This. ComboBox1.Items.AddRange (New Object[] { "Name one", "name Two", "name three", "name Four"}); This. combobox1.location =NewSystem.Drawing.Point ( About, +); This. Combobox1.name ="ComboBox1"; This. combobox1.size =NewSystem.Drawing.Size (133, -); This. Combobox1.tabindex =2; This. combobox1.selectedindexchanged + =NewSystem.EventHandler ( This. combobox1_selectedindexchanged); This. SuspendLayout (); This. Autoscaledimensions =NewSystem.Drawing.SizeF (6F, 12F); This. AutoScaleMode =System.Windows.Forms.AutoScaleMode.Font; This. Controls.Add ( This. ComboBox1); This. Controls.Add ( This. Label1); This. Name ="Editorcontrol"; This. Size =NewSystem.Drawing.Size ( About, -); This. ResumeLayout (false); This. PerformLayout (); } PrivateLabel Label1; PrivateComboBox ComboBox1; Public stringresult =""; Private voidComboBox1_SelectedIndexChanged (Objectsender, EventArgs e) {Result=comboBox1.SelectedItem.ToString (); } } }
Inherit a custom attribute edit manager class from System.Drawing.Design.UITypeEditor, as follows:
usingSystem.Drawing.Design; usingSystem.Windows.Forms.Design; namespaceSimon.WinForms.Examples.PropertyGrid { Public classEditor:uitypeeditor { Public OverrideUitypeeditoreditstyle GetEditStyle (System.ComponentModel.ITypeDescriptorContext context) {//when editing property values, display on the right ... More Buttons returnUitypeeditoreditstyle.modal; } Public Override ObjectEditValue (System.ComponentModel.ITypeDescriptorContext context, System.IServiceProvider provider,Objectvalue) { varEdsvc = provider. GetService (typeof(IWindowsFormsEditorService)) asIWindowsFormsEditorService; if(Edsvc! =NULL) { varPopedcontrol =NewEditorcontrol (); //and ShowDialog this way, you can pop a form to edit it.Edsvc.dropdowncontrol (Popedcontrol); Value=Popedcontrol.result; } return Base. EditValue (context, provider, value); } } }
Defines a type that uses the PropertyGrid display property.
usingSystem.ComponentModel; namespaceSimon.WinForms.Examples.PropertyGrid { Public classShowedclass {[DisplayName ("name")] [Editor (typeof(Editor),typeof(System.Drawing.Design.UITypeEditor))] Public stringName {Get;Set; } [Editor (typeof(Editor),typeof(System.Drawing.Design.UITypeEditor))] Public stringDescription {Get;Set; } } }
Put PropertyGrid on the form, then instantiate your class and let PropertyGrid display the settings to see the effect.
New Showedclass () {name=" I have no name "};
Original connection: C # How to define let PropertyGrid control display [...] button, and then click the drop-down box to display the custom control edit property values
C # How to define let PropertyGrid control display [...] button, and then click the drop-down box to display the custom control edit property values