[RESOLVED] [2005] Sharing control but preventing dispose?
I have a form with various controls, one of which is a combobox (called "C") which has some significant code behind it.
In a mode of the app, I open a sub-form and need that same combobox on it, rather than copy all of the code into the sub-form, in the load event of the sub-form I did;
VB Code:
Dim CBox As New ComboBox
CBox = Main.C
Which is great, since the sub-form now has the ComboBox and it works fine, I have only one set of code for it too. However... when I close the sub-form with Me.Close and go back to the main form, the ComboBox Main.C is now gone, presumably it was disposed of when the sub-form was closed.
How can I prevent this? I'm assuming there must be a way of sharing controls between forms without having to duplicate them?
Re: [2005] Sharing control but preventing dispose?
I am not sure if this will fix it but it is worth to try:
in the form's closing event.
Re: [2005] Sharing control but preventing dispose?
Great idea, but it didnt work. I also tried;
CBox = Nothing
and also
CBox = New ComboBox
and
Main.C = CBox
CBox.Dispose
Looks like I'll have to create a new instance of the combobox on the sub-form and copy all the code too.
Re: [2005] Sharing control but preventing dispose?
First of all, the code you posted is wrong. The first line declares a variable, creates a new ComboBox object and assigns the object to the variable. The second line then assigns an existing ComboBox object to the same variable, thus simply discarding the object you created on the previous line. If you intend to use an existing object, not a new object, then don't use the "New" keyword. Your code should simply be:
VB Code:
Dim CBox As ComboBox = Main.C
Secondly, Thius can't possibly be all the relevant code because this will not make the ComboBox appear on the second form. I'm guessing that you later assign that same ComboBox to a variable that was created in the designer. All variables created in the designer will have their assigned objects disposed when the form is disposed. Also, if you're doing that then you must be creating a third ComboBox that is also simply discarded.
I very strongly recommend that you don't use the same control on both forms. It is a very bad idea in my opinion. If you genuinely want to persist then you should absolutely not create this ComboBox in the designer of the second form. You should remove it from the Controls collection of the first from and add it to the Controls collection of the second form. Of course, you'll have to change the Location property so that it appears in the right place. Before the second form is destroyed you should remove this ComboBox again and add it back to the first form.
I reiterate that I consider what you're doing to be a very bad idea. I believe very strongly that you should use two distinct objects and simply assign the appropriate property values from one to the other when appropriate.
Re: [2005] Sharing control but preventing dispose?
JM,
The solution here was that the control should be swopped between the form control collections. I did a quick test and that worked fine.
Given that this is not a good idea, it turned out to be a trivial amount of code to create a copy of the existing ComboBox onto the sub-form. The reason being that I can simply copy the size, items list, items list formatting etc. to the new box. I created a change event that sets the index of the original box and then re-aligns the copy. So the copy is merely a slave.