Read STRING GADGET text after ENTER key is pushed ...

Just starting out? Need help? Post your questions and find answers here.
marc_256
Enthusiast
Enthusiast
Posts: 749
Joined: Thu May 06, 2010 10:16 am
Location: Belgium
Contact:

Read STRING GADGET text after ENTER key is pushed ...

Post by marc_256 »

Hello,

I need to read the text in a 'STRING GADGET' and only if the ENTER key is pushed,
I found this PB example

Code: Select all

;
; ------------------------------------------------------------
;
;   PureBasic - Gadget example file
;
;    (c) Fantaisie Software
;
; ------------------------------------------------------------
;

#WindowWidth  = 390
#WindowHeight = 350

If OpenWindow(0, 100, 200, #WindowWidth, #WindowHeight, "PureBasic - Gadget Demonstration", #PB_Window_MinimizeGadget)
    
  Top = 10
  GadgetHeight = 24

  FrameGadget(#PB_Any, 10, Top, 370, 290, "Player...") : Top+20

  StringGadget(0,  20, Top, 200, GadgetHeight, "")
  ButtonGadget(1, 223, Top,  72, GadgetHeight, "Play")
  ButtonGadget(2, 295, Top,  72, GadgetHeight, "Stop")  : Top+35
  DisableGadget(2,1)
  
  GadgetToolTip(1,"Play the current song")
  
  PanelGadget(3, 20, Top, #WindowWidth-50, #WindowHeight-Top-60)
    AddGadgetItem(3, 0, "MP3 PlayList")
      ListViewGadget(4, 6, 10, 230, 148)

      For k=0 To 30
        AddGadgetItem(4, -1, "Music Song n° "+Str(k))
      Next

      ButtonGadget(5,  250, 10, 80, GadgetHeight, "Add")
      ButtonGadget(6,  250, 38, 80, GadgetHeight, "Remove")
      ButtonGadget(7,  250, 66, 80, GadgetHeight, "Select")
      GadgetToolTip(7, "Select the current song")
      
      TrackBarGadget(17, 10, 168, 310, 25, 0, 100)

    AddGadgetItem(3, 1, "Options")
      Top = 10
      CheckBoxGadget(10, 10, Top, 250, GadgetHeight, "Enable low-pass filter") : Top+30
      CheckBoxGadget(11, 10, Top, 250, GadgetHeight, "Enable visual plug-in")  : Top+30
      ComboBoxGadget(12, 10, Top, 250, 21) : Top+30
        AddGadgetItem(12, -1, "FireWorks")
        AddGadgetItem(12, -1, "OpenGL spectrum")
        AddGadgetItem(12, -1, "Bump bass")
      SetGadgetState(12,0)
      DisableGadget(12,1)
      
      OptionGadget(13, 10, Top, 80, GadgetHeight, "640*480") : Top+20
      OptionGadget(14, 10, Top, 80, GadgetHeight, "800*600") : Top+20
      OptionGadget(15, 10, Top, 80, GadgetHeight, "1024*768")
      SetGadgetState(13, 1)
      
      ButtonGadget(16, 150, Top, 80, GadgetHeight, "Info")
  CloseGadgetList()

  TextGadget  (9, 10, #WindowHeight-30, 250, 24, "PureBasic - Gadget demonstration")
  ButtonGadget(8, #WindowWidth-100, #WindowHeight-36, 80, 24, "Quit")

  SetGadgetState(3, 0)

  Repeat
    Event = WaitWindowEvent()
    
    If Event = #PB_Event_Gadget

      Select EventGadget()
        Case 0
          If EventType() = #PB_EventType_ReturnKey
            MessageRequester("Info", "Return key pressed", 0)
            SetActiveGadget(0)
          EndIf
          
        Case 1 ; Play
          DisableGadget(2,0)  ; Enable the 'Stop' gadget
          DisableGadget(1,1)  ; Disable the 'Play' Gadget
      
        Case 2 ; Stop
          DisableGadget(1,0)  ; Enable the 'Play' gadget
          DisableGadget(2,1)  ; Disable the 'Stop' Gadget
        
        Case 4
          If EventType() = 2
            SetGadgetText(0, GetGadgetText(4)) ; Get the current item from the ListView..
          EndIf

        Case 5 ; Add
          AddGadgetItem(4, -1, "New Item Added...")

        Case 6 ; Remove
          RemoveGadgetItem(4, GetGadgetState(4)) ; Remove the current element of the ListView

        Case 7 ; Select
          SetGadgetText(0, GetGadgetText(4)) ; Get the current item from the ListView..
  
        Case 8 ; Quit...
          Event = #PB_Event_CloseWindow

        Case 11 ; Enable PlugIn..
          DisableGadget(12, 1-GetGadgetState(11))
          
        Case 16 ;
          If GetGadgetState(13) : Result$ = GetGadgetText(13) : EndIf
          If GetGadgetState(14) : Result$ = GetGadgetText(14) : EndIf
          If GetGadgetState(15) : Result$ = GetGadgetText(15) : EndIf
         
          MessageRequester("Info", "Selected screen mode: "+Result$, 0)
        
        Case 17
          SetGadgetText(0, Str(GetGadgetState(17)))
          
      EndSelect

    EndIf

  Until Event = #PB_Event_CloseWindow

EndIf

End 
But this is not working PB 5.70F x64 - WIN10 Home x64

Code: Select all

      Select EventGadget()
        Case 0
          If EventType() = #PB_EventType_ReturnKey
            MessageRequester("Info", "Return key pressed", 0)
            SetActiveGadget(0)
          EndIf

Can someone help me out here ?

thanks,
marc
Last edited by marc_256 on Sat Oct 19, 2019 12:21 am, edited 1 time in total.
- every professional was once an amateur - greetings from Pajottenland - Belgium -
PS: sorry for my english I speak flemish ...
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4664
Joined: Sun Apr 12, 2009 6:27 am

Re: Read STRING GADGET text after ENTER key is pushed ...

Post by RASHAD »

Change to :

Code: Select all

StringGadget(0,  20, Top, 200, GadgetHeight, "",#ES_MULTILINE)
OR

Code: Select all

  AddKeyboardShortcut(0, #PB_Shortcut_Return, 15)

  Repeat
    Event = WaitWindowEvent()
   
    If Event = #PB_Event_Gadget

      Select EventGadget()
        Case 0
        .
        .
        .
  
      EndSelect
 
    ElseIf #PB_Event_Menu; 
      Select EventMenu()
        Case 15
           If GetActiveGadget() = 0
             MessageRequester("Info", "Return key pressed", 0)
           EndIf
         EndSelect

    EndIf

  Until Event = #PB_Event_CloseWindow
Egypt my love
marc_256
Enthusiast
Enthusiast
Posts: 749
Joined: Thu May 06, 2010 10:16 am
Location: Belgium
Contact:

Re: Read STRING GADGET text after ENTER key is pushed ...

Post by marc_256 »

Hi RASHAD,

Code: Select all

StringGadget(0,  20, Top, 200, GadgetHeight, "",#ES_MULTILINE)
thanks to help me with this problem,
works very well, with the #ES_MULTILINE.
the only thing is, that I get a sound (beep) every time I push the enter key ??

You are the best,
thanks,
marc
- every professional was once an amateur - greetings from Pajottenland - Belgium -
PS: sorry for my english I speak flemish ...
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Read STRING GADGET text after ENTER key is pushed ...

Post by netmaestro »

One way to remove the beep: viewtopic.php?f=12&t=3802
BERESHEIT
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Read STRING GADGET text after ENTER key is pushed ...

Post by mk-soft »

Simply way...

Code: Select all

;-TOP

Procedure EventReturnKey()
  PostEvent(#PB_Event_Gadget, GetActiveWindow(), GetActiveGadget(), #PB_EventType_ReturnKey)
EndProcedure


If OpenWindow(0, 0, 0, 400, 300, "StringGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  StringGadget(0, 10, 10, 380, 30, "StringGadget...")
  EditorGadget(1, 10, 50, 380, 240)
  
  CreateMenu(0, WindowID(0))
  BindMenuEvent(0, 1000, @EventReturnKey())
  
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Break
        
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 0
            Select EventType()
              Case #PB_EventType_Focus
                AddKeyboardShortcut(0, #PB_Shortcut_Return, 1000)
              Case #PB_EventType_LostFocus
                RemoveKeyboardShortcut(0, #PB_Shortcut_Return)
              Case #PB_EventType_ReturnKey
                Debug "Return"
                SetActiveGadget(1)
            EndSelect
                
        EndSelect
        
    EndSelect
  ForEver
  
EndIf
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Read STRING GADGET text after ENTER key is pushed ...

Post by mk-soft »

With more than one StringGadget...

Update

Code: Select all

;-TOP

; ----

#MenuEvent_ReturnKey = 1000

Procedure DoEventGadgetType()
  Protected gadget = EventGadget()
  If IsGadget(gadget)
    Select GadgetType(gadget)
      Case #PB_GadgetType_String
        Select EventType()
          Case #PB_EventType_Focus
            AddKeyboardShortcut(0, #PB_Shortcut_Return, #MenuEvent_ReturnKey)
          Case #PB_EventType_LostFocus
            RemoveKeyboardShortcut(0, #PB_Shortcut_Return)
        EndSelect
    EndSelect
  EndIf
EndProcedure

Procedure DoEventReturnKey()
  PostEvent(#PB_Event_Gadget, GetActiveWindow(), GetActiveGadget(), #PB_EventType_ReturnKey)
EndProcedure

; ----

If OpenWindow(0, 0, 0, 400, 300, "StringGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  StringGadget(0, 10, 10, 380, 30, "StringGadget 1")
  StringGadget(1, 10, 50, 380, 30, "StringGadget 2")
  EditorGadget(2, 10, 90, 380, 190)
  
  CreateMenu(0, WindowID(0))
  BindEvent(#PB_Event_Gadget, @DoEventGadgetType())
  BindMenuEvent(0, #MenuEvent_ReturnKey, @DoEventReturnKey())
  
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Break
        
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 0
            Select EventType()
              Case #PB_EventType_ReturnKey
                Debug "Return Gadget 0"
                SetActiveGadget(1)
            EndSelect
            
          Case 1
            Select EventType()
              Case #PB_EventType_ReturnKey
                Debug "Return Gadget 1"
                SetActiveGadget(2)
            EndSelect
            
        EndSelect
        
    EndSelect
  ForEver
  
EndIf
Last edited by mk-soft on Sat Oct 19, 2019 9:30 am, edited 1 time in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4664
Joined: Sun Apr 12, 2009 6:27 am

Re: Read STRING GADGET text after ENTER key is pushed ...

Post by RASHAD »

Glad to read your snippet NM even if it is from while to while :)
Egypt my love
marc_256
Enthusiast
Enthusiast
Posts: 749
Joined: Thu May 06, 2010 10:16 am
Location: Belgium
Contact:

Re: Read STRING GADGET text after ENTER key is pushed ...

Post by marc_256 »

Hi,

I tried the following stuff,

#PB_String_Multiline|#ES_AUTOVSCROLL

Code: Select all

StringGadget(1,20,20,100,21,"",#PB_String_Multiline|#ES_AUTOVSCROLL)
but PB tells me that "#PB_String_Multiline" is not a PB constant ...

Code: Select all

StringGadget(1,20,20,100,21,"1234567890",#ES_MULTILINE|#ES_AUTOVSCROLL)
with "#ES_MULTILINE" it works well


thanks,
marc
- every professional was once an amateur - greetings from Pajottenland - Belgium -
PS: sorry for my english I speak flemish ...
marc_256
Enthusiast
Enthusiast
Posts: 749
Joined: Thu May 06, 2010 10:16 am
Location: Belgium
Contact:

Re: Read STRING GADGET text after ENTER key is pushed ...

Post by marc_256 »

I had have also a problem with auto scroll editor gadget,
after adding a line in the editor gadget ...

so I used this stuff,

Code: Select all

    AddGadgetItem(0, -1, outputstr)
    SetActiveGadget(0)
    SetActiveGadget(1)
and works very well.

marc
- every professional was once an amateur - greetings from Pajottenland - Belgium -
PS: sorry for my english I speak flemish ...
ludoke
Enthusiast
Enthusiast
Posts: 153
Joined: Fri Jul 08, 2016 5:35 pm
Location: Essen (Belgium)

Re: Read STRING GADGET text after ENTER key is pushed ...

Post by ludoke »

I have made some minor adjustments so that it is a little clearer to me.
; ----

Code: Select all

Enumeration
  #main_win:#str1:#str2:#edit1  ;I prefer names instead of numbers 
 EndEnumeration 
#MenuEvent_ReturnKey = 1000
Global temp$
Procedure DoEventGadgetType()
  Protected gadget = EventGadget()
  If IsGadget(gadget)
    Select GadgetType(gadget)
      Case #PB_GadgetType_String
        Select EventType()
          Case #PB_EventType_Focus
            SetGadgetText(gadget,"0.0")  ;set predefined text
            AddKeyboardShortcut(#main_win, #PB_Shortcut_Return, #MenuEvent_ReturnKey)
            SetGadgetColor(gadget, #PB_Gadget_BackColor,$E0E292);set color blue
          Case #PB_EventType_LostFocus
            RemoveKeyboardShortcut(#main_win, #PB_Shortcut_Return)
             SetGadgetColor(gadget, #PB_Gadget_BackColor,$FFFFFF);reset to white 
        EndSelect
    EndSelect
  EndIf
EndProcedure

Procedure DoEventReturnKey()
  PostEvent(#PB_Event_Gadget, GetActiveWindow(), GetActiveGadget(), #PB_EventType_ReturnKey)
EndProcedure

; ----

If OpenWindow(#main_win, 0, 0, 400, 300, "StringGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  StringGadget(#str1, 10, 10, 380, 30, "StringGadget 1")
  StringGadget(#str2, 10, 50, 380, 30, "StringGadget 2")
  EditorGadget(#edit1, 10, 90, 380, 190)
 
  CreateMenu(#main_win, WindowID(#main_win))
  BindEvent(#PB_Event_Gadget, @DoEventGadgetType())
  BindMenuEvent(#main_win, #MenuEvent_ReturnKey, @DoEventReturnKey())
 
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Break
       
      Case #PB_Event_Gadget
        Select EventGadget()
          
          Case #str1 
            Select EventType()
              Case #PB_EventType_ReturnKey
                  temp$=GetGadgetText(#str1)
                  Debug "str1="+temp$        
                 SetActiveGadget(#edit1)
            EndSelect
           
          Case #str2 
    
            Select EventType()
              Case #PB_EventType_ReturnKey 
                 temp$=GetGadgetText(#str2)
                  Debug "str2="+temp$                       
                SetActiveGadget(#edit1)
            EndSelect
           
        EndSelect
       
    EndSelect
  ForEver
 
EndIf
User avatar
Lord
Addict
Addict
Posts: 849
Joined: Tue May 26, 2009 2:11 pm

Re: Read STRING GADGET text after ENTER key is pushed ...

Post by Lord »

I like to work with BindGadgetEvent().
So, here is my approach:

Code: Select all

EnableExplicit

Enumeration
  #Win1  
EndEnumeration
Enumeration
  #SG1
  #SG2
  #SG3
  #EG1
EndEnumeration
Enumeration
  #Men1  
EndEnumeration

Define.i i

Procedure EventReturnKey()
  PostEvent(#PB_Event_Gadget, GetActiveWindow(), GetActiveGadget(), #PB_EventType_ReturnKey)
EndProcedure
Procedure ManageKeyboardShortcut()
  Protected EventGadget
  EventGadget=EventGadget()
  Select EventGadget
    Case #SG1 To #SG3
      Select EventType()
        Case #PB_EventType_Focus
          AddKeyboardShortcut(#Win1, #PB_Shortcut_Return, #Men1)
        Case #PB_EventType_LostFocus
          RemoveKeyboardShortcut(#Win1, #PB_Shortcut_Return)
        Case #PB_EventType_ReturnKey
          Debug "Return"
          AddGadgetItem(#EG1, -1, GetGadgetText(EventGadget))
          ; SetActiveGadget(-1); no Gadget has focus
          ; alternativ: goto next StringGadget
          EventGadget+1
          SetActiveGadget(EventGadget)
          ; only Windows: cursor at end of line
          CompilerIf #PB_Compiler_OS=#PB_OS_Windows
            SendMessage_(GadgetID(EventGadget), #EM_SETSEL, $0FFFFFFF, $0FFFFFFF)
          CompilerEndIf
      EndSelect
  EndSelect
EndProcedure

If OpenWindow(#Win1, 0, 0, 400, 300, "StringGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  StringGadget(#SG1, 10, 10, 380, 30, "StringGadget1...")
  StringGadget(#SG2, 10, 50, 380, 30, "StringGadget2...")
  StringGadget(#SG3, 10, 90, 380, 30, "StringGadget3...")
  EditorGadget(#EG1, 10, 130, 380, 160)
  
  If CreateMenu(0, WindowID(#Win1))
    
    BindMenuEvent(#Win1, #Men1, @EventReturnKey())
    
    BindGadgetEvent(#SG1, @ManageKeyboardShortcut())
    BindGadgetEvent(#SG2, @ManageKeyboardShortcut())
    BindGadgetEvent(#SG3, @ManageKeyboardShortcut())
    
    ; If there are a lot of StringGadgets to bind, a loop would work here too,
    ; as long as StringGadgets are continous numbered::
    ;   For i=#SG1 To #SG3
    ;     BindGadgetEvent(i, @ManageKeyboardShortcut())
    ;   Next
    
  EndIf
  
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Break
    EndSelect
  ForEver
 
EndIf
Image
ludoke
Enthusiast
Enthusiast
Posts: 153
Joined: Fri Jul 08, 2016 5:35 pm
Location: Essen (Belgium)

Re: Read STRING GADGET text after ENTER key is pushed ...

Post by ludoke »

@lord,
that's what i love,short examples to learn.
I still struggle with the many possibilities of PB.
A book with more explanation and many examples would that exist?

I think there is an error in my code
CreateMenu(#main_win, WindowID(#main_win)) ,is #main_win correct?

maybe i should replace #main_win with #edit

CreateMenu(#menu, WindowID(#main_win)) ; I think I should use a different enumeration ?

Or will PB give this a different number because it is a different gadget?
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Read STRING GADGET text after ENTER key is pushed ...

Post by mk-soft »

There are several separate object numbering systems.
- Window
- Menu bar
- Gadget
- Images
- etc

Show Purebasic Help 'Purebasic objects overview'
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply