Using Select..Case..Endselect

Just starting out? Need help? Post your questions and find answers here.
matalog
Enthusiast
Enthusiast
Posts: 165
Joined: Tue Sep 05, 2017 10:07 am

Using Select..Case..Endselect

Post by matalog »

Select Case EndSelect are completely new to me.

I have read what the reference has to offer on them and it doesn't mean very much to me given my lack of knowledge.

I have been trying to learn from experience, to get an action to occur during a leftclick for example.

I have this, code, that some others were adding to, but I can use it to highlight one of my problems.

I can't see why

Code: Select all

Select EventType()
   Case #PB_EventType_LeftClick       : Debug "Click with left mouse button"
 EndSelect
Is returning a left click so often in this, the mouse is not clicking, but yet the

Code: Select all

Case #PB_EventType_LeftClick
is going ahead with the

Code: Select all

: Debug "Click with left mouse button"
.

Is there a way to give a unique Case number to #PB_EventType_LeftClick, like the

Code: Select all

AddKeyboardShortcut(0,#PB_Shortcut_Space,10)
AddKeyboardShortcut(0,#PB_Shortcut_Add,20)
have?

Here is the full code i'm talking about.

Code: Select all

EnableExplicit

Enumeration 
  #InputString
  #ConvertButton
  #ResultString
  #InputText 
  #ResultText
  #TextGadget
EndEnumeration

Define Event.i, Exit.i, Input$

OpenWindow(0, 0, 0, 220, 100, "Inch To cm", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
InitKeyboard()
TextGadget(#InputText, 10, 10, 60, 20, "Input")
TextGadget(#ResultText, 150, 10, 60, 20, "Result")
StringGadget(#InputString, 10, 40, 60, 20, "0" ,#PB_String_Numeric)
ButtonGadget(#ConvertButton, 80, 40, 60, 20, "Convert")
StringGadget(#ResultString, 150, 40, 60, 20, "0")

TextGadget(#TextGadget,10,70,200,20,"Gadget : "+Str(GetActiveGadget())+" has the focus")
AddKeyboardShortcut(0,#PB_Shortcut_Space,10)
AddKeyboardShortcut(0,#PB_Shortcut_Add,20)
AddWindowTimer(0,10,100)
Repeat
  Event = WaitWindowEvent()
 Select EventType()
   Case #PB_EventType_LeftClick        : Debug "Click with left mouse button"
 EndSelect
 
  Select Event
    Case #PB_Event_Timer
      SetGadgetText(#TextGadget,"Gadget : "+Str(GetActiveGadget())+" has the focus")     
     
   Case #PB_Event_Menu
      Select EventMenu()
        Case 10
          If GetActiveGadget() = #InputString
            SetGadgetText(#ResultString, StrF(ValD(GetGadgetText(#InputString)) * 2.54, 3))
          EndIf
         
        Case 20
          If GetActiveGadget() = #ResultString
            SetActiveGadget(#InputString)
          Else
            SetActiveGadget(GetActiveGadget()+1)
          EndIf
         
      EndSelect
     
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #InputString
          Input$ = GetGadgetText(#InputString)
          If Input$ = ""
            DisableGadget(#ConvertButton, #True)
          Else
            DisableGadget(#ConvertButton, #False)
          EndIf
        Case #ConvertButton
          SetGadgetText(#ResultString, StrF(ValD(GetGadgetText(#InputString)) * 2.54, 3))
      EndSelect
     
    Case #PB_Event_CloseWindow
      Exit = #True
  EndSelect
 
Until Exit
infratec
Always Here
Always Here
Posts: 6874
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Using Select..Case..Endselect

Post by infratec »

You still make the same fault:

You are using code at the wrong place.

You can/should only use EventType() when you reached the section of the gadget you want to test.
And remember: not all gadgets rises a #PB_EventType_LeftClick

Code: Select all

Select Event()
  Case #PB_Event_Gadget
    Select EventGadget()
      Case #CanvasGadget
        Select EventType()
          Case #PB_EventType_LeftClick
A StringGadget or a ButtonGadget doesn't generate #PB_EventType_LeftClick.
See the help. The possible EventTypes are written in the help to the gadget.

A StringGadget rises #PB_EventType_Focus which is nearly identical.
A #PB_EventType_LeftClick on a button makes no sense, because this is the (only) Event of this gadget.
User avatar
spikey
Enthusiast
Enthusiast
Posts: 586
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: Using Select..Case..Endselect

Post by spikey »

You can think of Select/Case as being a stack of multiple 'If x = y then' where 'x' always has the same value and only 'y' changes.

Code: Select all

Select Event   
    Case #PB_Event_Timer  ; If Event = #PB_Event_Timer
    Case #PB_Event_Menu  ; If Event = #PB_Event_Menu
    ...
EndSelect
The reason you are seeing odd results is because #PB_EventType_LeftClick has an underlying value of zero. However EventType() will also be zero when it is undefined. It is undefined, for example, when a timer event occurs, this must be distinguished by using EventTimer(); or a shortcut or menu event which must be distinguished using EventMenu().

You need to do something like this to get the result you are expecting:

Code: Select all

EnableExplicit

Enumeration
  #InputString
  #ConvertButton
  #ResultString
  #InputText
  #ResultText
  #TextGadget
EndEnumeration

Define Event.i, Exit.i, Input$

OpenWindow(0, 0, 0, 220, 100, "Inch To cm", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
InitKeyboard()
TextGadget(#InputText, 10, 10, 60, 20, "Input")
TextGadget(#ResultText, 150, 10, 60, 20, "Result")
StringGadget(#InputString, 10, 40, 60, 20, "0" ,#PB_String_Numeric)
ButtonGadget(#ConvertButton, 80, 40, 60, 20, "Convert")
StringGadget(#ResultString, 150, 40, 60, 20, "0")

TextGadget(#TextGadget,10,70,200,20,"Gadget : "+Str(GetActiveGadget())+" has the focus")
AddKeyboardShortcut(0,#PB_Shortcut_Space,10)
AddKeyboardShortcut(0,#PB_Shortcut_Add,20)
AddWindowTimer(0,10,100)
Repeat
  Event = WaitWindowEvent()
 
  Select Event
    Case #PB_Event_Timer
      SetGadgetText(#TextGadget,"Gadget : "+Str(GetActiveGadget())+" has the focus")     
     
   Case #PB_Event_Menu
      Select EventMenu()
        Case 10
          If GetActiveGadget() = #InputString
            SetGadgetText(#ResultString, StrF(ValD(GetGadgetText(#InputString)) * 2.54, 3))
          EndIf
         
        Case 20
          If GetActiveGadget() = #ResultString
            SetActiveGadget(#InputString)
          Else
            SetActiveGadget(GetActiveGadget()+1)
          EndIf
         
      EndSelect
     
    Case #PB_Event_Gadget
      Select EventType()
        Case #PB_EventType_LeftClick        : Debug "Click with left mouse button"
      EndSelect
      
      Select EventGadget()
        Case #InputString
          Input$ = GetGadgetText(#InputString)
          If Input$ = ""
            DisableGadget(#ConvertButton, #True)
          Else
            DisableGadget(#ConvertButton, #False)
          EndIf
        Case #ConvertButton
          SetGadgetText(#ResultString, StrF(ValD(GetGadgetText(#InputString)) * 2.54, 3))
      EndSelect
     
    Case #PB_Event_CloseWindow
      Exit = #True
  EndSelect
 
Until Exit
In practice however you may want to be even more selective by distinguishing EventType() after distinguishing on EventGadget(), so that you can implement precise code for each of your gadgets.
matalog
Enthusiast
Enthusiast
Posts: 165
Joined: Tue Sep 05, 2017 10:07 am

Re: Using Select..Case..Endselect

Post by matalog »

I read https://www.purebasic.com/documentation ... event.html and concluded that I should be able to read a left-click with #PB_EventType_LeftClick anywhere on the window. Not necessarily on a gadget. Should I be able to?
matalog
Enthusiast
Enthusiast
Posts: 165
Joined: Tue Sep 05, 2017 10:07 am

Re: Using Select..Case..Endselect

Post by matalog »

I mean, to achieve something like this, only with Left Click really, is it necessary to make some sort of gadget? A Window Event cannot be used?

Code: Select all

If OpenWindow(0, 0, 0, 230, 120, "Eventtypes example...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
     CanvasGadget(132, 0, 0, 230, 120) 

     Repeat
       Event = WaitWindowEvent()
       
       Select Event
        
    
             Case #PB_Event_Gadget  
             ;  Select EventWindow
                   Select EventGadget()
             Case 132 
               Select EventType()
               Case   #PB_EventType_MouseEnter      : Debug "The mouse cursor entered the gadget"
                Case    #PB_EventType_MouseLeave      : Debug "The mouse cursor left the gadget"
                ;Case    #PB_EventType_MouseMove       : Debug "The mouse cursor moved"
                Case    #PB_EventType_MouseWheel      : Debug "The mouse wheel was moved"
                Case    #PB_EventType_LeftButtonDown  : Debug "The left mouse button was pressed"
                Case    #PB_EventType_LeftButtonUp    : Debug "The left mouse button was released"
                Case    #PB_EventType_LeftClick       : Debug "A click With the left mouse button"
                Case    #PB_EventType_LeftDoubleClick : Debug "A double-click With the left mouse button"
                Case    #PB_EventType_RightButtonDown : Debug "The right mouse button was pressed"
                Case    #PB_EventType_RightButtonUp   : Debug "The right mouse button was released"
                Case    #PB_EventType_RightClick      : Debug "A click With the right mouse button"
                Case    #PB_EventType_RightDoubleClick: Debug "A double-click With the right mouse button"
                Case    #PB_EventType_MiddleButtonDown: Debug "The middle mouse button was pressed"
                 Case   #PB_EventType_MiddleButtonUp  : Debug "The middle mouse button was released"

               EndSelect
        
       EndSelect
EndSelect
     Until Event = #PB_Event_CloseWindow
  EndIf
infratec
Always Here
Always Here
Posts: 6874
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Using Select..Case..Endselect

Post by infratec »

matalog wrote:I read https://www.purebasic.com/documentation ... event.html and concluded that I should be able to read a left-click with #PB_EventType_LeftClick anywhere on the window. Not necessarily on a gadget. Should I be able to?
If you read this help correct, then you will see that there is

Code: Select all

#PB_Event_LeftClick
And not

Code: Select all

#PB_EventType_LeftClick
You are using:

Code: Select all

Repeat
  Event = WaitWindowEvent()
 Select EventType()
   Case #PB_EventType_LeftClick        : Debug "Click with left mouse button"
 EndSelect
Which is wrong.
matalog
Enthusiast
Enthusiast
Posts: 165
Joined: Tue Sep 05, 2017 10:07 am

Re: Using Select..Case..Endselect

Post by matalog »

I had to read that twice to even notice the difference in them. Thanks, I get it now. There is not a lot of difference in them to be honest.
Post Reply