1 Attachment(s)
[RESOLVED] Usercontrol inside Usercontrol
Hello guys!
Please let me introduce the issue i found, i just cant explain, nor find a workaround. The situation is simple; i would like to place usercontrols inside usercontrols, that are control containers in this case. It works great as long as i drop the usercontrols inside the container-usercontrol in the form edit view. But if i place a usercontrol into a usercontrol (container) at the usercontrol sceen (so i make it a default usercontrol to be attached) things goes wrong. There will be no parent object available and such.
I have been attached a simple example that will show you this issue.
The yellow UserControl (named ucUberContainer) is the object i'm fighting with.
Do you have any idea how to fix it? I'm outa ideas.
I appreciate any help!
Re: Usercontrol inside Usercontrol
Confused. How to replicate the problem?
1. I open Form1 in design view, I can add all 4 ucs into the yellow uc.
2. I open ucUberContainer in design view, I can add the other 3 ucs into the control.
Maybe describe what colored controls get dropped where and in what order? Also specifically whether the form is in design view or the control is in design view.
Edited: Nevermind, I see what you are talking about. Had to open the ucObject to figure it out. Playing...
Re: Usercontrol inside Usercontrol
Ok, not sure I am 100% correct in trying to explain this...
By referencing properties of UserControl.Parent, you are not guaranteed that the Parent object exposes those properties. Containers do have some properties that all should support, but none are guaranteed as far as I know. Whether you get different results if all the ocx's are compiled might be worth testing; maybe this is an IDE-only thing?
Anyway, by referencing the ParentControls collection vs the Parent.Controls (you expect to be there), you get past the problem. Though you still can't reference expected properties of the .Parent object.
Code:
Dim x As Long
For x = 0 To UserControl.ParentControls.Count - 1
Debug.Print UserControl.ParentControls.Item(x).Name
Next
Edited: I'm not even sure (while in IDE) what the Parent object really is. Is it the ubercontainer or is it that control's Extender object?
Edited yet again. For the other 3 examples, the parent object is the form. But for your problem, the ucUberContainer is the parent; and its parent is the form; however, the ucUberContainer is a child of itself (more or less) which looks confusing. Again, maybe just an IDE thing?
If you add this line before the loop starts, you'll see what I mean:
Debug.Print "Parent object: "; TypeName(UserControl.Parent)
The results end up as the following. Go figure.
--------------------------------------------------
Parent object: ucUberContainer
ucUberContainer1
ucObject1
--------------------------------------------------
Re: Usercontrol inside Usercontrol
Uhh man, thanks for the extensive testing! Now you are just expecting some answer from me, that i dont even have a single clue how to explain. Hehe!
However i see what you mean. Well more or less i am..
The ParentControls is a great alternative way to getting past the issue i'm fighting with! The problem is (as you pointed out) the Parent object is quite confused at this point. For example i cant access the background color as well..
UserControl.Parent.BackColor
This will work for the first 3 object, but not for the ucUberContainer.
Quote:
Again, maybe just an IDE thing?
Well i'm not sure how you meand that, but it doesnt matter if im going to compile the project, the same issue will be exists.
Maybe i should "redirect" the parent object from the ucUberControl by some dirty way like this:
Code:
Public Property Get Parent() As Object
Set Parent = UserControl.Parent
End Property
However it might only work on some special cases, but it's not a workaround :(
It also gives me the Form's object (again), but i would expect the ucUberContainer's properties, that is seems like unavailable at this point.
Edit:
It seems like that even better to redirect all the properties one by one that i'm working with.
Code:
'in ucUberContainer
Public Property Get BackColor() As Long
BackColor = UserControl.BackColor
End Property
Now it will be available for the controls inside.
Code:
'it works for 'redirecting' the controls..
Public Property Get Controls() As Object
Set Controls = UserControl.Controls
End Property
Re: Usercontrol inside Usercontrol
That very well may work for controls that you can code. Don't expect it to work for example if your controls are placed in yet another 3rd party container, though it may work.
Regarding IDE-only. The only way I think that you will know for sure whether this behavior will be present when compiled is to compile the ocx and try it. Edited: It may not even compile!