Dialogue between window and class

Hello everyone!

I don’t know how to solve a little problem, so I’m asking someone who knows more than me.

I have a window that calls a method of a class by passing it parameters; this method creates a recordset and processes it.
I need to make the window display a couple of data present in each record that the class processes.

I would say that the class method should raise an event, but I don’t know how to do it.

Can someone help me?

I am using Xojo 2025r1.1 on Windows 11.

Thanks!

Nedi

I’m inclined to think that there are no asynchronous things in your process.

So maybe just (algorithm, you need to write things):

Var obj As New MyClassWithRecords

obj.DoTheProcessing()

If obj.MoveToFirstRecord()  Then // False if fail
  Do
    DisplayThisRecord(obj.GetCurrentRecord())
  Loop Until Not obj.MoveToNextRecord() // True if could move to next
End

use selectsql to insert data because it can use the returning data (rowset)
you could give the method the output listbox to insert data direct. (OOP)
or collect data first (object array) and then display it.
or give the method the window and call a method from there to add/display this data of interest.
if you have multiple windows with the same need, add/create a interface (Display Method) to the windows.

The class method runs a loop reading a recordset, and for each record processed it must be able to notify the window about the record it has just processed, and the window must notify the user. The class could do this by raising an event that the window should catch, but I don’t know how to do it.

I like to use an event bus for this sort of thing, and pretty much all my apps use Ian Jones’ EventBusIJ, with a few simple customizations of my own.

Any object can subscribe to any event, any object can publish any event, and a variant can be passed with any event.

1 Like

You could possibly use a delegate to supply the window function to the class.

1 Like

Add the event to the class and in the Method code use RaiseEvent to fire the event as needed.

Drag the class onto the window, creating an instance of the class. Use the menus to add the event to that instance.

The event on the instance is now part of the window’s code and can interact as needed.

4 Likes

just notify or you will ask a yes no via dialog there?
simplified its

method loop(w As mywindow) 
if w.mydialog(row) then

Thank you all for your posts!
Tim’s solution is what was on my mind, and it works very well!
What I was missing was dragging the class onto the window: I had defined the class instance as a property of the window, so I couldn’t handle the event defined in the parent class.
Another great thing to add to my Xojo knowledge!
Thanks again to all of you: you are awesome!

2 Likes

You could, using AddHandler if you wanted to. You just need to be sure to use RemoveHandler later when destroying the instance or the Window to prevent leaks.

1 Like

Thank you, Greg: I will try soon your solution!

Hi Greg, I’m having some trouble developing what you suggested.
I have the class clsCommTestate in which I inserted an Event Definition ElaborataCarpetta(Anno As Integer, NumCarp As Integer).
In a window I defined oCommTes As clsCommTestate as window property, and in the Opening of the window I instantiated the class with oCommTes = New clsCommTestate.
Now I would like to execute AddHandler, but I can’t indicate the event ElaborataCarpetta, because it doesn’t exist in my instance.
What should I do?
Thanks!

Maybe I need to drag the class onto the window, as Tim suggested?
In this case it is easier to write the code directly in the event rather than using AddHandler.

the event definition

Event Find(v as String)

the call method

Public Sub Find(obj as WindowFind, v as String)  
End Sub

init

Var win As New WindowFind
AddHandler win.Find, WeakAddressOf Find

the calling method get the sender object as first argument.

could look like

oCommTes = New clsCommTestate
AddHandler oCommTes.ElaborataCarpetta, WeakAddressOf ElaborataCarpetta
Public Sub ElaborataCarpetta(obj as clsCommTestate, Anno As Integer, NumCarp As Integer)  
End Sub

Thank you, but the AddHandler line raise the error "Type clsCommTestate has no member named “ElaborataCarpetta”

it should look like

And this is exactly what I did in creating the class.
Immagine 1

But when I create the instance of the class in the window and try to run AddHandler it gives me an error


Immagine 4

Are you not missing a comma?

1 Like

Super Tim!!
You are right, and with the missing comma everything is working!

Thank a lot to you and Markus, who gives the right solution!

1 Like

Ok, Greg: it works!

Thank to you (and @MarkusR) for this good solution!

1 Like