Write an editor for your properties

zhaozj2021-02-16  186

There is often such a problem online: How to add a "..." button or drop-down list box when you display it in the properties window, and then edit the value of this property by a custom editor. I will give a common example, assuming that your property represents a path, you want to have a "..." button when displayed in the Properties window, click the Directory Selection dialog, you can pick the folder directly.

First, to write a Editor class, inherit from system.drawing.design.uitypeEditor:

Imports System.Security.Permissionsimports System.ComponentModelimports System.drawing.Designimports System.Windows.Forms

Public class patheditor inherits uitypeEditor

_ Public Overrides Function EditValue (ByVal context As ITypeDescriptorContext, _ ByVal provider As IServiceProvider, ByVal value As Object) As Object Using chooseDialog As New FolderBrowserDialog () With chooseDialog

.ShowNewFolderButton = False .Description = "Choose the folder of" & _ context.PropertyDescriptor.DisplayName .SelectedPath = CStr (value) .RootFolder = Environment.SpecialFolder.MyComputer .ShowDialog () Return .SelectedPath End With End Using End Function

_ Public Overrides Function GetEditStyle (_ ByVal context As ITypeDescriptorContext) As UITypeEditorEditStyle Return UITypeEditorEditStyle.Modal End Function

_ public overrides function getpaintvaluesupported (_ Byval Context as itypescriptorContext) AS Boolean Return False End Function

END CLASS

When implementing this class, several methods of UITYPEEDITOR should be rewritten. The getEditStyle method returns a number indicating that the editor uses an external editor (display "...", and pops up) or use a drop-down list. This example we use an external editor. If you don't rewrite this method, you will not have any special editing methods by default. GetPaintValeSupported method Returns whether to support the attribute editor redraw. We often see the editor of the color property directly display color, the editor of the icon properties can display the thumbnail of the icon, etc., all through this function. Here we don't need it. The last EditValue property is the specific definition of editing methods. We need a directory Select dialog to edit your directory, you can create it in your code. The value parameter represents the value of the attribute before editing this property, and the Contex parameter provides some information about the property itself. After writing, we can bind this editor to our properties. Used is EditoAttribute, as follows:

_public proty perlpath () AS String Get Return_PerlPath End Get Set (Byval Value As String) _PERLPATH = Value End SETEND Property

This EditoAttribute should accept two parameters, one is the type of Editor class, just that the class we have written, one must be the type of UITYPEEDITOR itself and can be obtained with the GetType operator. Your properties are now editing with custom editors. Figure:

After clicking that editing button, pop up the editor:

There are also many editors, such as editing of the collection attribute, editor editing the custom structure, etc., can be freely designed, and then write Editor to design your own properties.

转载请注明原文地址:https://www.9cbs.com/read-8512.html

New Post(0)