Re: RadioGroup -- Detecting Focus
Posted: Tue Apr 26, 2022 9:36 am
Hola Red2.
Aquí te comparto mi mi idea, espero que te pueda servir.
Un saludo cordial y una cervecita virtual!!!
Exclusive forum for HMG, a Free / Open Source xBase WIN32/64 Bits / GUI Development System
http://mail.hmgforum.com/
Hola Red2.
Code: Select all
#include "hmg.ch"
Function Main
#define aStdColor WHITE
#define aActiveColor { 020, 210, 040 }
DEFINE WINDOW Form_1 ;
AT 0,0 ;
WIDTH 640 HEIGHT 480 ;
TITLE 'HMG Demo' ;
MAIN ;
FONT 'Arial' SIZE 10 ;
BACKCOLOR aStdColor
DEFINE RADIOGROUP Radio_1
OPTIONS { 'One' , 'Two' , 'Three', 'Four' }
VALUE 1
WIDTH 100
TOOLTIP 'RadioGroup'
ONCHANGE MsgInfo( "On Change", "Radio 1" )
READONLY { .F. , .T. , .F. , .T. }
ROW 10
COL 10
BACKCOLOR aStdColor
END RADIOGROUP
DEFINE RADIOGROUP Radio_2
ROW 10
COL 150
OPTIONS { 'A' , 'B' , 'C', 'D' }
VALUE 1
WIDTH 100
TOOLTIP 'RadioGroup'
READONLY { .F. , .F. , .F. , .F. }
BACKCOLOR aStdColor
END RADIOGROUP
HMG_RadioGroupSetEvent( "Form_1", "Radio_1", "OnGotFocus", { || Form_1.Radio_1.BackColor := aActiveColor } )
HMG_RadioGroupSetEvent( "Form_1", "Radio_1", "OnLostFocus", { || Form_1.Radio_1.BackColor := aStdColor } )
HMG_RadioGroupSetEvent( "Form_1", "Radio_2", "OnGotFocus", { || Form_1.Radio_2.BackColor := aActiveColor } )
HMG_RadioGroupSetEvent( "Form_1", "Radio_2", "OnLostFocus", { || Form_1.Radio_2.BackColor := aStdColor } )
HMG_RadioGroupSetEvent( "Form_1", "Radio_2", "OnChange", { || MsgInfo( "On Change", "Radio 2" ) } )
@ 150,10 TEXTBOX Text_1 ;
VALUE "Some text" ;
BACKCOLOR aStdColor ;
ON GOTFOCUS Form_1.Text_1.BackColor := aActiveColor ;
ON LOSTFOCUS Form_1.Text_1.BackColor := aStdColor
END WINDOW
Form_1.Text_1.SetFocus
Form_1.Center
HMG_EnableRadioGroupGotLostFocusEvents( .F. /* lProccedEachRadioButtonEvent */ )
Form_1.Activate
Return Nil
**********************************************************
Function HMG_RadioGroupSetEvent ( cForm, cControl, cEventName, bNewProc )
LOCAL nControlIndex := GetControlIndex( cControl, cForm )
LOCAL nEvent := 0
IF .Not. hb_isBlock ( bNewProc )
bNewProc := ""
ENDIF
IF nControlIndex = 0 .OR. _HMG_SYSDATA [ 1 ] [ nControlIndex ] <> "RADIOGROUP"
Return Nil
ENDIF
DO CASE
CASE Upper( cEventName ) = "ONLOSTFOCUS"
nEvent := 10
CASE Upper( cEventName ) = "ONGOTFOCUS"
nEvent := 11
CASE Upper( cEventName ) = "ONCHANGE"
nEvent := 12
ENDCASE
IF nEvent > 0 //store event action
_HMG_SYSDATA [ nEvent ] [ nControlIndex ] := bNewProc
ENDIF
Return Nil
**********************************************************
Function HMG_RadioGroupGotLostFocusEvents( lProccedEachRadioButtonEvent )
#define WM_COMMAND 0x0111
#define BN_KILLFOCUS 0x0007
#define BN_SETFOCUS 0x0006
Local nMsg := EventMSG ()
Local hWnd := EventHWND ()
Local wParam := EventWPARAM ()
Local lParam := EventLPARAM ()
Local nControlIndex
Static _nLastRadioGroupFocusedIndex := 0
Default lProccedEachRadioButtonEvent := .T.
IF nMsg == WM_COMMAND
nControlIndex := hb_AScan ( _HMG_SYSDATA [ 3 ], { | xHandle | IF ( hb_isArray ( xHandle ), hb_AScan ( xHandle, lParam, , , .T. ) <> 0 , .F. ) } )
IF nControlIndex > 0 .AND. _HMG_SYSDATA [ 1 ] [ nControlIndex ] == "RADIOGROUP"
DO CASE
CASE HIWORD( wParam ) == BN_KILLFOCUS .AND. lProccedEachRadioButtonEvent //Call OnLostFocus action for each radio button
_DoControlEventProcedure( _HMG_SYSDATA [ 10 /* OnLostFocus */ ] [ nControlIndex ], nControlIndex )
RETURN 0
CASE HIWORD( wParam ) == BN_SETFOCUS //Call OnGotFocus action
IF lProccedEachRadioButtonEvent
_DoControlEventProcedure( _HMG_SYSDATA [ 11 /* OnGotFocus */ ] [ nControlIndex ], nControlIndex )
RETURN 0
ELSEIF _nLastRadioGroupFocusedIndex <> nControlIndex //Other radiogroup get focus
IF _nLastRadioGroupFocusedIndex <> 0 //call last radiogroup onLostFocus action
_DoControlEventProcedure( _HMG_SYSDATA [ 10 /* OnLostFocus */ ] [ _nLastRadioGroupFocusedIndex ], _nLastRadioGroupFocusedIndex )
ENDIF
_nLastRadioGroupFocusedIndex := nControlIndex //store current radiogroup
_DoControlEventProcedure( _HMG_SYSDATA [ 11 /* OnGotFocus */ ] [ nControlIndex ], nControlIndex )
RETURN 0
ENDIF
ENDCASE
ELSE
IF .Not. lProccedEachRadioButtonEvent .AND. _nLastRadioGroupFocusedIndex <> nControlIndex .AND. _nLastRadioGroupFocusedIndex > 0 //Other control type than radiogroup -> call last radiogroup onLostFocus action
_DoControlEventProcedure( _HMG_SYSDATA [ 10 /* OnLostFocus */ ] [ _nLastRadioGroupFocusedIndex ], _nLastRadioGroupFocusedIndex )
_nLastRadioGroupFocusedIndex := 0
RETURN Nil
ENDIF
ENDIF
ENDIF
RETURN Nil
*************************************************************************************
Function HMG_EnableRadioGroupGotLostFocusEvents( lProccedEachRadioButtonEvent )
Local cProc := "HMG_RadioGroupGotLostFocusEvents( " + hb_ValToExp ( lProccedEachRadioButtonEvent ) + " )"
Local nIndex := EventCreate( cProc, NIL, NIL )
EventProcessHMGWindowsMessage ( nIndex, .T. )
RETURN Nil
Code: Select all
#include "hmg.ch"
Function Main
#define aStdColor WHITE
#define aActiveColor { 020, 210, 040 }
DEFINE WINDOW Form_1 ;
AT 0,0 ;
WIDTH 640 HEIGHT 600 ;
TITLE 'HMG Demo' ;
MAIN ;
FONT 'Arial' SIZE 10 ;
BACKCOLOR aStdColor
DEFINE RADIOGROUP Radio_1
OPTIONS { 'One' , 'Two' , 'Three', 'Four' }
VALUE 1
WIDTH 100
TOOLTIP 'RadioGroup'
ONCHANGE addlog( "Radio_1 OnChange" )
READONLY { .F. , .T. , .F. , .T. }
ROW 10
COL 10
BACKCOLOR aStdColor
END RADIOGROUP
DEFINE RADIOGROUP Radio_2
ROW 10
COL 150
OPTIONS { 'A' , 'B' , 'C', 'D' }
VALUE 1
WIDTH 100
TOOLTIP 'RadioGroup'
READONLY { .F. , .F. , .F. , .F. }
BACKCOLOR aStdColor
END RADIOGROUP
HMG_RadioGroupSetEvent( "Form_1", "Radio_1", "OnGotFocus", { || (addlog ( "Radio_1 onGotFocus" ), Form_1.Radio_1.BackColor := aActiveColor )} )
HMG_RadioGroupSetEvent( "Form_1", "Radio_1", "OnLostFocus", { || (addlog ( "Radio_1 onLostFocus" ), Form_1.Radio_1.BackColor := aStdColor )} )
HMG_RadioGroupSetEvent( "Form_1", "Radio_2", "OnGotFocus", { || (addlog ( "Radio_2 onGotFocus" ), Form_1.Radio_2.BackColor := aActiveColor )} )
HMG_RadioGroupSetEvent( "Form_1", "Radio_2", "OnLostFocus", { || (addlog ( "Radio_2 onLostFocus" ), Form_1.Radio_2.BackColor := aStdColor )} )
HMG_RadioGroupSetEvent( "Form_1", "Radio_2", "OnChange", { || addlog( "Radio_2 OnChange" ) } )
@ 120,10 TEXTBOX Text_1 ;
VALUE "Some text" ;
BACKCOLOR aStdColor ;
ON GOTFOCUS ( addlog ( "Text_1 onGotFocus" ), Form_1.Text_1.BackColor := aActiveColor ) ;
ON LOSTFOCUS ( addlog ( "Text_1 onLostFocus" ), Form_1.Text_1.BackColor := aStdColor )
@ 150,10 TEXTBOX Text_2 ;
VALUE "Some text 2" ;
BACKCOLOR aStdColor ;
ON GOTFOCUS ( addlog ( "Text_2 onGotFocus" ), Form_1.Text_2.BackColor := aActiveColor );
ON LOSTFOCUS ( addlog ( "Text_2 onLostFocus" ), Form_1.Text_2.BackColor := aStdColor )
@ 180,10 DATEPICKER Date_1 ;
VALUE DATE() ;
TOOLTIP 'DatePicker Control' ;
ON GOTFOCUS addlog ( "Date_1 onGotFocus" ) ;
ON LOSTFOCUS addlog ( "Date_1 onLostFocus" )
@ 210,10 BUTTON Button_1 CAPTION "Button" WIDTH 100 HEIGHT 24 ;
ACTION addlog ("Button_1 pressed") ;
ON GOTFOCUS addlog ( "Button_1 onGotFocus" ) ;
ON LOSTFOCUS addlog ( "Button_1 onLostFocus" )
@ 240,10 COMBOBOX Combo_1 ;
WIDTH 100 ;
ITEMS { '1 | Uno' , '2 | Dos' , '3 | tres' } ;
VALUE 1 ;
ON GOTFOCUS addlog ( "Combo_1 onGotFocus" ) ;
ON LOSTFOCUS addlog ( "Combo_1 onLostFocus" ) ;
ON ENTER addlog ( "Combo_1 onEnter" )
@ 270,10 EDITBOX Edit_1 ;
WIDTH 200 ;
HEIGHT 80 ;
VALUE 'EditBox!!' ;
TOOLTIP 'EditBox' ;
MAXLENGTH 255 ;
ON GOTFOCUS addlog ( "EditBox_1 onGotFocus" ) ;
ON LOSTFOCUS addlog ( "EditBox_1 onLostFocus" ) ;
NOVSCROLL ;
NOHSCROLL
DEFINE TREE Tree_1 AT 360,10 WIDTH 120 HEIGHT 22 ON EXPAND tree_expand() ITEMIDS
ON GOTFOCUS addlog ( "Tree_1 onGotFocus" )
ON LOSTFOCUS addlog ( "Tree_1 onLostFocus" )
NODE 'Expand Grid' ID 1
END NODE
END TREE
Form_1.Tree_1.Cargo ( 1 ) := { | nTreeValue | ShowGrid( nTreeValue ) }
Form_1.Tree_1.HasButton ( 1 ) := .T.
HMG_ChangeWindowStyle(Form_1.Tree_1.Handle, Nil, 512, .T.,.T.)
@ 10,370 RICHEDITBOX RichEditBox_1 ;
HEIGHT 420 ;
WIDTH 250 ;
VALUE "" ;
BACKCOLOR aStdColor ;
ON GOTFOCUS addlog ( "RichEditBox_1 onGotFocus" ) ;
ON LOSTFOCUS addlog ( "RichEditBox_1 onLostFocus" ) ;
READONLY ;
NOTABSTOP
END WINDOW
Form_1.Text_1.SetFocus
Form_1.Center
HMG_EnableRadioGroupGotLostFocusEvents( .F. /* lProccedEachRadioButtonEvent */ )
Form_1.Activate
Return Nil
****************************
function tree_expand()
Local xCargo:=Form_1.Tree_1.Cargo ( This.TreeItemValue )
IF xCargo = Nil
RETURN
ENDIF
EVAL( xCargo, This.TreeItemValue )
RETURN
******************************
Function ShowGrid( nTreevalue )
Form_1.Tree_1.Item( nTreevalue ) := "Collapse Grid"
@ 380,30 GRID Grid_1 OF Form_1 ;
WIDTH 320 ;
HEIGHT 170 ;
HEADERS {'Column 1','Column 2','Column 3'} ;
WIDTHS {100,100,100};
ON GOTFOCUS addlog ( "Grid_1 onGotFocus" ) ;
ON LOSTFOCUS addlog ( "Grid_1 onLostFocus" ) ;
VIRTUAL ;
ITEMCOUNT 1000 ;
ON QUERYDATA QueryTest()
Form_1.Tree_1.Cargo ( nTreevalue ) := { | nTreeValue | HideGrid( nTreeValue ) }
Return
*******************************
Procedure QueryTest()
This.QueryData := Str ( This.QueryRowIndex ) + ',' + Str ( This.QueryColIndex )
Return
*********************************
Function HideGrid( nTreevalue )
Form_1.Tree_1.Item( nTreevalue ) := "Expand Grid"
Form_1.Grid_1.Release
Form_1.Tree_1.Cargo ( nTreevalue ) := { | nTreeValue | ShowGrid( nTreeValue ) }
Return
****************************
function addlog ( ctxt )
form_1.RichEditBox_1.addtext ( len( form_1.RichEditBox_1.value) ) := ctxt + crlf
RETURN
***********************************************************
* OnGotFocus / OnLostFocus RadioGroup Event Functions *
***********************************************************
Function HMG_RadioGroupSetEvent ( cForm, cControl, cEventName, bNewProc )
LOCAL nControlIndex := GetControlIndex( cControl, cForm )
LOCAL nEvent := 0
IF .Not. hb_isBlock ( bNewProc )
bNewProc := ""
ENDIF
IF nControlIndex = 0 .OR. _HMG_SYSDATA [ 1 ] [ nControlIndex ] <> "RADIOGROUP"
Return Nil
ENDIF
DO CASE
CASE Upper( cEventName ) = "ONLOSTFOCUS"
nEvent := 10
CASE Upper( cEventName ) = "ONGOTFOCUS"
nEvent := 11
CASE Upper( cEventName ) = "ONCHANGE"
nEvent := 12
ENDCASE
IF nEvent > 0 //store event action
_HMG_SYSDATA [ nEvent ] [ nControlIndex ] := bNewProc
ENDIF
Return Nil
**********************************************************
Function HMG_RadioGroupGotLostFocusEvents( lProccedEachRadioButtonEvent )
#define WM_COMMAND 0x0111
#define BN_KILLFOCUS 0x0007
#define BN_SETFOCUS 0x0006
#define EN_SETFOCUS 0x0100
#define CBN_SETFOCUS 0x0003
#define LBN_SETFOCUS 0x0004
#define NM_SETFOCUS (-0x0007)
Local nMsg := EventMSG ()
Local hWnd := EventHWND ()
Local wParam := EventWPARAM ()
Local lParam := EventLPARAM ()
Local nControlIndex
Static _nLastRadioGroupFocusedIndex := 0
Default lProccedEachRadioButtonEvent := .T.
IF nMsg == WM_COMMAND
nControlIndex := hb_AScan ( _HMG_SYSDATA [ 3 ], { | xHandle | IF ( hb_isArray ( xHandle ), hb_AScan ( xHandle, lParam, , , .T. ) <> 0 , .F. ) } )
IF nControlIndex > 0 .AND. _HMG_SYSDATA [ 1 ] [ nControlIndex ] == "RADIOGROUP"
DO CASE
CASE HIWORD( wParam ) == BN_KILLFOCUS .AND. lProccedEachRadioButtonEvent //Call OnLostFocus action for each radio button
_DoControlEventProcedure( _HMG_SYSDATA [ 10 /* OnLostFocus */ ] [ nControlIndex ], nControlIndex )
RETURN 0
CASE HIWORD( wParam ) == BN_SETFOCUS //Call OnGotFocus action
IF lProccedEachRadioButtonEvent
_DoControlEventProcedure( _HMG_SYSDATA [ 11 /* OnGotFocus */ ] [ nControlIndex ], nControlIndex )
RETURN 0
ELSEIF _nLastRadioGroupFocusedIndex <> nControlIndex //Other radiogroup got focus
IF _nLastRadioGroupFocusedIndex <> 0 //call last radiogroup onLostFocus action
_DoControlEventProcedure( _HMG_SYSDATA [ 10 /* OnLostFocus */ ] [ _nLastRadioGroupFocusedIndex ], _nLastRadioGroupFocusedIndex )
ENDIF
_nLastRadioGroupFocusedIndex := nControlIndex //store current radiogroup
_DoControlEventProcedure( _HMG_SYSDATA [ 11 /* OnGotFocus */ ] [ nControlIndex ], nControlIndex )
RETURN 0
ENDIF
ENDCASE
ELSE
IF .Not. lProccedEachRadioButtonEvent .AND. ;
( HIWORD( wParam ) == BN_SETFOCUS .OR. ;
HIWORD( wParam ) == EN_SETFOCUS .OR. ;
HIWORD( wParam ) == CBN_SETFOCUS .OR. ;
HIWORD( wParam ) == LBN_SETFOCUS ) .AND. ;
_nLastRadioGroupFocusedIndex > 0 //Other control type than radiogroup got focus -> call last radiogroup onLostFocus action
_DoControlEventProcedure( _HMG_SYSDATA [ 10 /* OnLostFocus */ ] [ _nLastRadioGroupFocusedIndex ], _nLastRadioGroupFocusedIndex )
_nLastRadioGroupFocusedIndex := 0
RETURN Nil
ENDIF
ENDIF
ELSEIF nMsg == WM_NOTIFY .AND. .Not. lProccedEachRadioButtonEvent .AND. _nLastRadioGroupFocusedIndex > 0 .AND. GetNotifyCode ( lParam ) = NM_SETFOCUS //Other control type than radiogroup got focus -> call last radiogroup onLostFocus action
_DoControlEventProcedure( _HMG_SYSDATA [ 10 /* OnLostFocus */ ] [ _nLastRadioGroupFocusedIndex ], _nLastRadioGroupFocusedIndex )
_nLastRadioGroupFocusedIndex := 0
ENDIF
RETURN Nil
*************************************************************************************
Function HMG_EnableRadioGroupGotLostFocusEvents( lProccedEachRadioButtonEvent )
Local cProc := "HMG_RadioGroupGotLostFocusEvents( " + hb_ValToExp ( lProccedEachRadioButtonEvent ) + " )"
Local nIndex := EventCreate( cProc, NIL, NIL )
EventProcessHMGWindowsMessage ( nIndex, .T. )
RETURN Nil
Code: Select all
#include "dll.ch"
#xtranslate DllCall => HMG_CallDLL // Lock and Unlock form
* - - - - - - - - - - - - - -
procedure StateEdit_OnInit()
//
local lcFormName := "DataBrowse"
local pnLockwHDL := GetFormHandle( lcFormName )
// Lock parent form "DataBrowse"
DllCall( "user32.dll", DLL_OSAPI, "LockWindowUpdate", pnLockwHDL )
....
return
========================
#include "dll.ch"
#xtranslate DllCall => HMG_CallDLL // Lock and Unlock form
* - - - - - - - - - - - - - -
function StateEdit_OnRelease()
//
local llRetVal := .T.
// Unlock parent form "DataBrowse"
DllCall( "user32.dll", DLL_OSAPI, "LockWindowUpdate", 0 )
....
return llRetVal