User Tools

Site Tools


codesamples:broadcast_message_winforms

Broadcasting a message to all windows in a WinForms application

All VO users know about BroadcastMessage method from SSA example to etablish a communication system between different windows (Forms). It’s very easy to implement this in X#. It’s also more powerful.

You can use “BroacastMessage” from EVERYWHERE in your code. There is even no need to identify a particular “Sender” (oSender: NULL_OBJECT) but every opened Form can receive the message by implementing a “BroadCastMessageReceive” method

PUBLIC FUNCTION BroadCastMessage(oSender AS Form, cMessage AS STRING, oExtra AS OBJECT) AS VOID PASCAL
  LOCAL aParameters AS OBJECT[]
  LOCAL oFormsList:= List<Form>{} AS List<Form>
  LOCAL oReturnValue AS OBJECT
  aParameters:= <OBJECT>{oSender,cMessage,oExtra}
  //We "copy" the "current" Opened Forms list ...into a local variable (oFormList).   
  //Why? The FOREACH...NEXT doesn't accept that a change occurs in its list. 
  //Such a change can occur if one of the opened forms, during the loop,  opens a NEW Form in its "BroadCastMessageReceive"...
  FOREACH oForm AS Form IN Application.OpenForms
    oFormsList:Add(oForm)
  NEXT                                                    
 
  FOREACH oForm AS Form IN oFormsList          
    IF !oForm:IsDisposed
      oReturnValue:=Send(oForm,"BroadCastMessageReceive",aParameters)
      IF oReturnValue IS LOGIC  .and. ! (LOGIC)oReturnValue  
        //If a particular Form:BroadcastMessageReceive method returns FALSE,
        //exit the loop  
        EXIT
      ENDIF
    ENDIF
  NEXT
 
PUBLIC FUNCTION Send( self oObject as object, cMethod as string, aParameters as object[] ) as object 
  local oType as System.Type
  local oReturn as object
  local oInfo as MethodInfo
 
  oType := oObject:GetType()
  oReturn := null_object
  oInfo := oType:GetMethod( cMethod )
  if oInfo != null_object
    oReturn := oInfo:Invoke( oObject, aParameters )
  endif
 
  oType := null_object
  oInfo := null_object
 
  return oReturn

Implementation example.

Class xyz inherit Form
Method xyz_FormClosed(sender AS OBJECT, e AS System.Windows.Forms.FormClosedEventArgs) AS VOID
 
  BroadcastMessage(SELF,"xyz_FormClosed",oUsefullThingOrNULLOBJECT)
 
CLASS MySpecialTextBoxControl inherit Textbox
  Method xx()
  BroadCastMessage(SELF :FindForm(), "Hello",SELF)                     
 
CLASS abc inherit Form
 
Method BroadCastMessageReceive(oSender as Form,cMessage as STRING,oExtra as OBJECT) AS LOGIC
 
  do case
  case cMessage=="xyz_FormClosed"
    do something
    // If you are sure no other form needs this message, you can return FALSE
    RETURN FALSE
  Case cMessage = « Hello » .and. oExtra IS MySpecialTextBoxControl
    Local oTbox := < MySpecialTextBoxControl >oExtra As MySpecialTextBoxControl
    Do something   
  endcase              
  RETURN TRUE //This means the Broadcast Function will continue his loop...

Code courtesy by Guy Deprez

codesamples/broadcast_message_winforms.txt · Last modified: 2018/09/11 04:12 by wolfgangriedmann