RaiseEvent on ContainerControl

I have a listbox on a container control. On the container I have an event definition PropertyChanged. I want to raise this event when the listbox CellTextChange event is raised. If I try to raise this event in the CellTextChange event of the listbox I get the error ‘This item does not exist’.

What am I missing here?

Events can only be raised in the class where they are defined.

Ok sorted it. Thanks

So if anyone else needs a solution, the way I did this was to have computed properties on the container. These are set from the listbox. In the set of the computed, the event is raised.

A method, in my opinion, could be more appropriated. Something like:

Class ContainerControl1     // Container with a listBox
Inherits ContainerControl

//Define ContainerControl1.Events.MyChangedEvent():  // New Event Definition
Public Event MyChangedEvent()

//Define ContainerControl1.Methods.RaiseMyChangedEvent(): // A method used just to raise my Event
Sub RaiseMyChangedEvent()
  RaiseEvent MyChangedEvent  // Fire my new Event
End Sub

//Define ContainerControl1.Listbox1.Events.Change():  // Let's fire MyChangedEvent when ListBox Change Event fires
Sub Change()               // The change Event from the contained ListBox
  RaiseMyChangedEvent()  // Tell the container to fire my event
End Sub

End Class

As Rick has pointed out, Raising Events in a Container isn’t so hard to do. I rely on this functionality for a LOT of our projects. We are big fans of Containers to make UI modular and easier to code.

[quote=115708:@Mike Charlesworth]Ok sorted it. Thanks

So if anyone else needs a solution, the way I did this was to have computed properties on the container. These are set from the listbox. In the set of the computed, the event is raised.[/quote]
Really no need
If you have a container that contains a listbox and the container has an event definition, Foo, then in the listbox you can raiseEvent Foo
When you drop an instance of this container on a layout you can then implement the Foo event

True. There is no need to try to bubble the event out, it’s already there. :stuck_out_tongue:

Class ContainerControl1     // Container with a listBox
Inherits ContainerControl

//Define ContainerControl1.Events.MyChangedEvent():  // New Event Definition
Public Event MyChangedEvent()

//Define ContainerControl1.Listbox1.Events.Change():  // Let's fire MyChangedEvent when ListBox Change Event fires
Sub Change()               // The change Event from the contained ListBox
  RaiseEvent MyChangedEvent  // Fire my new Event
End Sub

End Class

So now I am confused with OP question in why he can’t find the event to raise.

Without seeing code or a sample I have no idea

It is exactly as described in the OP.

Since I know this works (because do this all the time), you’re doing something wrong in your code. Without seeing it we can’t help.

Mike, here is what you said working without additional tricks

1 Like

Thanks guys, I thought it worked too. I have done it this way in the past and had no problem. Rick your example works fine and as I wanted. I have just tried to recreate what I did this morning and no problem it works fine. I cant explain it but it is working fine now. I must have done something wrong but cant see what now.

Does that then mean Eli’s quote below is incorrect?

Not really. Just does not apply to the current context.

I.e container controls?

See my example:

Class ContainerControl1     // Container with a listBox
Inherits ContainerControl

//Define ContainerControl1.Events.MyChangedEvent():  // New Event Definition
Public Event MyChangedEvent()

//Define ContainerControl1.Listbox1.Events.Change():  // Let's fire MyChangedEvent when ListBox Change Event fires
Sub Change()               // The change Event from the contained ListBox
  RaiseEvent MyChangedEvent  // Fire my new Event
End Sub

End Class

The container is itself a class. The change() method is in the ContainerControl1 Class level and the raise ocurred there.

[quote=115906:@Rick Araujo]See my example:

Class ContainerControl1     // Container with a listBox
Inherits ContainerControl

//Define ContainerControl1.Events.MyChangedEvent():  // New Event Definition
Public Event MyChangedEvent()

//Define ContainerControl1.Listbox1.Events.Change():  // Let's fire MyChangedEvent when ListBox Change Event fires
Sub Change()               // The change Event from the contained ListBox
  RaiseEvent MyChangedEvent  // Fire my new Event
End Sub

End Class

The container is itself a class. The change() method is in the ContainerControl1 Class level and the raise ocurred there.[/quote]
I see, thanks Rick

:wink: