?
Solved

PropertyGrid using TypeConverter

Posted on 2006-05-22
4
Medium Priority
?
1,817 Views
Last Modified: 2008-02-01
I have a propertyGrid, and there are few integer data in it.  But every data have different range, and I need to provide "dropdown menu" for each integer property...
Could anyone help me... Thanks!

for example:
The code below... the drop down menu has been show, but it contain only '0' for both Table_Amount (suppose {5, 6, 7, 8,9, 10}) and Chair_Amount (suppose {0, 1, 2, ... , 19, 20})

[CategoryAttribute("MeetingRoom"), TypeConverter(typeof(AmountConverter))]
public int Table_Amount
{
    get { return m_table.amount; }
    set { m_table.amount = value; }
}

[CategoryAttribute("MeetingRoom"), TypeConverter(typeof(AmountConverter))]
public int Chair_Amount
{
    get { return m_chair.Count; }
    set { m_chair= value.Count; }
}

m_table = new myObj(5, 10, 7);
m_chair = new myObj(0, 20, 12);

class myObj
{
   public int min;
   public int max;
   public AmountConverter ac;
   public  int amount;
   
   public myObj(int minAmount, int maxAmount, int defaultAmount)
   {
        min = minAmount;
        max = maxAmount;
        amount = defaultAmount;
        ac = new AmountConverter(min, max);
   }
}

internal class AmountConverter : Int32Converter
{
    private int m_min;
    private int m_max;

    // if without this constructor
    // application will crash during forcus on a property item which related with AmountConverter class...
    public AmountConverter() : base ()
    {
           m_min = 0;
           m_max = 0;
    }
    public AmountConverter(int min, int max) : base ()
    {
           m_min = min;
           m_max = max;
    }
    public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
    {
           return true;
    }

    public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
    {
      ArrayList data = new ArrayList();
      for (int i = m_min; i <= m_max; i++)
            data.Add(i);                  
      return new StandardValuesCollection(data);
    }
}
0
Comment
Question by:billcch
  • 2
  • 2
4 Comments
 
LVL 7

Expert Comment

by:levyuk
ID: 16740968
Let me get this right, you want to have a drop down menu that has some integer values?
0
 

Author Comment

by:billcch
ID: 16742209
That's right... and the integer values are between the given min and max.  
0
 
LVL 7

Accepted Solution

by:
levyuk earned 2000 total points
ID: 16743084
Try something like this

[TypeConverter(typeof(Amount )),
CategoryAttribute("Amount "),
DescriptionAttribute("Sets or returns the amount")]
public int Amount
{
get {//do something;}
set {//do something;}
}

    public class Amount : TypeConverter
    {
        private ArrayList values;

        public Amount()
        {
            // Initializes the standard values list with defaults.
            values = new ArrayList(new int[] {1,2,3,4,5,6,7,8,9,10 });//Put the available values in here
        }

        // Indicates this converter provides a list of standard values.
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        // Returns a StandardValuesCollection of standard value objects.
        public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            // Passes the local string array.
            StandardValuesCollection svc = new StandardValuesCollection(values);
            return svc;
        }

        public override bool CanConvertFrom(ITypeDescriptorContext context, Type type)
        {
            if (type == typeof(int))
            {
                return true;
            }
            else
            {
                return base.CanConvertFrom(context, type);
            }
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value.GetType() == typeof(int))
            {
                // Returns the string value to assign to the property.
                return (int)value;
            }
            else
            {
                return base.ConvertFrom(context, culture, value);
            }
        }
    }

That should help
0
 

Author Comment

by:billcch
ID: 16870474
Thank you!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question