2

I am using Visual Studio 2015 Community to create an application using Windows Forms in C#. I have created a custom control (implementing a Button) with public properties and settings. How do I get those custom properties/settings to appear in the Visual Studio property manager when I add the custom button to a form?

I have private backers with public accessors as one would normally do;

I used to do this many years ago but I have forgotten how to get it to appear in the Property Manager. By default the VS 2015, and previous versions, Property Manager is the area where one would normally set the properties of the standard controls and is located in the lower right hand side of the IDE.

here is a snippet of one of the properties I am working with:

public class Spot : Button
{
    // backers for the properties
    private bool isselected = false;

    // //////////////////////////////////////////////////////////////
    // user accessable properties relying on the above backers
    //

    /// <summary>
    /// Get/Set if the spot is selected
    /// </summary>
    public bool isSelected
    {
        get
        {
            return isselected;
        }
        set
        {
            isselected = value;
        }
    }
}

Hope someone here can help remind me how. I did do several searches here, and through google, but no place seems to have what I am looking for. With my specific searches that include "Property Manager" I get allot of real-estate results that are no where near what I am looking for.

japreja
  • 23
  • 5

1 Answers1

1

You need to use a DependencyProperty. Taken from here:

// Dependency Property
public static DependencyProperty CurrentTimeProperty = 
     DependencyProperty.Register( "CurrentTime", typeof(DateTime),
     typeof(MyClockControl), new FrameworkPropertyMetadata(DateTime.Now));

// .NET Property wrapper
public DateTime CurrentTime
{
    get { return (DateTime)GetValue(CurrentTimeProperty); }
    set { SetValue(CurrentTimeProperty, value); }
}

This would give you a new Property "CurrentTime" as an example.

Yours would be something like:

public static DependencyProperty SelectedProperty = 
     DependencyProperty.Register( "IsSelected", typeof(Boolean),
     typeof(Spot), new FrameworkPropertyMetadata(false));

public Boolean IsSelected
{
    get { return (Boolean)GetValue(SelectedProperty); }
    set { SetValue(SelectedProperty, value); }
}

--- Edit since it is for WinForms:

Try as seen here:

[EditorBrowsable(EditorBrowsableState.Always)]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Bindable(true)]
public bool isSelected
{
    get
    {
        return isselected;
    }
    set
    {
        isselected = value;
    }
}
Community
  • 1
  • 1
PhilMasteG
  • 3,095
  • 1
  • 20
  • 27
  • Thank you for the response but I should have clarified that I am not using WPF. I am using standard windows forms, if that makes sense. Should I edit my question to reflect that? or leave it more generic and have multiple answers based on this one question for the various project types? – japreja Aug 23 '15 at 20:22
  • Sorry for that, please add the information that you use WinForms. – PhilMasteG Aug 23 '15 at 20:26
  • Beautiful, that worked like a charm using your edit for winforms. Guess you killed two birds with one stone. Thanks. – japreja Aug 23 '15 at 20:41