0

Please consider my codes below:

I'm getting an error Constructor on type 'System.String' not found. when I add new string to the collection using the PropertyGrid control.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        propertyGrid1.SelectedObject = Class1.Instance.StringCollection;
    }
}

-----------------------------------------------------------------------------

public sealed class Class1
{
    private static Class1 _instance = new Class1();
    private List<string> _stringListCollection = new List<string>();

    public Class1()
    {
    }   

    public static Class1 Instance 
    {
        get { return _instance; }
    }

    public List<string> StringCollection
    {
        get { return _stringListCollection; }
        set { _stringListCollection  = value; }
    }
}
yonan2236
  • 13,371
  • 33
  • 95
  • 141
  • Your code is not correct. You probably assigned `Class1.Instance` to the property grid. By assigning `Class1.Instance` and then pressing the `'''` in front of `StringCollection`, and the pressing the Add button, you'l get the error you mentioned. – Mohammad Dehghan Feb 02 '12 at 09:02
  • The code provided did not compile for two reasons: Instance does not have a type specified because no class named Instance is declared. I assume it is supposed to be typed Class1. Secondly The event handler named Form1 is illegal because a method can not have the same name as it's enclosing type. I changed this to Form1_Load and assigned it to the Load event, based on the code in the event handler. – Crippledsmurf Feb 02 '12 at 09:07
  • Sorry guys for the confussion. This is not my actual code, it is more likely just a pseudocode.. : ) – yonan2236 Feb 02 '12 at 09:14

2 Answers2

1

When you assign List of something to PropertyGrid, it tries to show single row with modify ... button, where default modify dialog require Item class to have default constructor, which is not right in case of string

You can create class with default constructor and string property in it, and assign a collection of that class instead of string

Or you can use EditorAttribute to override default editor

Hope this helps

Arsen Mkrtchyan
  • 49,896
  • 32
  • 148
  • 184
  • I just realized... I want this class to be static... so making an instance is not possible right?.. so no counstructor will be created as well... How should I accomplish it? – yonan2236 Feb 02 '12 at 08:59
  • you can override editor with your custom @yonan2236, see edited post – Arsen Mkrtchyan Feb 02 '12 at 09:01
  • 1
    http://stackoverflow.com/questions/6307006/how-can-i-use-a-winforms-propertygrid-to-edit-a-list-of-strings-c, here I found more complete answer to this question – Arsen Mkrtchyan Feb 02 '12 at 09:04
0

Here is a little class that implements CollectionEditor and fixes the problem for a list of strings:

public class CollectionEditorBase : CollectionEditor
{
    public CollectionEditorBase(Type type) : base(type) { }

    protected override object CreateInstance(Type itemType)
    {
        //Fixes the "Constructor on type 'System.String' not found." when it is an empty list of strings
        if (itemType == typeof(string)) return string.Empty;
        else return Activator.CreateInstance(itemType);
    }
}

Now just change the editor to be used with you list of strings:

public class MySettings
{
    [Editor(typeof(CollectionEditorBase), typeof(System.Drawing.Design.UITypeEditor))]
    public List<string> ListOfStrings { get; set; } = new List<string>();
}

And then you us an instance of MySettings in the property grid:

propertyGrid1.SelectedObject = new MySettings();

At the top of your class, you would have to use System.ComponentModel and System.ComponentModel.Design or fully qualifies these names in your code.

John Kurtz
  • 775
  • 8
  • 16