Page 1 of 1

Gadget() Lost focus

Posted: Mon Dec 02, 2024 9:13 pm
by Lima
This code block illustrates the situation in which the user removes focus from gadget(0) by placing the cursor in another getgad(), or uses Enter to advance to the next field.

In both situations it is necessary to ensure that the content of gadget(0) is correct.

When the content of gadget(0) is not correct, an informative message should be displayed.

Proposal:

1 Run the program.

2 Place the cursor in gadget(0).

3 Validate with Enter.

4 Analyze the result.

I assume that, as it is, it gives an inconvenient result.

Is it possible to validate the content of gadget(0), display a message to the user and continue execution normally?

Note: If the message is not displayed, there is no problem.

I appreciate all possible help.

Code: Select all


#ReturnKey=1

     Procedure     FocusLost(K)
      Debug "gadget(0) LostFocus  "+Str(K)
       If GetGadgetText(0)="Input  0"
         MessageRequester("","Wrong data")
        SetActiveGadget(0)
       Else
         Debug "Correct data"
;                MessageRequester("","Correct data")
         SetActiveGadget(1)
       EndIf
    EndProcedure

      Procedure     FocusGet(N)
        Debug "gadget(0) GetFocus  "+Str(N)
    EndProcedure


If OpenWindow(0, 0, 0, 430, 490, "Event handling example...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)


   StringGadget(0,10,10,100,25,"Input  0")
   StringGadget(1,10,40,100,25,"Input  1")
   
    StringGadget(2,10,80,100,25,"Input  2")
   
   
   
   AddKeyboardShortcut(0, #PB_Shortcut_Return, #ReturnKey)
   
   If CreateMenu(0, WindowID(0))
     MenuTitle("Menu")
     MenuItem(1, "Item 1")
     MenuItem(2, "Item 2")
     MenuItem(3, "Item 3")
   EndIf

   Repeat
     Event = WaitWindowEvent()
     
     Select Event
     
       Case #PB_Event_Gadget
         Select EventGadget()
           Case 0
              Select EventType()
                  Case #PB_EventType_LostFocus
                   K+1
                        FocusLost(K)

                       Case #PB_EventType_Focus
                         N+1
                        FocusGet(N)
 
               EndSelect
             
           
         EndSelect
       
       Case #PB_Event_Menu
         Select EventMenu()
           Case #ReturnKey
               K+1
               FocusLost(K)
             
           Case 2 : Debug "Menu item 2 clicked!"
           Case 3 : Debug "Menu item 3 clicked!"
         EndSelect
     
     EndSelect
   Until Event = #PB_Event_CloseWindow
 EndIf


Re: Gadget() Lost focus

Posted: Tue Dec 03, 2024 5:38 am
by mestnyi

Code: Select all

#ReturnKey=1

Procedure     FocusLost(K)
   Debug "gadget(0) LostFocus  "+Str(K)
   If GetGadgetText(0)="Input  0"
      MessageRequester("","Wrong data")
      ;While WaitWindowEvent( ) : Wend  ; 
      ;
      If EventData()
         SetActiveGadget(0)
      EndIf
   Else
      Debug "Correct data"
      ;                MessageRequester("","Correct data")
      ;SetActiveGadget(1)
   EndIf
EndProcedure

Procedure     FocusGet(N)
   Debug "gadget(0) GetFocus  "+Str(N)
EndProcedure


If OpenWindow(0, 0, 0, 430, 490, "Event handling example...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
   
   
   StringGadget(0,10,10,100,25,"Input  0")
   StringGadget(1,10,40,100,25,"Input  1")
   
   StringGadget(2,10,80,100,25,"Input  2")
   
   
   
   AddKeyboardShortcut(0, #PB_Shortcut_Return, #ReturnKey)
   
   If CreateMenu(0, WindowID(0))
      MenuTitle("Menu")
      MenuItem(1, "Item 1")
      MenuItem(2, "Item 2")
      MenuItem(3, "Item 3")
   EndIf
   
   Repeat
      Event = WaitWindowEvent()
      
      Select Event
            
         Case #PB_Event_Gadget
            Select EventGadget()
               Case 0
                  Select EventType()
                     Case #PB_EventType_LostFocus
                        Debug 777
                        K+1
                        FocusLost(K)
                        
                     Case #PB_EventType_Focus
                        N+1
                        FocusGet(N)
                        
                  EndSelect
                  
                  
            EndSelect
            
         Case #PB_Event_Menu
            Select EventMenu()
               Case #ReturnKey
                  K+1
                  ;FocusLost(K)
                  PostEvent(#PB_Event_Gadget, EventWindow(), GetActiveGadget(), #PB_EventType_LostFocus, GadgetID(GetActiveGadget()))
                  SetActiveGadget(-1)
                  
               Case 2 : Debug "Menu item 2 clicked!"
               Case 3 : Debug "Menu item 3 clicked!"
            EndSelect
            
      EndSelect
   Until Event = #PB_Event_CloseWindow
EndIf


Re: Gadget() Lost focus

Posted: Tue Dec 03, 2024 3:03 pm
by Lima
Thank you very much mestnyi for your help.

I see that the only correction that seems to be necessary is the one I am proposing now. I did not find any errors in the tests I carried out.

Thank you very much again.

Code: Select all


#ReturnKey=1

     Procedure     FocusLost(K)
      Debug "gadget(0) LostFocus  "+Str(K)
       If GetGadgetText(0)="Input  0"
         MessageRequester("","Wrong data")
        SetActiveGadget(0)
       Else
         Debug "Correct data"
;                MessageRequester("","Correct data")
         SetActiveGadget(1)
       EndIf
    EndProcedure

      Procedure     FocusGet(N)
        Debug "gadget(0) GetFocus  "+Str(N)
    EndProcedure


If OpenWindow(0, 0, 0, 430, 490, "Event handling example...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)


   StringGadget(0,10,10,100,25,"Input  0")
   StringGadget(1,10,40,100,25,"Input  1")
   
    StringGadget(2,10,80,100,25,"Input  2")
   
   
   
   AddKeyboardShortcut(0, #PB_Shortcut_Return, #ReturnKey)
   
   If CreateMenu(0, WindowID(0))
     MenuTitle("Menu")
     MenuItem(1, "Item 1")
     MenuItem(2, "Item 2")
     MenuItem(3, "Item 3")
   EndIf

   Repeat
     Event = WaitWindowEvent()
     
     Select Event
     
       Case #PB_Event_Gadget
         Select EventGadget()
           Case 0
              Select EventType()
                  Case #PB_EventType_LostFocus
                   K+1
                        FocusLost(K)

                       Case #PB_EventType_Focus
                         N+1
                        FocusGet(N)
 
               EndSelect
             
           
         EndSelect
       
       Case #PB_Event_Menu
         Select EventMenu()
           Case #ReturnKey
                     SetActiveGadget(-1)

;                K+1
;                FocusLost(K)
             
           Case 2 : Debug "Menu item 2 clicked!"
           Case 3 : Debug "Menu item 3 clicked!"
         EndSelect
     
     EndSelect
   Until Event = #PB_Event_CloseWindow
 EndIf

Re: Gadget() Lost focus

Posted: Tue Dec 03, 2024 3:16 pm
by mestnyi
Lima wrote: Tue Dec 03, 2024 3:03 pm Thank you very much mestnyi for your help.

I see that the only correction that seems to be necessary is the one I am proposing now. I did not find any errors in the tests I carried out.

Thank you very much again.
I'm glad it helped you. in your example, if you choose the second gadget, then there are problems)) :)

Re: Gadget() Lost focus

Posted: Tue Dec 03, 2024 3:52 pm
by TI-994A
Lima wrote: Mon Dec 02, 2024 9:13 pmIs it possible to validate the content of gadget(0), display a message to the user and continue execution normally?
Having to dismiss a message box every time an input is incorrect is quite disruptive to the workflow. A better approach would be to indicate the error somewhere around the input box, to notify the user of the error, which could be tended to eventually. Here's a simple example:

Code: Select all

#ReturnKey = 1

Procedure FocusLost(K)
  Debug "gadget(0) LostFocus  " + Str(K)
  If GetGadgetText(0) = "Input  0"
    HideGadget(GetGadgetData(0), #False)
  Else
    HideGadget(GetGadgetData(0), #True)
  EndIf
EndProcedure

Procedure FocusGet(N)
  Debug "gadget(0) GetFocus  " + Str(N)
EndProcedure


If OpenWindow(0, 0, 0, 430, 490, "Event handling example...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  StringGadget(0, 10, 10, 100, 25,"Input  0")    
  
  LoadFont(0, "Arial", 11)
  TextGadget(3, 10, 36, 150, 20, "value not valid!")
  SetGadgetFont(3, FontID(0))
  SetGadgetColor(3, #PB_Gadget_FrontColor, #Red)
  HideGadget(3, #True)
  SetGadgetData(0, 3)
  
  StringGadget(1,10,55,100,25,"Input  1")  
  StringGadget(2,10,100,100,25,"Input  2")
  
  AddKeyboardShortcut(0, #PB_Shortcut_Return, #ReturnKey)
  
  If CreateMenu(0, WindowID(0))
    MenuTitle("Menu")
    MenuItem(1, "Item 1")
    MenuItem(2, "Item 2")
    MenuItem(3, "Item 3")
  EndIf
  
  Repeat
    Event = WaitWindowEvent()
    
    Select Event
        
      Case #PB_Event_Gadget
        Select EventGadget()
            
          Case 0
            
            Select EventType()
                
              Case #PB_EventType_LostFocus
                K+1
                FocusLost(K)                
                
              Case #PB_EventType_Focus
                N+1
                FocusGet(N)                
                
            EndSelect                      
            
        EndSelect
        
      Case #PB_Event_Menu
        Select EventMenu()
            
          Case #ReturnKey
            K + 1
            FocusLost(K)            
            
          Case 2 : Debug "Menu item 2 clicked!"
            
          Case 3 : Debug "Menu item 3 clicked!"
            
        EndSelect 
        
    EndSelect
    
  Until Event = #PB_Event_CloseWindow  
EndIf

Re: Gadget() Lost focus

Posted: Wed Dec 04, 2024 9:43 pm
by Lima
Thank you very much TI-994A.
It's an interesting solution that I hadn't thought of and that could be useful in some situations.