简体   繁体   中英

How to Use Visual Studio Resource Selector Dialog in Custom Designer

Using .NET 4.0 with Visual Studio 2017 and Visual Basic .NET (could be done in C# just as well), I have created a WinForms application. As part of the application I have created a custom control by adding a new class and inheriting from System.Windows.Forms.Control

Public Class MyControl
    Inherits System.Windows.Forms.Control
End Class

If I add the custom control to a form, I can add a BackgroundImage using the Property Window. In the Property Window, if I click in the BackgroundImage property, it shows an ellipsis button. Clicking on that button will open a Select Resource Dialog window.

在此处输入图片说明

I have now created a custom designer for the control by inheriting from System.Windows.Forms.Design.ControlDesigner . I have also created the designer form that pops up when the control is double clicked in design view. On the designer form I want to be able to choose a Background image using the same Select Resource dialog from Visual Studio that is shown above. I have been unable to find where the Select Resource Dialog exists.My suspicion was in the following assembly, but I did not find it.

Microsoft.VisualStudio.Design.dll

Can someone tell me the fully qualified namespace for the Select Resource Dialog used by Visual Studio and which assembly it exists in?

You can show the property editor of all properties at design time using code. To do so, you can find the internal EditorServiceContext which is in System.Design assembly, and then invoke its EditValue method by passing the your control designer, the control to edit and the property name to edit.

Example

You can find the full source code for example here in this repository:

The core part of the sample is the designer class:

using System.ComponentModel.Design;
using System.Linq;
using System.Windows.Forms.Design;
public class MyControlDesigner : ControlDesigner
{
    DesignerVerbCollection verbs;
    public override DesignerVerbCollection Verbs
    {
        get
        {
            if (verbs == null)
            {
                verbs = base.Verbs;
                verbs.Add(new DesignerVerb("Show Select Resource", 
                    (s, e) => ShowSelectResource()));
            }
            return verbs;
        }
    }
    public void ShowSelectResource()
    {
        var editorServiceContext = typeof(ControlDesigner).Assembly.GetTypes()
            .Where(x => x.Name == "EditorServiceContext").FirstOrDefault();
        var editValue = editorServiceContext.GetMethod("EditValue",
            System.Reflection.BindingFlags.Static |
            System.Reflection.BindingFlags.Public);
        editValue.Invoke(null, new object[] { this, this.Component, "SomeProperty" });
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM