regain #Gadget value from string ?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

regain #Gadget value from string ?

Post by Vera »

Hello,

I'm struggling for hours but I simply can't find the answer how to get hold of a #Gadget ID-value by it's stringname? (Sorry for this curious expression but I also miss the words.)

I would like to process a row of gadgets within a loop and therefore need to replace the gadget-IDs by a further variable. The string notations only differ by their numbering to easily change them. Then I'm stuck not knowing how to grab the value adressed by the new string notation.

Code: Select all

 Win_0 = OpenWindow(#PB_Any, 0, 0, 100, 100, "hui",  #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_TitleBar )
   Str_0 = StringGadget(#PB_Any, 10, 10, 20, 24, "1")
   Str_1 = StringGadget(#PB_Any, 40, 10, 20, 24, "2")
   Str_2 = StringGadget(#PB_Any, 70, 10, 20, 24, "3")

For i= 0 To 2
  strX.s = "Str_" + Str(i)
 ; strY = ; ???(strX)                    ;??? how to get the ID-value of Str_i
 ; Debug Str_0  
  Debug strX
  Debug strY
; SetGadgetColor(strY, #PB_Gadget_BackColor, $00ccff)
Next i

Repeat  : Until WaitWindowEvent() = #PB_Event_CloseWindow
greetings ~ Vera
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: regain #Gadget value from string ?

Post by RASHAD »

Hi Vera
Is that what you are asking for ?
I hope so

Code: Select all

Global Dim Anyid(4)

Win_0 = OpenWindow(#PB_Any, 0, 0, 100, 100, "hui",  #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_TitleBar )
   Anyid(0) = StringGadget(#PB_Any, 10, 10, 20, 24, "1")
   Anyid(1) = StringGadget(#PB_Any, 40, 10, 20, 24, "2")
   Anyid(2) = StringGadget(#PB_Any, 70, 10, 20, 24, "3")

For i= 0 To 2
  strX.s = "Str_" + Str(i)
; strY = ; ???(strX)                    ;??? how to get the ID-value of Str_i
; Debug Str_0 
  Debug strX
  Debug Anyid(i)
; SetGadgetColor(strY, #PB_Gadget_BackColor, $00ccff)
Next i

Repeat  : Until WaitWindowEvent() = #PB_Event_CloseWindow 

Egypt my love
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: regain #Gadget value from string ?

Post by IdeasVacuum »

Hi Vera

Try this:

Code: Select all

#StrGdgt0 = 0
#StrGdgt1 = 1
#StrGdgt2 = 2

Win_0 = OpenWindow(#PB_Any, 0, 0, 100, 100, "hui",  #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_TitleBar )
   StringGadget(#StrGdgt0, 10, 10, 20, 24, "1")
   StringGadget(#StrGdgt1, 40, 10, 20, 24, "2")
   StringGadget(#StrGdgt2, 70, 10, 20, 24, "3")

For i= 0 To 2
SetGadgetColor(i, #PB_Gadget_BackColor, $00ccff)
Next i

Repeat  : Until WaitWindowEvent() = #PB_Event_CloseWindow
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: regain #Gadget value from string ?

Post by Vera »

Thanks to you :)

both work fine ~ now I'll have a closer look how & why

cheers ~ Vera
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: regain #Gadget value from string ?

Post by netmaestro »

For interest's sake, same task when nothing is known about the stringgadgets:

Code: Select all

Procedure EnumStrings(hwnd, lparam)
  cn$ = Space(100)
  GetClassName_(hwnd, @cn$, 99)
  If UCase(cn$) = "EDIT"
    SetGadgetColor(GetDlgCtrlID_(hwnd), #PB_Gadget_BackColor, $00ccff)
  EndIf
  ProcedureReturn #True
EndProcedure
 
Win_0 = OpenWindow(#PB_Any, 0, 0, 100, 100, "hui",  #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_TitleBar )
   StringGadget(#PB_Any, 10, 10, 20, 24, "1")
   StringGadget(#PB_Any, 40, 10, 20, 24, "2")
   StringGadget(#PB_Any, 70, 10, 20, 24, "3")
   EnumChildWindows_(WindowID(win_0), @EnumStrings(), 0)
   
Repeat  : Until WaitWindowEvent() = #PB_Event_CloseWindow
Windows only, of course :mrgreen:
BERESHEIT
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: regain #Gadget value from string ?

Post by Vera »

Hi,

I realize that my major hindrance arose from using #PB_Any. Had I taken constants as usually I might have come across the 'classic' solution halfway naturally. Though favoring more straight ways the AnyID-array solves what I struggled with and is a nice lesson to learn.

But I have one question: why is the dimension in this example set to (4) although only (2) [0-2=3] are needed?

@ netmaestro
have no opportunity to test it ad hoc but it looks as if all existing gadgets would be addressed and changed (colored) in one go. gripping snippet ~ thanks

I'd like to show you what I've been aming at and maybe you have some valuable hints about these nested event loop. As it's only meant as nice closing gimmick [I call it endSplash ;)] it's fine so far, but ultimately it's not a proper way to do it.

Code: Select all

ci = CreateImage(#PB_Any, 100, 100 )  ;   , 32| #PB_Image_Transparent
Global Dim Anyid(8)

Win_0 = OpenWindow(#PB_Any, 0, 0, 100, 100, "hui",   #PB_Window_ScreenCentered | #PB_Window_BorderLess ) ;#PB_Window_SystemMenu |
StickyWindow(Win_0, 1)
img = ImageGadget(#PB_Any, 0,0, 100, 100, ImageID(ci))

   Anyid(0) = StringGadget(#PB_Any, 10, 10, 20, 20, " 1")  ; , #PB_String_BorderLess
   Anyid(1) = StringGadget(#PB_Any, 40, 10, 20, 20, " 2")  ; , #PB_String_BorderLess
   Anyid(2) = StringGadget(#PB_Any, 70, 10, 20, 20, " 3")  ; , #PB_String_BorderLess
   Anyid(3) = StringGadget(#PB_Any, 10, 40, 20, 20, " S")  ; , #PB_String_BorderLess
   Anyid(4) = StringGadget(#PB_Any, 40, 40, 20, 20, " O")  ; , #PB_String_BorderLess
   Anyid(5) = StringGadget(#PB_Any, 70, 40, 20, 20, " L")  ; , #PB_String_BorderLess
   Anyid(6) = StringGadget(#PB_Any, 10, 70, 20, 20, " V")  ; , #PB_String_BorderLess
   Anyid(7) = StringGadget(#PB_Any, 40, 70, 20, 20, " E")  ; , #PB_String_BorderLess
   Anyid(8) = StringGadget(#PB_Any, 70, 70, 20, 20, " D")  ; , #PB_String_BorderLess
      
  StartDrawing(ImageOutput(ci))
  DrawingMode(#PB_2DDrawing_Outlined)
  Box(3,3, 94,94, $00ccff)
;   Box(2,2, 96,96, $00ccff)           ; alternativ with
;   Box(5,5, 90,90, $00ccff)           ; two borderlines
  StopDrawing()
  SetGadgetState(img, ImageID(ci))
  
Procedure recol()
For n = 0 To 2
  startTime=ElapsedMilliseconds()
  Repeat
    WaitWindowEvent(33)
   
    For i= 0 To 8
      startTime=ElapsedMilliseconds()
      Repeat
        WaitWindowEvent(22)  ; or
        SetGadgetColor(Anyid(i), #PB_Gadget_BackColor, Random(RGB(255, 255, 255)) )
        Until ElapsedMilliseconds()-startTime>4 ; or e.g. 539 or 39
    Next i
   
  Until ElapsedMilliseconds()-startTime>9
Next n
EndProcedure

recol()

; ;------------------- Alternativ version 2 ----------------
; 
; Procedure recol()
; For n = 0 To 12
;   startTime=ElapsedMilliseconds()
;   Repeat
;     WaitWindowEvent(3)
;    
;     For i= 0 To 8
;       startTime=ElapsedMilliseconds()
;       Repeat
;         WaitWindowEvent(3) 
;         Until ElapsedMilliseconds()-startTime>22 ; en-/crease the delay
;         SetGadgetColor(Anyid(i), #PB_Gadget_BackColor, Random(RGB(255, 255, 255)) )
;     Next i
;    
;   Until ElapsedMilliseconds()-startTime>9
; Next n
; EndProcedure
; 
; recol()
; 
; ;------------------- -------------- ----------------


Repeat  
  wwe = WaitWindowEvent()
  
    Select wwe
      Case #PB_Event_CloseWindow
        Break
    
       Case #PB_Event_Gadget
         Select EventGadget()
           Case img
             Select EventType()
               Case #PB_EventType_LeftClick
                 Break
               Case #PB_EventType_RightClick
                 recol()
               EndSelect
               
         EndSelect
         
    EndSelect
            
Until wwe = #PB_Event_CloseWindow
cheers ~ Vera

edit: added rightclick feature to review the effect // added alternativ procedure, made gadgets quadratic
Last edited by Vera on Thu Oct 21, 2010 11:40 pm, edited 2 times in total.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: regain #Gadget value from string ?

Post by RASHAD »

I meant by 4 that you can go to any number of gadgets in one line
Beside I am not neat
Next is some fancy variant

Code: Select all

ci = CreateImage(#PB_Any, 100, 105 )  ;   , 32| #PB_Image_Transparent
Global Dim Anyid(8)

Procedure SetGColor(parameter)
  Repeat
        For i= 0 To 8
        Delay(50)
        SetGadgetColor(Anyid(i), #PB_Gadget_BackColor, Random(RGB(255, 255, 255)) )
      Next i
    ForEver
EndProcedure

Win_0 = OpenWindow(#PB_Any, 0, 0, 100, 105, "hui",   #PB_Window_ScreenCentered | #PB_Window_BorderLess ) ;#PB_Window_SystemMenu |
StickyWindow(Win_0, 1)
img = ImageGadget(#PB_Any, 0,0, 100, 105, ImageID(ci))

   Anyid(0) = StringGadget(#PB_Any, 10, 10, 20, 24, "1")  ; , #PB_String_BorderLess
   Anyid(1) = StringGadget(#PB_Any, 40, 10, 20, 24, "2")  ; , #PB_String_BorderLess
   Anyid(2) = StringGadget(#PB_Any, 70, 10, 20, 24, "3")  ; , #PB_String_BorderLess
   Anyid(3) = StringGadget(#PB_Any, 10, 40, 20, 24, "S")  ; , #PB_String_BorderLess
   Anyid(4) = StringGadget(#PB_Any, 40, 40, 20, 24, "O")  ; , #PB_String_BorderLess
   Anyid(5) = StringGadget(#PB_Any, 70, 40, 20, 24, "L")  ; , #PB_String_BorderLess
   Anyid(6) = StringGadget(#PB_Any, 10, 70, 20, 24, "V")  ; , #PB_String_BorderLess
   Anyid(7) = StringGadget(#PB_Any, 40, 70, 20, 24, "E")  ; , #PB_String_BorderLess
   Anyid(8) = StringGadget(#PB_Any, 70, 70, 20, 24, "D")  ; , #PB_String_BorderLess
     
  StartDrawing(ImageOutput(ci))
  DrawingMode(#PB_2DDrawing_Outlined)
  Box(3,3, 94,99, $00ccff)
  StopDrawing()
  SetGadgetState(img, ImageID(ci))
  
  Thread = CreateThread(@SetGColor(),50)


Repeat

  wwe = WaitWindowEvent()
 
    Select wwe
      Case #PB_Event_CloseWindow
        Break
       Case #PB_Event_Gadget
         Select EventGadget()
           Case img
             Select EventType()
               Case #PB_EventType_LeftClick
                 Break
               EndSelect
         EndSelect
    EndSelect
           
Until wwe = #PB_Event_CloseWindow

Egypt my love
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: regain #Gadget value from string ?

Post by Vera »

What a nice example incl. a thread I haven't touched yet. 8)

Being on Linux: the app starts, nothing happens until the mouse hoovers the GUI and it quickly freezes. Rarely it shuffles the colors for a short while, but still ends up freezing.

Error: Application determined (by an external library)
Line: SetGadgetColor(Anyid(i), #PB_Gadget_BackColor, Random(RGB(255, 255, 255)) )


Any idea what library could be meant?
WilliamL
Addict
Addict
Posts: 1252
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: regain #Gadget value from string ?

Post by WilliamL »

Hey Vera,

Your code works great on my Mac (x86). Using the stringgadget was fortunate choice since that is the only gadget that color works on with the Mac.

@Rashad
I get an IMA error after the third box is drawn (in line 7)... or the forth box and the error is reported in the event loop... or the...

[edit] Used 'create threadsafe' didn't change the IMA (as Vera tried below).
If I take out the Repeat/Forever in the thread, it runs fine.
Last edited by WilliamL on Wed Oct 20, 2010 9:39 pm, edited 2 times in total.
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: regain #Gadget value from string ?

Post by Vera »

hihi ~ nice to hear it's triple-cross :mrgreen:

btw: just found: if you set the compiler option to 'create threadsafe executeable' the error doesn't appear and runs straight ahead until mouseover or time runout stops it

ups: I've take the wrong tab :(
still if I set 'create threadsafe executeable' the errorwarning now points to : wwe = WaitWindowEvent()

still fancy to see :)

I found it : repeat event needs to be changed to WindowEvent() + Delay()

Code: Select all

Repeat
  wwe = WindowEvent()
    Select wwe
      Case #PB_Event_CloseWindow
        Break
       Case #PB_Event_Gadget
         Select EventGadget()
           Case img
             Select EventType()
               Case #PB_EventType_LeftClick
                 Break
               EndSelect
         EndSelect
    EndSelect
   Delay(1)
Until wwe = #PB_Event_CloseWindow
WilliamL
Addict
Addict
Posts: 1252
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: regain #Gadget value from string ?

Post by WilliamL »

Interesting usage of WaitWindowEvent(33) to force the update of the gadgets and also cause a delay.
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: regain #Gadget value from string ?

Post by Fred »

Never put a Delay() right after a WindowEvent(), or you won't process them at full speed and your GUI will lag a lot. Do the delay only if WindowEvent() returns 0, which means there is no more event in the queue. The other (better) alternatuive is to use WaitWindowEvent(delay).
Post Reply