Search code examples
c#formsvisual-studiopropertyeditorform-designer

How to list all instances of a component of typeX on a form in a drop down of a control property in design time


Following situation:

I have a component class named ButtonGroupStyleController and a control class named EnhancedButton with this property:

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Browsable(true)]
public ButtonGroupStyleController StyleController { get; set;}

During desing-time in the forms designer, I now want to populate a drop-down in the property grid for this Property with all ButtonGroupStyleController instances that are currently placed on the form similar to the standard form properties AcceptButton and CancelButton, which list all Button instances on the form.

I hope I described my problem clearly and understandable.

The STyleControllerCode is still nearly empty currently because I wanted to implement the function in the question first

namespace DarkTower.Core.Rendering.Forms
{
    [Serializable]
    public class ButtonGroupStyleController : Component, INotifyPropertyChanged
    {
        public ButtonGroupStyleController()
        {

        }

        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

enter image description here

enter image description here


Solution

  • Based on my test, you can define the following class to show the list of instances in design time.

    public class EnhancedButton:Button
        {
    
            [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
            [Browsable(true)]
            public ButtonGroupStyleController StyleController { get; set; }
    
        }
    
     [Serializable]
        public class ButtonGroupStyleController : Component, INotifyPropertyChanged
        {
    
            public ButtonGroupStyleController()
            {
                this.Text = "Hi Winform";
            }
            private string text;
    
            public string Text
            {
                get { return text; }
                set
                {
                    text = value;
                    OnPropertyChanged("Text");
                }
            }
            public event PropertyChangedEventHandler PropertyChanged;
    
            //[NotifyPropertyChangedInvocator]
            protected virtual void OnPropertyChanged([CallerMemberName] string propertyName=null)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
                
            }
    
        }
    

    After you added two class, please rebuild the project and add EnhanceButton and ButtonGroupStyleController to your form from the Toolbox.

    Finally, please find the property called stylecontroller and you will see the result.