User Tools

Site Tools


codesamples:broadcast_message_winforms

This is an old revision of the document!


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 AS FormCollection    
  LOCAL oReturnValue AS OBJECT
  aParameters:= <OBJECT>{oSender,cMessage,oExtra}
  oFormsList:= Application.OpenForms

  FOREACH oForm AS Form IN oFormsList
    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
  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...
codesamples/broadcast_message_winforms.1528396190.txt.gz · Last modified: 2018/06/07 18:29 by wolfgangriedmann