Ok, so I’ve been playing with this and decided to give Bing a go with their chat search. The result was not functional but did correct a few wrong assumptions on my part re syntax. I’ve been able to massage the example, so it runs but the thing that is making me pull my hair out is how to add the Editor attribute to my custom class.
Here is what I have so far, it’s output and error:
[void][System.Reflection.Assembly]::Load('System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
[void][System.Reflection.Assembly]::Load('System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
Add-Type -AssemblyName "System.Windows.Forms"
# Define your custom class
class MyClass {
[string]$MyProperty
# Default Constructor
MyClass(){
$this.MyProperty = "Default"
}
}
# Create a new form that will be used as the custom editor
class MyClassEditorForm : System.Windows.Forms.Form {
MyClassEditorForm() {
$this.Text = "MyClass Editor Form"
$this.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog
$this.MaximizeBox = $false
$this.MinimizeBox = $false
$this.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
# Add controls to your form here
}
}
# Implement the UITypeEditor class to create a modal editor for your custom class
class MyClassEditor : System.Drawing.Design.UITypeEditor {
# Override GetEditStyle to specify that we want to use a modal dialog for editing
[System.Drawing.Design.UITypeEditorEditStyle] GetEditStyle([System.ComponentModel.ITypeDescriptorContext]$context) {
return [System.Drawing.Design.UITypeEditorEditStyle]::Modal
}
# Override EditValue to display the custom editor form and update the value of the property in your custom class with the selected value from the form
[object]EditValue([System.ComponentModel.ITypeDescriptorContext]$context, [System.IServiceProvider]$provider, [object]$value) {
# Get the IWindowsFormsEditorService from the provider
[object]$editorService = $provider.GetService([System.Windows.Forms.Design.IWindowsFormsEditorService])
if ($null -ne $editorService) {
# Create an instance of your custom editor form
$editorForm = [MyClassEditorForm]::New()
# Display the form as a modal dialog and update the value of your property with the selected value from the form
if ($editorService.ShowDialog($editorForm) -eq [System.Windows.Forms.DialogResult]::OK) {
$value.NMyProperty = "Selected Value"
}
}
return $value
}
}
# Register your UITypeEditor with PowerShell
[System.ComponentModel.TypeDescriptor]::AddAttributes(
[MyClass],
[System.ComponentModel.EditorAttribute]([MyClassEditor], [System.Drawing.Design.UITypeEditor]),
[System.ComponentModel.TypeConverterAttribute]("ExpandableObjectConverter")
)
# Create an instance of your custom class and add it to a PropertyGrid control
$MyClass = [MyClass]::New()
$propertyGrid = [System.Windows.Forms.PropertyGrid]::New()
$propertyGrid.SelectedObject = $MyClass
# Display the PropertyGrid control in a new form
$form = [System.Windows.Forms.Form]::New()
$form.Controls.Add($propertyGrid)
$form.ShowDialog() | Out-Null
Not surprisingly there is no “…” showing at the end of the property value field. All I want at the moment is for it to open a custom form for the custom type. Then I’d need to know how the feed values back and forth.
I’ve been digging into PowerShell classes and found that there is a way to add Attributes to a class vs. their properties but I haven’t wrapped my head around it yet and am not sure if it’s going to help me.
Is this a limitation with PowerShell Classes? Will I need to resort to embedding C# code to create the custom classes in order to leverage this approach?
Disclaimer: I don’t really know C# or have a full understanding of classes, inheritance, etc.
Links I’ve used:
c# - How to create custom PropertyGrid editor item which opens a form? - Stack Overflow
powershell custom editor for propertygrid control winforms - Search (bing.com)
Creating Custom Attributes - powershell.one
Powershell: Creating and using custom attributes (powershellexplained.com)