Access window controls from a class

Hi all. I’d like to reference a window’s controls from a class, modifying some control properties.

Multiple windows would have controls of the same name, each window would create an instance of the class.

Can I pass a window object or handle to a class instance and have access to that window’s controls from the class instance?


That’s going at the problem backwards. The window should manage its own controls.

  1. Create a new event definition within your class. (Let’s call it DoSomething)
  2. When the class instance needs to modify a window’s control, do RaiseEvent DoSomething
  3. In the class instance in the window, select new event and select your DoSomething (If you don’t see it, save your project and it should be there.)
  4. Now in this event, change the control in whatever way you would like.
    The beauty of this technique is that
  5. the Window handles its own controls.
  6. Each window which contains your class instance can respond to the Do Something event however it needs to.

Have a property in your class or declare a variable in code for storing the window reference into a variable.

So for example, something like this (either in code or added through a property of the class (depends on wether it is to be used after the execution of the constructor).

Then create a method called ‘Constructor’, this will be executed when a new instance of the class is made. This will also allow you to pass in some parameters.

Have a parameter in the constructor as something like ‘win as window’ or ‘win as myWindow’ (if myWindow subclasses window).

Then code into the method something to method something like ‘theWindow = win’. That is if you declare theWindow as a property of the class.

If you want to refer to the window from just the constructor, refer the window using ‘win’, as it is declared as parameter of the constructor or you can reference theWindow (outside and inside the constructor).

I’ll put this out in code for you to make it easier to understand:

//reference the window on the same line as creating an instance of myClass
dim myObject as new myClass(self)

class myClass
//class property (not declared in code)
dim theWindow as window

sub constructor(win as window)
theWindow = win
end sub

sub closeWindowForThisClass()
end sub
end class

Probably confusing but you can ask me any questions if you wish. I wrote ‘dim theWindow as window’ as pseudo code representing the declaring of a variable as a property instead of through code if that makes sense.

BTW - If you are interested in learning more about the power of event definitions within Xojo, the definitive book on the subject is linked here (from another topic, link provided by Norman Palardy)

Use a class interface. Define methods that tell the window to update themself with this information. That way, your class doesn’t need to know the details, it just throws the data at the window and lets the window do whatever is appropriate.

Sounds like an interesting approach but I am a little confused with your explanation. How does the class interface interact with the window?


The window implements the class interface. you pass a parameter of type your-class-interface to be object instance. The object calls the methods in the class interface and passes its data to them. any window that implements the interface can be passed to the object and it doesn’t need to know the details of the window.

If you have 2 coordinates, X1/Y1 and X2/Y2, then the width of the rectangle that joins them will be Abs(X2-X1)+1. Similarly for the height.

Forum is posting my stuff in the wrong threads. This is the second time it’s happened in the last couple of days.

Are you opening a bunch of topics in tabs?

No, I posted and then immediately posted again, and the second post wound up in a thread I had previously posted on.