[quote=285626:@Tim Streater]I have a ContainerControl which has a number of controls and some properties. Let’s say that a property is called myProp. Several of the controls are a class which is a subclass of TextArea, call it myTextarea, which has some methods. So there are several instances of myTextarea.
Now, myProp is some read-only data which will be needed by all the instances of myTextarea, specifically by their KeyDown event handlers. And because all the KeyDown event handlers are identical, I’ve put their code in a myTextarea method, to be called by the actual KeyDown event handlers.
The difficulty I have is how to refer to myProp from within this method. Just using its name gives “not found”, prefixing with me or self just gives that myTextarea has no such property.[/quote]
I understand your setup as follows please correct me if I’m wrong:
Class MyContainerControl
Properties:
MyProp // The MyTextArea class needs to get this value
Controls:
textArea1 As MyTextArea
textArea2 As MyTextArea
...
First their is no need for a separate event handler and/or method like this:
...because all the KeyDown event handlers are identical, I've put their code in a myTextarea method, ...
As the KeyDown event of MyTextArea is identical for all instances you will create of MyTextArea, just put the code into the this KeyDown event and raise a newly added KeyDown event definition:
[code]// Add a new KeyDown event definition to MyTextArea
Function KeyDown(key As String) As Boolean
// Implement the KeyDown event of MyTextArea and raise the above event definition
Function KeyDown(key As String) As Boolean
If RaiseEvent KeyDown(key) Then
Return True
End
// Query the container control here
End[/code]
Secondly I think you stumbled across the EmbeddedWindowControl vs ContainerControl issue, as you want to access ContainerControl at runtime, which is not easily possible. A ContainerControl at runtime is a Canvas subclass called EmbeddedWindowControl. You can unfortunately not cast an EmbeddedWindowControl to a ContainerControl as the latter is a subclass of Window. This is a an extension function on EmbeddedWindowControl which returns itself as ContainerControl:
[code]Module EmbeddedWindowControlExtensions
Static dct As New Dictionary()
Dim ewcHandle As Integer = ewc.Handle
Dim retval As ContainerControl = Nil
If dct.HasKey(ewcHandle) Then
Dim wr As WeakRef = dct.Value(ewcHandle)
If wr.Value IsA ContainerControl Then
retval = ContainerControl(wr.Value)
End
End
If retval Is Nil Then
Dim iterator As Runtime.ObjectIterator = Runtime.IterateObjects()
Do Until Not iterator.MoveNext()
If iterator.Current IsA ContainerControl Then
Dim cc As ContainerControl = ContainerControl(iterator.Current)
If cc.Handle = ewc.Handle Then
retval = cc
Exit
End
End
Loop
dct.Value(ewcHandle) = New WeakRef(retval)
End
Return retval
End Module[/code]
With this you can now replace the part // Query the container control here in the KeyDown event of MyTextArea with something like this:
...
Dim parent As EmbeddedWindowControl = EmbeddedWindowControl(Parent)
Dim cc As ContainerControl = parent.ContainerControl
Dim myCC As MyContainerControl = MyContainerControl(cc)
Dim myPropValue As String = myCC.MyProp
...
Return False
There is a ton of request to introduce a ContainerControl property on EmbeddedWindowControl, a request which Xojo hasn’t addressed in the last 10 years. It makes ContainerControls less valuable. I think that technically it should be easy for Xojo to implement that, but they just don’t care all request are either reviewed and then forgotten or they have never been reviewed at all. Here are a few of them:
2722 - ContainerControl and IsA incompatibility
4491 - Suggestion for getting ContainerControl references via Window.Control
6027 - Traverse Controls on Dynamic ContainerControls
23030 - Add EmbeddedWindowControl.Container property