V
V
Vadim Bulgakov2015-10-06 01:28:25
Programming
Vadim Bulgakov, 2015-10-06 01:28:25

How to easily organize a Combobox in a Propertygrid (c#)?

Of course, I found an article on Habré, but I can’t perceive such a large amount of new information (((
habrahabr.ru/post/78024
While this works and in the Propertygrid I see this property and can change:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication13
{

    [TypeConverter(typeof(ExpandableObjectConverter))]
    class Vec2
    {
        
        [DisplayName("Координата x"), Description("Координата x"), Category("Координаты")]
        public float x { get; set; }


        [DisplayName("Координата y"), Description("Координата y"), Category("Координаты")]
        public float y { get; set; }

        public Vec2(float x, float y)
        {
            this.x = x;
            this.y = y;
        }
       
    }
}

A simple example with a Combobox would be desirable, like this, or at least a link to something simpler than the Khabrov article "PropertyGrid in Visual Studio: displaying fields associated with collections of objects"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Makarov, 2015-10-06
@Silector

For example, you have an object with a property of type string and you want to be able to select a new value for this property from a drop-down list through the PropertyGrid. It looks like this:

using System;
using System.ComponentModel;

public class MyClass
{
    [TypeConverter(typeof(EnumConverter))]
    public string stringProperty {get; set;}

    public MyClass()
    {
        stringProperty = EnumConverter.ALT1;
    }

    class EnumConverter : TypeConverter
    {
        public const string ALT1 = "Альтернатива1";
        public const string ALT2 = "Альтернатива2";
        public const string ALT3 = "Альтернатива3";

        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)  {return true;  }

        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)   {return true;  }

        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            return new StandardValuesCollection(new[] {ALT1, ALT2, ALT3 });
        }
    }
}

then something like:
to see what happened
PS The code did not run. Therefore, there may be small blemishes. Write in the comments if something does not work or is not clear.
PPS So that PropertyGrid the most powerful component. It can be very, very much. But no more =).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question