I'm following this C# article to learn how to create an ActionList and Action Items, however the article is only focused to action items of type DesignerActionPropertyItem
...
I would like to create an item of type DesignerActionMethodItem
to call a method that must open a MultilineStringEditor
to edit the text lines of the control, just the same action item that we can see in a default RichTextBox control:
Someone could explain me how to do this in C# or VB.NET?.
I'm stuck on the values to pass to the UITypeEditor.EditValue()
method, I think that is the method that invokes/displays the editor, but I'm not sure which value I must pass to the first parameter (it accepts an IServiceProvider
or ITypeDescriptorContext
). I seen this related answer but I think there should exist a more direct/easier way than creating a class that implements IServiceProvider
and ITypeDescriptorContext
... since I will to run a specific UITypeEditor
(MultilineStringEditor
).
This is what I got so far; when I click on the "Edit Text Lines..." action item nothing happens, any exception, just nothing of nothing; I'm not sure whether that is a good or bad signal, because if I try to pass other kind of values to the first parameter of UITypeEditor.EditValue()
method then I got exceptions of invalid type casting when I click on my custom action item.
C# code version:
public class MyControlActionList : DesignerActionList {
private DesignerActionUIService designerActionUISvc;
public new MyControl Component {
get { return (MyControl)base.Component; }
}
public MyControlActionList(MyControl component) : base(component) {
// Cache a reference to DesignerActionUIService, so the DesigneractionList can be refreshed.
this.designerActionUISvc = (DesignerActionUIService)GetService(typeof(DesignerActionUIService));
}
public override DesignerActionItemCollection GetSortedActionItems() {
DesignerActionItemCollection items = new DesignerActionItemCollection();
items.Add(new DesignerActionMethodItem(this, "EditTextLines", "Edit Text Lines...", "Behavior", "Opens the Lines collection editor", false));
return items;
}
public void EditTextLines(){
PropertyDescriptor pd = TypeDescriptor.GetProperties(this.Component)("Text");
MultilineStringEditor editor = (MultilineStringEditor)pd.GetEditor(typeof(UITypeEditor));
editor.EditValue((IServiceProvider)this.GetService(typeof(MultilineStringEditor)), this.Component.Text);
}
}
VB.NET code version:
Public Class MyControlActionList : Inherits DesignerActionList
Private designerActionUISvc As DesignerActionUIService
Public Shadows ReadOnly Property Component As MyControl
Get
Return DirectCast(MyBase.Component, MyControl)
End Get
End Property
Public Sub New(ByVal component As MyControl)
MyBase.New(component)
' Cache a reference to DesignerActionUIService, so the DesigneractionList can be refreshed.
Me.designerActionUISvc = DirectCast(GetService(GetType(DesignerActionUIService)), DesignerActionUIService)
End Sub
Public Overrides Function GetSortedActionItems() As DesignerActionItemCollection
Dim items As New DesignerActionItemCollection()
items.Add(New DesignerActionMethodItem(Me, "EditTextLines", "Edit Text Lines...", "Behavior", "Opens the Lines collection editor", False))
Return items
End Function
Public Sub EditTextLines()
Dim pd As PropertyDescriptor = TypeDescriptor.GetProperties(Me.Component)("Text")
Dim editor As MultilineStringEditor = DirectCast(pd.GetEditor(GetType(UITypeEditor)), MultilineStringEditor)
editor.EditValue(CType(Me.GetService(GetType(MultilineStringEditor)), IServiceProvider), Me.Component.Text)
End Sub
End Class