Click here to Skip to main content
15,888,018 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
trying to add properties to a property grid with one property having a dropdown(custom type)

Im using VS2010 VB.net with reflection

For my full solution - Download it here

https://www.nyvault.com/files/reflection/xml_propgrid_reflect_sk.zip

Password is 1

The point of this project is to populate a propertyGrid I dont want to create a custom grid, i just want to populate the already made out of the box generic microsoft thing.

basically, I get the class to be created with reflection. using data from XML Some of the fields use a custom type (this will be used for the dropdown)

This is what happens when I run the solution: So i make the class, and it looks fine It creates everything and sets it up. then when I make an instance of this class in my MAIN() in calls a default constructor [new()] for the type(which is hardcoded dropdown items) instead of the custom constructor I wanted [new(byval test as integer)]

basically here are the class constructors for the custom type (located in customlist.vb)

Public Sub New()     
        ' Gather all the localized strings currently loaded
        ' Gather all the strintTables from the current project.    
        For i As Integer = 0 To 4
            myStringCollection.Add(New MyString(100 + i, "Test " & i))
        Next
    End Sub


   Public Sub New(ByVal val As Integer)      
        For i As Integer = 0 To val
            myStringCollection.Add(New MyString(100 + i, "Testy " & i))
        Next
    End Sub


it calls the Public Sub New() but i want to call the Public Sub New(ByVal val As Integer)

please help, this has been making me rip my hair out for two months.
Posted

1 solution

Your MyStringTypeConverter is a TypeConverter attribute to a class. You can't add custom constructors to a type converter and expect the framework to call them knowing what to pass in for the "val" parameter. Luckily the .NET framework has a way around this...

First, you could create a new Attribute, call it something like MyStringValueAttribute, and add a single property to this attribute called Value. Add it to your class that you use the MyStringTypeConverter on.

When the TypeConverter calls CanConvertFrom or CanConvertTo, it passes some context information. You can use reflection to see if your attribute is attached to the class and if it is, you can load the value in and create your string table that way.

The only other thing I can think of is to add the Val property to the MyString. It really isn't clear what the "val" is for in the type converter or what you are trying to get at there, so I can't tell you the best place to put it.

There really isn't any other way to do this in the constructor, the framework won't call it.

Maybe if you gave some more information about what you are trying to do rather than how you are trying to do it, we can help you get some better answers. All I can tell is you are creating a class from an XML file but I really don't know where the MyString plays into it, and what the custom constructor is really supposed to do...
 
Share this answer
 
Comments
Ess Kay 13-May-13 10:11am    
Do you mind giving me a working example? prefereably relating to my project?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900