The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

C#/.NET question

DaenrisDaenris Registered User regular
edited April 2008 in Help / Advice Forum
Okay, so I have a class which has a member property Points, which is a list of points (List<Point>). I'm trying to display this class in a PropertyGrid at runtime. That works and I can open up the Collections editor for Points and it shows me all the points that are currently in the list. However, under the right side of the collections editor, the X and Y value for each Point is just "Object does not match target type" and I can't actually change it. I can add a new point to the List from there, but it's just 0,0 and I can't change it.

If I use an array of Points instead of a List, I can expand the array in the PropertryGrid and edit X/Y values of each point, however if I try to bring up the Collections editor on the array I get the same issue as above, with me being unable to edit the points.

I'm thoroughly baffled. Everything I've found on google with people encountering this error message is vastly different situations that don't help me, or they're using a collection containing multiple types of objects in it, which is not the case for me since I'm using a strongly typed list of Points which isn't even a custom class.

At this point I'm probably just going to ignore it since using the array sort of works, but it's frustrating.

Daenris on

Posts

  • VThornheartVThornheart Registered User regular
    edited April 2008
    Hmm... we may be able to help if you have some example code. Do you have a snippet where the propertygrid is being filled or somesuch thing?

    I've never actually used PropertyGrid before, but I'm very familiar with many of the idiosyncrasies in .NET... if I saw some code, hopefully I could be of assistance.

    VThornheart on
    3DS Friend Code: 1950-8938-9095
  • GanluanGanluan Registered User regular
    edited April 2008
    Do you have a public property set up for the points generic collection?

    Ganluan on
  • DaenrisDaenris Registered User regular
    edited April 2008
    Yes, there is a public property. As I said, the property correctly shows up and is visible in the propertygrid, it's just when I open the collections editor to add/edit points in the list that it has the issue.

    I'm at work now so don't have the code with me, but it's fairly trivial.
    public class A
    {
       private List<Point> _points;
       public List<Point> Points { get { return _points; } set { _points = value; } }
    
       public A() {
          _points = new List<Point>;
       }
    }
    

    Then in my form, I have a PropertyGrid. It handles the filling automatically by just setting the SelectedObject property. There's some code in a mouseup event handler to select my object from the form which works and I get back the correct object. So the code to actually put it in the PropertyGrid is just
    propertyGrid.SelectedObject = currentObject;
    

    I can get some actual code snippets this evening, but this is the core of the problem.

    Daenris on
  • VThornheartVThornheart Registered User regular
    edited April 2008
    Hmm, when I get back from work, if the problem hasn't already been solved, I'll try to look into PropertyGrid more and see if there's any strange limitations in it that'd cause this problem. (I love the .NET framework, but frequently there seems to be unexpected/poorly documented limitations with many of the controls)

    VThornheart on
    3DS Friend Code: 1950-8938-9095
  • AngelHedgieAngelHedgie Registered User regular
    edited April 2008
    Hmmm...this might be the answer. Basically, you need to formally note that your class is capable of being expanded. Otherwise, PropertyGrid doesn't know to expose the internal members to modification.

    AngelHedgie on
    XBL: Nox Aeternum / PSN: NoxAeternum / NN:NoxAeternum / Steam: noxaeternum
  • DaenrisDaenris Registered User regular
    edited April 2008
    Hmm... I'll try that, but I'm not sure it's the same issue, though it may be. As I said, when using a Point array it will expand, but the Collection Editor still shows me the "Object does not match target type" when trying to use it with the array. Lists I don't believe ever have an expand option in a PropertyGrid, so the Collection Editor is the only way to add/edit items without writing my own custom editor.

    Daenris on
  • DaenrisDaenris Registered User regular
    edited April 2008
    Alright. The solution AngelHedgie linked didn't fix the problem.

    I started a new project and limited it to just enough code to test. Below is the entirety of my code (aside from auto-generated Form Designer code).
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public MyClass mc;
    
            public Form1()
            {
                InitializeComponent();
                mc = new MyClass();
                Point p = new Point(5, 10);
                mc.Points.Add(p);
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                propertyGrid1.SelectedObject = mc;
            }
        }
    
        
        public class MyClass
        {
            private List<Point> _points;
    
            public List<Point> Points { get { return _points; } set { _points = value; } }
    
            public MyClass()
            {
                _points = new List<Point>();
            }
        }
    }
    

    Daenris on
  • VThornheartVThornheart Registered User regular
    edited April 2008
    Hmm, take a look at this article as well:

    http://www.codeproject.com/KB/tabs/customizingcollectiondata.aspx

    It sounds like, when you're dealing with collections (even generic, strongly-typed collections like the one you have), you may have to implement the ICustomTypeDescriptor interface and create a class that inherits from PropertyDescriptor so that the PropertyGrid knows how to deal with the collection of points and the point objects themselves.

    Give this a shot, as well as AngelHedgie's suggestion... usually these kinds of problems either stem from not having the right attributes attached to the class, not implementing an expected interface, or both.

    VThornheart on
    3DS Friend Code: 1950-8938-9095
  • DaenrisDaenris Registered User regular
    edited April 2008
    Hmm... I'll give it a read. It's odd though, because I can create lists of other types (int, PropertyGrid, a custom class MyPoint containing an X and Y int property similar to the standard Point class) and they show up and work fine in the Collection Editor, but Point doesn't.

    I'll read that article and see if it helps. Otherwise I guess I'll just implement my own point class since that seems to work even though the built in one doesn't. Odd though.

    Daenris on
  • VThornheartVThornheart Registered User regular
    edited April 2008
    Aye... it could be that they're doing something strange and/or unusual with their Point implementation that's causing the PropertyGrid to not know how to deal with it.

    VThornheart on
    3DS Friend Code: 1950-8938-9095
Sign In or Register to comment.