Advice on Binding Custom Events
Advice on Binding Custom Events
Could someone, please, explain how to Bind Custom Events.
I've read the manual but I don't fully understand what to enter in the optional entries.
Thank you in advance.
I've read the manual but I don't fully understand what to enter in the optional entries.
Thank you in advance.
DE AA EB
Re: Advice on Binding Custom Events
Here's one I've slapped together for demonstrating the custom event binding.
Code: Select all
#Window = 0
#My_Custom_Event = 10
Procedure SayHello()
Debug "My Custom event on window #" + EventWindow()
Debug "Hello"
EndProcedure
OpenWindow(#Window, 100, 100, 200, 200, "Binding To custom event", #PB_Window_SystemMenu)
EditorGadget(0, 10, 10, 180, 180)
BindEvent(#My_Custom_Event, @SayHello())
AddWindowTimer(#Window, 1, 2000)
Repeater.b = 3
Repeat
Event = WaitWindowEvent()
If Event = #PB_Event_Timer And EventTimer() = 1
If Repeater
Repeater-1
PostEvent(#My_Custom_Event)
Else
RemoveWindowTimer(#Window, 1) : Debug " ~~~~ FIN ~~~~~~"
EndIf
EndIf
Until Event = #PB_Event_CloseWindow
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
Re: Advice on Binding Custom Events
@Thunder93: Your example doesn't address his question about the optional parameters.
Re: Advice on Binding Custom Events
True enough.. I'm not trying to be funny here, however I don't know how to explain It any better than what's already in the PB Help file.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
Re: Advice on Binding Custom Events
Code: Select all
BindEvent(Event, @Callback() [, Window [, Object [, EventType]]])
Window Parameter is the Window identifier number. Optional Field unless you wanting to use Object field and also EvenType parameter. The Window parameter could use #PB_All to be used to apply for all running windows.
Object Parameter is is the gadget, menu-item or systray identifier number. Optional Field unless wanting to use EventType parameter. #PB_All to be used to apply for all gadget, menu-item or systray objects.
EventType Parameter to bind to specific event type like (e.g: #PB_EventType_LeftClick, #PB_EventType_Focus.) #PB_All to be used to apply to all event types.
... clear as mud?
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
Re: Advice on Binding Custom Events
@Thunder93
wow such a horrible example. your custom event overlapps with WM_ENABLE. custom events have to start from #PB_Event_FirstCustomValue!
@davido
well, custom events are custom events. you enter whatever you want/need to enter. it depends on your application and what you are trying to achieve. its just a way of communicating within your application. if you dont need the object and type parameters, its good practice to simply use -1, since that value is outside of purebasics range.
c ya,
nco2k
wow such a horrible example. your custom event overlapps with WM_ENABLE. custom events have to start from #PB_Event_FirstCustomValue!
@davido
well, custom events are custom events. you enter whatever you want/need to enter. it depends on your application and what you are trying to achieve. its just a way of communicating within your application. if you dont need the object and type parameters, its good practice to simply use -1, since that value is outside of purebasics range.
Code: Select all
Enumeration
#Window_0 = 0
#Event_0 = #PB_Event_FirstCustomValue
#Event_1
EndEnumeration
OpenWindow(#Window_0, #PB_Ignore, #PB_Ignore, 400, 300, "Test", #PB_Window_SystemMenu)
PostEvent(#Event_0, #Window_0, 123, 456, 789)
PostEvent(#Event_1, #Window_0, -1, -1, 123456789)
Repeat
Select WaitWindowEvent()
Case #Event_0
Debug "Event_0:"
Debug "EventGadget: "+Str(EventGadget())
Debug "EventType: "+Str(EventType())
Debug "EventData: "+Str(EventData())
Debug ""
Case #Event_1
Debug "Event_1:"
Debug "EventGadget: "+Str(EventGadget())
Debug "EventType: "+Str(EventType())
Debug "EventData: "+Str(EventData())
Debug ""
Case #PB_Event_CloseWindow
Break
EndSelect
ForEver
nco2k
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
Re: Advice on Binding Custom Events
If you wanting to be technical.. I'm sure he knows better, heck I even know better. The way to do the custom event wasn't important, it wasn't what the focus for my demonstration. Also in the future, don't be so crude. There is more friendly manner in pointing out incompleteness and such than the way you just done.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
Re: Advice on Binding Custom Events
it doesnt matter. a beginner searches this forum on how to use custom events, finds this thread and copy+pastes the first code he can find. you shouldnt post codes with such basic mistakes, because they set up a bad example for beginners.Thunder93 wrote:If you wanting to be technical.. I'm sure he knows better, heck I even know better. The way to do the custom event wasn't important, it wasn't what the focus for my demonstration.
the world isnt a big liberal arts college. if you got offended by what i just said, well.. sucks to be you then.Thunder93 wrote:Also in the future, don't be so crude. There is more friendly manner in pointing out incompleteness and such than the way you just done.
c ya,
nco2k
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
Re: Advice on Binding Custom Events
You worrying about how code examples should look.. to your standards and assumptions. However you should also be worrying about the manner in which you communicating on here. You setting crude example, and perhaps beginners seeing this thinks this is normal way on PureBasic forums. However to be perfectly honest, you cannot do no harm in being little more respectable with your communications on here. This is often why people are in a feud on PureBasic board.
I've stepped in, because I've seen no responses to this person's post. Trying to be considered, and I get this annoyance from a person.
I've stepped in, because I've seen no responses to this person's post. Trying to be considered, and I get this annoyance from a person.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
Re: Advice on Binding Custom Events
@nco2k: Thanks for the example you posted. Turnabout is fair play though; to use a custom event type you should start enumerating with #PB_EventType_FirstCustomValue.
@Thunder93: Thanks for your example. It also gave an opportunity to mention as a side note of how to come up with the custom event number itself.
@davido: Here's some odds and ends points from my point of view.
For BindEvent(), the syntax is BindEvent(Event, @Callback() [, Window [, Object [, EventType]]]) .
By specifying a window, object, or eventtype you can finetune the callback to handle more specific situations, or group them to be handled in a common way even when they may seem different. You can use different BindEvent()'s that all use the same callback. This means you can use a BindEvent() that handles an event for everything from a given window and another BindEvent() specifying the same callback so that it also handles the event of a specific object and also eventype from a separate window.
For the optional parameters you can specify #PB_All, but if you do that all of the parameters after that one have to also be #PB_All. I think the default for leaving the optional parameters unspecified is that they each equal #PB_All.
If a callback handles an event (or events) from more than one source it can use EventWindow(), EventGadget(), EventType(), EventMenu(), EventTimer(), and Event() (as of v5.60) to help sort out things a bit. This is highly likely if you use #PB_All in the BindEvent() for any of the optional parameters or you use more than one BindEvent() with the same callback.
For PostEvent(), the syntax is PostEvent(Event [, Window, Object [, Type [, Data]]]) .
The important thing to note here is that any custom events or custom types be first enumerated starting with #PB_Event_FirstCustomValue, or #PB_EventType_FirstCustomValue respectively. You can also use values that PureBasic already defines but these won't be posted to the operating systems event loop, just PureBasic's event loop. This means you can't trigger the operating system to do something by posting a custom event.
You have the option to add a data value which is an integer and more importantly can also be a pointer to a larger structure or object. It's value can be retrieved with EventData() in your callback procedure. If you use the data parameter but not the other preceding optional parameters then settle on an unused value such as -1 (as demonstrated by nco2k.).
@Thunder93: Thanks for your example. It also gave an opportunity to mention as a side note of how to come up with the custom event number itself.
@davido: Here's some odds and ends points from my point of view.
For BindEvent(), the syntax is BindEvent(Event, @Callback() [, Window [, Object [, EventType]]]) .
By specifying a window, object, or eventtype you can finetune the callback to handle more specific situations, or group them to be handled in a common way even when they may seem different. You can use different BindEvent()'s that all use the same callback. This means you can use a BindEvent() that handles an event for everything from a given window and another BindEvent() specifying the same callback so that it also handles the event of a specific object and also eventype from a separate window.
For the optional parameters you can specify #PB_All, but if you do that all of the parameters after that one have to also be #PB_All. I think the default for leaving the optional parameters unspecified is that they each equal #PB_All.
If a callback handles an event (or events) from more than one source it can use EventWindow(), EventGadget(), EventType(), EventMenu(), EventTimer(), and Event() (as of v5.60) to help sort out things a bit. This is highly likely if you use #PB_All in the BindEvent() for any of the optional parameters or you use more than one BindEvent() with the same callback.
For PostEvent(), the syntax is PostEvent(Event [, Window, Object [, Type [, Data]]]) .
The important thing to note here is that any custom events or custom types be first enumerated starting with #PB_Event_FirstCustomValue, or #PB_EventType_FirstCustomValue respectively. You can also use values that PureBasic already defines but these won't be posted to the operating systems event loop, just PureBasic's event loop. This means you can't trigger the operating system to do something by posting a custom event.
You have the option to add a data value which is an integer and more importantly can also be a pointer to a larger structure or object. It's value can be retrieved with EventData() in your callback procedure. If you use the data parameter but not the other preceding optional parameters then settle on an unused value such as -1 (as demonstrated by nco2k.).
Last edited by Demivec on Wed Sep 27, 2017 12:24 am, edited 1 time in total.
Re: Advice on Binding Custom Events
@Demivec: Nicely put.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
Re: Advice on Binding Custom Events
@Demivec
in my defense, #PB_EventType_FirstCustomValue was added in a later version but you dont need to use it for your own custom messages (non-pb objects) because they wont produce any #PB_* events and therefor cant really clash with purebasics internals. PostEvent() + Type/Data is basically just PostMessage_() + wParam/lParam. so you can use whatever you want/need. but yes, if you plan on extending purebasic object events, then you should use it. #PB_Event_FirstCustomValue is mandatory though, cause it could clash with windows itself.
c ya,
nco2k
in my defense, #PB_EventType_FirstCustomValue was added in a later version but you dont need to use it for your own custom messages (non-pb objects) because they wont produce any #PB_* events and therefor cant really clash with purebasics internals. PostEvent() + Type/Data is basically just PostMessage_() + wParam/lParam. so you can use whatever you want/need. but yes, if you plan on extending purebasic object events, then you should use it. #PB_Event_FirstCustomValue is mandatory though, cause it could clash with windows itself.
c ya,
nco2k
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
Re: Advice on Binding Custom Events
Thanks for the additional explanation.nco2k wrote:in my defense, #PB_EventType_FirstCustomValue was added in a later version but you dont need to use it for your own custom messages (non-pb objects) because they wont produce any #PB_* events and therefor cant really clash with purebasics internals. PostEvent() + Type/Data is basically just PostMessage_() + wParam/lParam. so you can use whatever you want/need. but yes, if you plan on extending purebasic object events, then you should use it. #PB_Event_FirstCustomValue is mandatory though, cause it could clash with windows itself.
I hadn't noticed it either until just today.
Re: Advice on Binding Custom Events
Where's the overlaps / clashing that makes my earlier demonstration horrible example?nco2k wrote:@Thunder93
wow such a horrible example. your custom event overlapps with WM_ENABLE. custom events have to start from #PB_Event_FirstCustomValue!
Code: Select all
#My_Custom_Event = 10
Procedure WinCallback(hWnd, uMsg, wParam, lParam)
If uMsg = #WM_ENABLE
If wParam
Debug "Window Enabled (#WM_ENABLE message) ID: "+Str(uMsg)
Else
Debug "Window Disabled (#WM_ENABLE message) ID: "+Str(uMsg)
EndIf
EndIf
ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure
Procedure SayHello()
Debug "Hello...! My Custom event on window #" + EventWindow()
EndProcedure
OpenWindow(0, 100, 100, 200, 200, "Binding to custom event", #PB_Window_SizeGadget | #PB_Window_SystemMenu)
SetWindowCallback(@WinCallback())
BindEvent(#My_Custom_Event, @SayHello())
Debug "Disabling Window"
DisableWindow(0, #True)
Delay(1000)
Debug #LF$+"Enabling Window"
DisableWindow(0, #False)
Debug #LF$+"Performing PostEvent() - #1"
PostEvent(#My_Custom_Event)
Debug #LF$+"Performing PostEvent() - #2"
PostEvent(#My_Custom_Event)
Debug #LF$+"Performing PostEvent() - #3"
PostEvent(#My_Custom_Event)
Debug #LF$+"Performing PostEvent() - #4"
PostEvent(#My_Custom_Event)
Repeat
Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
Re: Advice on Binding Custom Events
Exactly how you think..Demivec wrote:For the optional parameters you can specify #PB_All, but if you do that all of the parameters after that one have to also be #PB_All. I think the default for leaving the optional parameters unspecified is that they each equal #PB_All.
Code: Select all
Enumeration Windows
#Window_1 = 0
#Window_2
EndEnumeration
Enumeration #PB_Event_FirstCustomValue
#My_Custom_Event
EndEnumeration
Procedure SayHello()
Debug "Hello!... My Custom event on window #" + EventWindow()
EndProcedure
OpenWindow(#Window_1, 100, 100, 200, 200, "Binding to custom event", #PB_Window_SystemMenu)
OpenWindow(#Window_2, 302, 100, 200, 200, "Binding to custom event", #PB_Window_SystemMenu)
BindEvent(#My_Custom_Event, @SayHello())
PostEvent(#My_Custom_Event, #Window_1, #PB_Any)
PostEvent(#My_Custom_Event, #Window_2, #PB_Any)
Repeat
Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley