Window reference from Toolbar

I am trying to acces controls in a window from a method in the toolbar.

If I access the control directly by using the Windows given name then it works.

If I access the control trough a reference is doesn’t work, the compiler tells me the item does not exist.
The reference is set in the constructor of the toolbar, which is a subclass from my baseToolBar class.
Looking in the debugger the property [Window] and my own created property contain the same object reference.

I have tried to use :
me.Window.Control.Caption
self.Window.Control.Caption
wRef.Control.Caption

I am obviously doing something wrong here, could somebody point out my flawed thinking here.

Thanks in advance for the help.

Self refers to the window that your control is on, so self.control.caption would be the way to go.

[quote=65429:@Wayne Golding]Self refers to the window that your control is on, so self.control.caption would be the way to go.[/quote] Thank you for you’re reply Wayne, but I think I haven’t properly described my problem.

In the constructor of the toolbar I am storing a reference to the window ( it get’s properly set)
I have checked this by using msgbox to check if I can access the title, which is displayed correctly.

But when I try to access a property, method or control on the window I get the “Item does not exist” message.

msgBox( wWindowRef.Title) // No Error
wWindowRef.Record_Delete // Item does not exist/

So I know I am missing something important here concerning referencing objects.
But for the likes of me I can’t seem to figure it out.

Paul,

did you look at:
Window.Control
Window.ControlCount

If these are what you are searching, take the number of controls and loop thru the Window controls to get what you want.
If you are searching for a specific control (ListBox, TextArea ?), try:
IsA
Watch carefully the examples there.

HTH

Here’s a simple piece of code who returns the names of the Controls found in Self:

[quote]// Simple test
If Self.ControlCount > 0 Then
Dim LoopIdx As Integer
Dim CtrlKind As String

For LoopIdx = 0 To Self.ControlCount - 1
  CtrlKind = CtrlKind + Self.Control(LoopIdx).Name + EndOfLine
  
Next

MsgBox "The main window have " + Str(wMain.ControlCount) + " available Controls." + EndOfLine +_
 EndOfLine + CtrlKind

End If
[/quote]

The returned informations are: the number of available Controls and a list of their name (either TexTArea1 or TA_Result).

The shared code have been in ToolBar1.Open as requested.
Note that the ToolBar Control is in the list… (ToolBar1 [?] if you do not renamed it; I renamed mine).

Is it what you were searching for ?

A bit expanded example below:

[code] // Simple test
If Self.ControlCount > 0 Then
Dim LoopIdx As Integer
Dim CtrlKind As String
Dim TA As TextArea

For LoopIdx = 0 To Self.ControlCount - 1
  CtrlKind = CtrlKind + Self.Control(LoopIdx).Name + EndOfLine
  
  If Self.Control(LoopIdx) IsA TextArea Then
    TA = TextArea(Self.Control(LoopIdx))
    TA.SelText = "This is " + TA.Name + EndOfLine
  End If
  
Next

MsgBox "The main window have " + Str(wMain.ControlCount) + " available Controls." + EndOfLine +_
 EndOfLine + CtrlKind

End If[/code]

I hope this helps.

BTW: I got in the TextArea Control:
This is TextArea1

I stop here: I added three more TextArea, modify the MsgBox line to (I removed MsgBox and replaced with):

TextArea4.SelText =

and get:

This is TextArea4
The main window have 11 available Controls.

PB_Draw
PB_Save
PB_Open
TB_Object2D1
PB_DrawTriangles
PB_DrawRosaces
PB_DrawArcs
TextArea1
TextArea2
TextArea3
TextArea4

And from there, you can get informations on what you have in your ToolBar (ToolItem Name, Caption, etc.).

Place the code to deal with the ToolBar contents inside a second loop.

More code have to be added to scan and report the whole ToolBar contents.

Hi Emile,

Thank you for the examples and referrals to the manual.
Everything you have described was already known (but never the less thank you for you’re effort)

It doesn’t however solve my issue, I am trying to access a method on the window.
The method exists but I do not want to use a direct calling method but I want too indirectly call the method.

What I have found so far is that I must be storing the wrong type as the window reference, but still have no clue on how to solve this.
this is what works in the toolbar :
msgBox( wWindowRef.Title) // No Error

wWindowRef.Record_Delete // CompileError: Item does not exist/

What is Record_Delete ?

That is a method I created on Window level.

just for clarification I want to reference window[prop/methods] from toolbar, but indirectly so the code becomes reusable.

How is wWindowRef declared (what data type)?

What you mention should work under these circumstances.

a) The Window has been instantiated (which it sounds like it has based on your other responses)
b) The Method MUST be marked as PUBLIC … otherwise they can only be references internal to the window, or subclasses of that window

So I’m thinking “B” might be the problem here?

You might use an Interface to solve this problem. Create a Class Interface with a method definition for Record_Delete (and any other methods you intend to use). Have your window implement this interface. Define your property as

wWindowRef as MyInterfaceName

wWindowRef is typed as a Window

[quote=65653:@Dave S]What you mention should work under these circumstances.

a) The Window has been instantiated (which it sounds like it has based on your other responses)
b) The Method MUST be marked as PUBLIC … otherwise they can only be references internal to the window, or subclasses of that window

So I’m thinking “B” might be the problem here?[/quote]
And the method is defined as public.

And in response too Tim’s remark about declaring a interface looks like this doesn’t resolve my current issue.
if my window has this interface, I still need to access it trough a reference off the window.

In the toolbar code I want to reference the method on my window indirectly, so I stored the window reference in a property.
But then I am not allowed to call : wWinRef.Method

And before i forget thank you all for the comments it is really appreciated.

[quote=65673:@Paul Ross]wWindowRef is typed as a Window
[/quote]
There’s the problem
A Window does not have a method named Record_Delete - a Windows only has the methods listed on http://documentation.xojo.com/index.php/Window

But YOUR window in your project (which is a subclass of Window does)

Change the declaration to use the name of your window instead as that kind of window DOES have that method

That is not the correct type if you want to access the type-specific methods. You need to definite it as the actual type of the window. So if the Window is called MyDefaultWindow, then the declaration would be:

wWindowRef As MyDefaultWindow

When you set the type to Window, then you can only access methods and properties that are part of the Window type (such as Title) unless you first “cast” it to the correct type like this:

MyDefaultWindow(wWindowRef).Record_Delete

Somehow I was looking in the right direction but it did not work at first.

I tried the answer provided by Norman and Paul (thank you gentlemen)
But I still got the same error, quit Xojo.
And retried it and behold it works.

[quote=65673:@Paul Ross]And in response too Tim’s remark about declaring a interface looks like this doesn’t resolve my current issue.
if my window has this interface, I still need to access it trough a reference off the window.[/quote]
It does work and is more generalizable. You can use the exact same code for any number of windows, as long as they implement the interface. I’d recommend you look into this further as it could take your programming to the next level.