I'm using Ambient.UserMode in the ReadProperties of a user control to determine whether the project is in run mode or design mode, and it works OK. However I also have sub user controls, i.e. user controls contained within other user controls, and Ambient.UserMode in them is always True, whether in run mode or design mode.
I understand that True is the default value for Ambient.UserMode for containers that don't provide the Ambient object/properties. So I need to do this with my higher level user control, presumably just by passing its own Ambient.UserMode value along. But how do I do this?
Quote:Originally Posted by Thinker It sounds like a Friend property get that returns a boolean should be sufficient.
OK, so I add that to the parent user control. But how does the sub user control access it? It doesn't know what its container is and this isn't added to the properties of the Extender object, so I can't get at it through that either.
Sorry to give a quick answer without trying it myself. I can get it to
work sometimes but other times it errors.
The Usercontrol.Extender.Container property should return a reference to
the parent usercontrol that can be used to reference the friend property.
Unfortunately, sometimes that object can't see all the properties (for
some unknown reason) and it errors. Also when I try to compile the ocx
I get typemismatch errors too. I am pretty sure there is a way to do it
but it might require a lot of errorhandling.
If you are creating the sub-control objects from within the main UserControl, you could create a Friend Property in the sub-control which stores the instance of the control which owns it. Something like:
Dim m_OwnerControl As MyMainControl
Friend Property Get OwnerControl As MyMainControl
OwnerControl = m_OwnerControl
Friend Property Set OwnerControl(value As MyMainControl)
Set m_OwnerControl = value
Then, in the main control, when you create the sub-control,
Set SubControl1.OwnerControl = Me
Then just reference m_OwnerControl in the sub control.
I haven't tested it, but as far as I know, there's no reason it shouldn't work.
Thinker - It sounds like hard work, but thanks for trying anyway.
VolteFace - By creating the subcontrols, presumably you mean loading them. I'd far prefer to just insert them at design time. You did give me the idea of setting the OwnerControl at initialisation of the parent control, but I couldn't get this to work because doing so triggered the subcontrol's initialize event before the OwnerControl was set, yet I needed the OwnerControl to be set by the time of the initialize event.
There is of course a simple solution, which is to have a global boolean variable which is set to True at the start of Sub Main. The user controls can easily access this at both run time and design time to find out if the application is in run mode or not. I would have preferred to use the UserMode property just because I like to keep the number of global variables down, but it clearly doesn't look like it would be worth the trouble.