Hi,
I have a screen that will need multiple combo boxes, and they will all need the same special stuff, so I am trying to make my own control that inherits from UltraComboEditor that I can reuse.
In addition to the standard drop-down functionality, I will need an extra drop-down button that drops down an UltraGrid for quick-summary purposes. (Actually, I will need three buttons, but I imagine the process is the same for the second two, so I'm starting with one.)
My strategy is to have member variables for the button (DropDownButtonEditor '_button') and the grid (UltraGrid '_grid'). In the constructor of this TestComboEditor class, I add the button to the class's ButtonsRight collection, set some Appearance properties (add an image), and set the Control property of _button to be the grid. I want to use the BeforeDropDown event to retrieve and set the DataSource for the grid.
The first problem I have is that there are two editor buttons being shown in addition to the standard drop-down button. I thought maybe a button was being added to the collection twice, so in the constructor of TestComboEditor , I surrounded the code that adds the _button to the ButtonsRight collection with 'if (!this.DesignMode). But that led to a 'Key already exists' exception in the constructor when the _button was being added to the ButtonsRight collection--apparently, just placing the TestComboEditor control on the form in the screen designer added a new DropDownEditorButton and added it to the ButtonsRight collection. So I changed the conditional around to only add the button if in DesignMode, and then I get nothing.
Now the problem is that I can't get it to work without throwing the 'Key already exists' exception. I really don't know what is going on. Any advice? I've attached the code I'm using for the TestComboEditor class.
Thanks,
J
Update: You can get the 'Key already exists' error in VS directly--add the control to a screen and save, close the screen, and then open back up in the designer.
Hi J,
jammerms said:Wow, I should have known that. Found it, overrode it, and still get the duplicate key error, both in the designer and when running it.
Did you start over with a clean control? If you used the existing one on a form, then it may already have been serializing the button.
jammerms said:I've tried using a conditional, as in 'if (!_bHaveButtonsBeenInitialized)', to only add the buttons if they're not already there, but that doesn't work either.
I think this won't work because the order is wrong. The OnCreateControl code gets called first, and then the form's deserialization code tries to add the button again. So in OnCreateControl, the button hasn't been created, yet, so you can't check for it, and then it blows up when the form gets deserialized.
I am really surprised this didn't work, though. I thought OnCreateControl only gets called when you place a new control on a form in the designer and not every time you run your application.
Wow, I should have known that. Found it, overrode it, and still get the duplicate key error, both in the designer and when running it.
I've tried using a conditional, as in 'if (!_bHaveButtonsBeenInitialized)', to only add the buttons if they're not already there, but that doesn't work either.
Is there a way to have a derived UltraComboEditor with buttons added to the ButtonsRight collection? There are a couple other properties I expose with the derived class that allow it to handle the save and load logic in a much better object-oriented fashion than using a bunch of standard UltraComboEditors and having the container screen know which one is which and handle all the logic.
OnCreateControl is a method (not an event) on Control. So every control has it and you should be able to override it. It's possible that the UltraComboEditor might be hiding this method from intellisense, but I can't imagine why it would be doing so, so that seems very unlikely.
I don't see an OnCreateControl event for the UltraComboEditor--is there some other place you can think of to do this?
This will be very tricky to do at design-time. If you add a button to the ButtonsRight collection in the constructor of the derived class, then this button will get added to the control when you place your control on a form. It will then get serialized when you save the form. The next time you open the form designer, the control will get deserialized, which will recreate the button, which is already there because you added it in the constructor.
I'm a little bit rusty on this, but I think the thing to do is to add the button in OnCreateControl instead of in the constructor.