Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1871
Editing option set items from user control.
posted

I have an UltraOptionSet user control. I need the ability to edit/add items from the designer. I have supplied a sample of what I am attempting but it appears I am implementing it incorrectly.

From the ToolBox I drag out OptionSetBase. By default it has two value list items. Both are set to blank.

To Edit the Items I click on the Items property which pops up an editor form. I change the items to:

Value  "Y" Display "Yes", Value "N" Display "No", and Value "X" Display "Exempt"

Looks great on the screen but when I load it the two default items get added back to the top. Not sure what I am doing wrong.

Designer after adding option set user control.

After Compiled and loaded.

OptionSet.zip
Parents
No Data
Reply
  • 469350
    Verified Answer
    Offline posted

    Hi,

    Dealing with the Visual Studio form designer and collection like this is extremely tricky. VS does not recognize when items in a collection change in a case like this where you have derived classes. It doesn't track the item that was edited and the new item you added are the same thing.

    So basically, what happens here is that when you open up the collection editor and change the existing items and save, it clears the whole collection and adds the items into a new list. Then, when you run the app, a new OptionSet is created and DotNet deserializes the three items.

    The OptionSet control probably uses a TypeConverter and/or a UITypeEditor on the Items property in order to deal with this. So you might be able to use the same one.

    You might also be able to get around this by playing around with implementing ISupportInitialize or maybe overriding OnCreateControl to remove the default items. But this will take a lot of trial-and-error to find the right way to get it to work, if it's even possible.

    My advice would be to create your own collection of items and then create the optionset items base on your own items. But personally, it's probably not worth the effort it will take to get it working the way you want it - in my experience.

Children