Page 1 of 1

for / next of a ENUM Code Inserted

Posted: Fri Nov 02, 2007 5:44 pm
by Rook Zimbabwe
OK I have a bunch of #BUTTON_ things and I want to clear the text off all of them in one solid proceedure... I have tried this:

Code: Select all

Procedure ClearITEMBUT()
For x = 0 To 24
  SetGadgetText(#BUTTON_IT(x), "")
    Next

EndProcedure
but nope! Buttons are #BUTTON_IT0 to #BUTTON_IT24... :D

also tried this:

Code: Select all

Procedure ClearITEMBUT()
For x = 0 To 24
  xx$ = Str(x)
  fu$ = "#BUTTON_IT"+xx$
  SetGadgetText(fu$, "")
    Next

EndProcedure
There must be a way to do this without having to go through the entire list!

Posted: Fri Nov 02, 2007 5:49 pm
by Trond
Be sure to put them in an enumeration, and do this:

Code: Select all

Procedure ClearITEMBUT() 
  For x = 0 To 24 
    SetGadgetText(#BUTTON_IT0+x, "") 
  Next
EndProcedure

Posted: Fri Nov 02, 2007 5:51 pm
by Rook Zimbabwe
Oh crud! Thats because it is a number... D'Oh!!!

Thanks Trond!

This is like Algebra when the teacher insisted that A+B = 22 and I told her that meant A = 11 and B = 11... no it isn't but I wanted to quip! 8)

Posted: Fri Nov 02, 2007 6:44 pm
by Comtois
Or like this ?

Code: Select all

Procedure ClearITEMBUT()
  For x = #BUTTON_IT0 To #BUTTON_IT24
    SetGadgetText(x, "")
  Next
EndProcedure

Posted: Fri Nov 02, 2007 7:38 pm
by Rook Zimbabwe

Code: Select all

Procedure ClearITEMBUT() 
  For x = #BUTTON_IT0 To #BUTTON_IT24 
    SetGadgetText(x, "") 
  Next 
EndProcedure
Code just jumps over that it shows nothing and does not work! Hmmm... It should!

Posted: Sat Nov 03, 2007 1:31 am
by PB
> Code just jumps over that it shows nothing and does not work! Hmmm... It should!

We need a full example to see why not.

Posted: Sat Nov 03, 2007 2:50 am
by Rook Zimbabwe
OK I have attempted to create 2 almost identical programs with the 2 PB FORM EDITORS out there... With the older version I can make this work. I was trying to get this idea to work with the new form editor.

It is probably my ignorance causing the problems (my wife always says so! :D )

OK the first version is the menu1.pref file. You can copy and paste it to the file named menu1.pref in the same directory as your program and all is fine.

Code: Select all

[Global]
SEL0 = Appetizer
SEL1 = Burgers

[Appetizer]
SEL0 = Nacho
SEL1 = BEEFY NACHO
SEL2 = SUPER NACHO
SEL3 = FRIED JALAPENO
SEL4 = ONION BRICK

[Burgers]
SEL0 = HAMBURGER
SEL1 = CHEESE BURGER
SEL2 = GAUCO BURGER
SEL3 = BACO BURGER
SEL4 = MEXI BURGER
SEL5 = FIESTA BURGER
OK now the program (WHICH WORKS) designed with formeditor 3.94

Code: Select all

; PureBasic Visual Designer v3.95 build 1485 (PB4Code)

;- Window Constants
;
Enumeration
  #Window_0
EndEnumeration

;- Gadget Constants
;
Enumeration
  #Button_IT0
  #Button_IT1
  #Button_SEL0
  #Button_SEL1
  #Button_SEL2
  #Button_SEL3
  #Button_SEL4
  #Button_SEL5
  #Button_SEL6
  #Button_SEL7
  #Button_SEL8
  #String_WHAT
  #Button_SELCLR
EndEnumeration

Global what
Global who$
Global FUBAR$

Global Dim ITEMS$(9)
Global Dim SELS$(9)

OpenPreferences("Menu.pref"); Preferences.prefs
  PreferenceGroup("Global")
    For i = 0 To 8
          se$ = Str(i)
        ITEMS$(0+i) = ReadPreferenceString("SEL"+se$, "")
    Next
ClosePreferences()

Procedure Open_Window_0()
  If OpenWindow(#Window_0, 596, 86, 349, 236, "New window ( 0 )",  #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar )
    If CreateGadgetList(WindowID(#Window_0))
      ButtonGadget(#Button_IT0, 10, 10, 90, 30, "")
      ButtonGadget(#Button_IT1, 10, 50, 90, 30, "")
      ButtonGadget(#Button_SEL0, 120, 10, 70, 40, "",#PB_Button_MultiLine)
      ButtonGadget(#Button_SEL1, 190, 10, 70, 40, "",#PB_Button_MultiLine)
      ButtonGadget(#Button_SEL2, 120, 60, 70, 40, "",#PB_Button_MultiLine)
      ButtonGadget(#Button_SEL3, 190, 60, 70, 40, "",#PB_Button_MultiLine)
      ButtonGadget(#Button_SEL4, 120, 110, 70, 40, "",#PB_Button_MultiLine)
      ButtonGadget(#Button_SEL5, 190, 110, 70, 40, "",#PB_Button_MultiLine)
      ButtonGadget(#Button_SEL6, 260, 10, 70, 40, "",#PB_Button_MultiLine)
      ButtonGadget(#Button_SEL7, 260, 60, 70, 40, "",#PB_Button_MultiLine)
      ButtonGadget(#Button_SEL8, 260, 110, 70, 40, "",#PB_Button_MultiLine)
      StringGadget(#String_WHAT, 10, 160, 330, 20, "")
      ButtonGadget(#Button_SELCLR, 10, 190, 100, 30, "CLEAR")
      
    EndIf
  EndIf
EndProcedure

Procedure SELCLR()

   For x = #Button_SEL0 To #Button_SEL8 
     SetGadgetText(x, "") 
   Next 

EndProcedure

Procedure FUBAR(what)

      SetGadgetText(#String_WHAT, "")
        FUBAR$ = GetGadgetText(what)
          SetGadgetText(#String_WHAT, FUBAR$)

EndProcedure

Procedure PLOPIT(what)
  who$ = GetGadgetText(what)
   OpenPreferences("Menu1.pref")
      PreferenceGroup(who$)
         For i = 0 To 8
            se$ = Str(i)
              SELS$(0+i) = ReadPreferenceString("SEL"+se$, "")
         Next
              For z = 0 To 8
                SetGadgetText(#Button_SEL0+z, SELS$(z))
              Next
       ClosePreferences() 

EndProcedure


Open_Window_0()

SetGadgetText(#Button_IT0, ITEMS$(0))
  SetGadgetText(#Button_IT1, ITEMS$(1))

Repeat ; Start of the event loop
  
  Event = WaitWindowEvent() ; This line waits until an event is received from Windows
  
  WindowID = EventWindow() ; The Window where the event is generated, can be used in the gadget procedures
  
  GadgetID = EventGadget() ; Is it a gadget event?
  
  EventType = EventType() ; The event type
  
  ;You can place code here, and use the result as parameters for the procedures
  
  If Event = #PB_Event_Gadget
    
    If GadgetID = #Button_IT0
      PLOPIT(#Button_IT0)
    ElseIf GadgetID = #Button_IT1
      PLOPIT(#Button_IT1)
    ElseIf GadgetID = #Button_SEL0
      FUBAR(#Button_SEL0)
    ElseIf GadgetID = #Button_SEL1
      FUBAR(#Button_SEL1)
    ElseIf GadgetID = #Button_SEL2
      FUBAR(#Button_SEL2)   
    ElseIf GadgetID = #Button_SEL3
      FUBAR(#Button_SEL3)
    ElseIf GadgetID = #Button_SEL4
      FUBAR(#Button_SEL4)     
    ElseIf GadgetID = #Button_SEL5
      FUBAR(#Button_SEL5)
    ElseIf GadgetID = #Button_SEL6
      FUBAR(#Button_SEL6)
    ElseIf GadgetID = #Button_SEL7
      FUBAR(#Button_SEL7)
    ElseIf GadgetID = #Button_SEL8
      FUBAR(#Button_SEL8)
    ElseIf GadgetID = #String_WHAT
      
    ElseIf GadgetID = #Button_SELCLR
      SELCLR()
    EndIf
    
  EndIf
  
Until Event = #PB_Event_CloseWindow ; End of the event loop

End
;
OK NOW THE PROBLEM CHILD!!!

Code: Select all


Enumeration
  #Window_0
EndEnumeration

Enumeration
  #Button_ITCLR
  #String_0
  #Button_IT8
  #Button_IT7
  #Button_IT6
  #Button_IT5
  #Button_IT4
  #Button_IT3
  #Button_IT2
  #Button_IT1
  #Button_IT0
  #Button_MENU1
  #Button_MENU0
EndEnumeration

Structure VisualDesignerGadgets
  Gadget.l
  EventFunction.l
EndStructure

Global what
Global who$
Global FUBAR$

Global Dim ITEMS$(9)
Global Dim SELS$(9)

OpenPreferences("Menu.pref"); Preferences.prefs
  PreferenceGroup("Global")
    For i = 0 To 8
          se$ = Str(i)
        ITEMS$(0+i) = ReadPreferenceString("SEL"+se$, "")
    Next
ClosePreferences()

Global NewList EventProcedures.VisualDesignerGadgets()

Procedure FUBAR(what)

      SetGadgetText(#String_0, "")
        FUBAR$ = GetGadgetText(what)
          SetGadgetText(#String_0, FUBAR$)

EndProcedure


Procedure PLOPIT(what)
  who$ = GetGadgetText(what)
   OpenPreferences("Menu1.pref")
      PreferenceGroup(who$)
         For i = 0 To 8
            se$ = Str(i)
              SELS$(0+i) = ReadPreferenceString("SEL"+se$, "")
         Next
              For z = 0 To 8
                SetGadgetText(#Button_IT0+z, SELS$(z))
              Next
       ClosePreferences() 

EndProcedure

Procedure SELCLR()

   For x = #Button_IT0 To #Button_IT8 
     SetGadgetText(x, "") 
   Next 

EndProcedure

Procedure Button_ITCLR_Event(Window, Event, Gadget, Type)
  
  SELCLR()
EndProcedure

Procedure String_0_Event(Window, Event, Gadget, Type)
  Debug "#String_0"
EndProcedure

Procedure Button_IT8_Event(Window, Event, Gadget, Type)
  FUBAR(#Button_IT8)
EndProcedure

Procedure Button_IT7_Event(Window, Event, Gadget, Type)
  FUBAR(#Button_IT7)
EndProcedure

Procedure Button_IT6_Event(Window, Event, Gadget, Type)
  FUBAR(#Button_IT6)
EndProcedure

Procedure Button_IT5_Event(Window, Event, Gadget, Type)
  FUBAR(#Button_IT5)
EndProcedure

Procedure Button_IT4_Event(Window, Event, Gadget, Type)
  FUBAR(#Button_IT4)
EndProcedure

Procedure Button_IT3_Event(Window, Event, Gadget, Type)
  FUBAR(#Button_IT3)
EndProcedure

Procedure Button_IT2_Event(Window, Event, Gadget, Type)
  FUBAR(#Button_IT2)
EndProcedure

Procedure Button_IT1_Event(Window, Event, Gadget, Type)
  FUBAR(#Button_IT1)
EndProcedure

Procedure Button_IT0_Event(Window, Event, Gadget, Type)
  FUBAR(#Button_IT0)
EndProcedure

Procedure Button_MENU1_Event(Window, Event, Gadget, Type)
  Debug "#Button_MENU1"
  PLOPIT(#Button_MENU1)
EndProcedure

Procedure Button_MENU0_Event(Window, Event, Gadget, Type)
  Debug "#Button_MENU0"
  PLOPIT(#Button_MENU0)
EndProcedure

Procedure RegisterGadgetEvent(Gadget, *Function)
  
  If IsGadget(Gadget)
    AddElement(EventProcedures())
    EventProcedures()\Gadget        = Gadget
    EventProcedures()\EventFunction = *Function
  EndIf
  
EndProcedure

Procedure CallEventFunction(Window, Event, Gadget, Type)
  
  ForEach EventProcedures()
    If EventProcedures()\Gadget = Gadget
      CallFunctionFast(EventProcedures()\EventFunction, Window, Event, Gadget, Type)
      LastElement(EventProcedures())
    EndIf
  Next
  
EndProcedure

Procedure Open_Window_0()
  
  If OpenWindow(#Window_0, 492, 64, 372, 263, "Window 0",  #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar )
    If CreateGadgetList(WindowID(#Window_0))
      ButtonGadget(#Button_MENU0, 10, 15, 105, 25, "")
      RegisterGadgetEvent(#Button_MENU0, @Button_MENU0_Event())
      ButtonGadget(#Button_MENU1, 10, 45, 105, 25, "")
      RegisterGadgetEvent(#Button_MENU1, @Button_MENU1_Event())
      ButtonGadget(#Button_IT0, 120, 15, 80, 55, "", #PB_Button_MultiLine)
      RegisterGadgetEvent(#Button_IT0, @Button_IT0_Event())
      ButtonGadget(#Button_IT1, 200, 15, 80, 55, "")
      RegisterGadgetEvent(#Button_IT1, @Button_IT1_Event())
      ButtonGadget(#Button_IT2, 280, 15, 80, 55, "")
      RegisterGadgetEvent(#Button_IT2, @Button_IT2_Event())
      ButtonGadget(#Button_IT3, 120, 70, 80, 55, "")
      RegisterGadgetEvent(#Button_IT3, @Button_IT3_Event())
      ButtonGadget(#Button_IT4, 200, 70, 80, 55, "")
      RegisterGadgetEvent(#Button_IT4, @Button_IT4_Event())
      ButtonGadget(#Button_IT5, 280, 70, 80, 55, "")
      RegisterGadgetEvent(#Button_IT5, @Button_IT5_Event())
      ButtonGadget(#Button_IT6, 120, 125, 80, 55, "")
      RegisterGadgetEvent(#Button_IT6, @Button_IT6_Event())
      ButtonGadget(#Button_IT7, 200, 125, 80, 55, "")
      RegisterGadgetEvent(#Button_IT7, @Button_IT7_Event())
      ButtonGadget(#Button_IT8, 280, 125, 80, 55, "")
      RegisterGadgetEvent(#Button_IT8, @Button_IT8_Event())
      StringGadget(#String_0, 10, 185, 350, 25, "")
      RegisterGadgetEvent(#String_0, @String_0_Event())
      ButtonGadget(#Button_ITCLR, 10, 215, 350, 35, "CLEAR ITEMS")
      RegisterGadgetEvent(#Button_ITCLR, @Button_ITCLR_Event())
      
    EndIf
  EndIf
EndProcedure



Open_Window_0()

Repeat
  
  Event  = WaitWindowEvent()
  Gadget = EventGadget()
  Type   = EventType()
  Window = EventWindow()
  
  Select Event
    Case #PB_Event_Gadget
      CallEventFunction(Window, Event, Gadget, Type)
      
  EndSelect
  
Until Event = #PB_Event_CloseWindow

End

Where have I done did gone wrong?

Posted: Sat Nov 03, 2007 3:37 am
by PB
> copy and paste it to the file named menu1.pref in the same directory as
> your program and all is fine

Not here. When I do that, all the buttons are blank, even if I put a full path
to the menu1.pref file in the source (in the PLOPIT() procedure). Therefore
I can't help at this stage as your example doesn't work as it's posted here.

Are you trying to do something like this:

Code: Select all

OpenWindow(0,0,0,640,480,"test",#PB_Window_SystemMenu)
CreateGadgetList(WindowID(0))

ButtonGadget(0,10,10,150,25,"Clear all other buttons")

For a=1 To 10
  x+40 : y+40
  ButtonGadget(a,x,y,50,20,Str(a))
Next

Repeat

  ev=WaitWindowEvent()

  If ev=#PB_Event_Gadget And EventGadget()=0

    For a=1 To 10 : SetGadgetText(a,"") : Next

  EndIf

Until ev=#PB_Event_CloseWindow
.

Posted: Sat Nov 03, 2007 3:54 am
by Sparkie

Code: Select all

Enumeration 
  #Button_ITCLR 
  #String_0 
  #Button_IT8 
  #Button_IT7 
  #Button_IT6 
  #Button_IT5 
  #Button_IT4 
  #Button_IT3 
  #Button_IT2 
  #Button_IT1 
  #Button_IT0 
  #Button_MENU1 
  #Button_MENU0 
EndEnumeration 
Then in your Procedure PLOPIT(what) you have a loop For z = 0 To 8 and SetGadgetText(#Button_IT0+z, SELS$(z)). The highest Enumerated gadget is #Button_IT0 + 2 therefore you get the error.

Posted: Sat Nov 03, 2007 4:31 am
by Rook Zimbabwe
{{{ OY! I was right... it was my own blind stupidity!!! }}}

Code: Select all


Enumeration
  #Window_0
EndEnumeration

Enumeration
  #Button_ITCLR
  #String_0
  #Button_IT0
  #Button_IT1
  #Button_IT2
  #Button_IT3
  #Button_IT4
  #Button_IT5
  #Button_IT6
  #Button_IT7
  #Button_IT8
  #Button_MENU1
  #Button_MENU0
EndEnumeration

Structure VisualDesignerGadgets
  Gadget.l
  EventFunction.l
EndStructure

Global what
Global who$
Global FUBAR$

Global Dim ITEMS$(9)
Global Dim SELS$(9)

OpenPreferences("Menu.pref"); Preferences.prefs
  PreferenceGroup("Global")
    For i = 0 To 2
          se$ = Str(i)
        ITEMS$(0+i) = ReadPreferenceString("SEL"+se$, "")
    Next
ClosePreferences()

Global NewList EventProcedures.VisualDesignerGadgets()

Procedure RegisterGadgetEvent(Gadget, *Function)
  
  If IsGadget(Gadget)
    AddElement(EventProcedures())
    EventProcedures()\Gadget        = Gadget
    EventProcedures()\EventFunction = *Function
  EndIf
  
EndProcedure

Procedure CallEventFunction(Window, Event, Gadget, Type)
  
  ForEach EventProcedures()
    If EventProcedures()\Gadget = Gadget
      CallFunctionFast(EventProcedures()\EventFunction, Window, Event, Gadget, Type)
      LastElement(EventProcedures())
    EndIf
  Next
  
EndProcedure

Procedure FUBAR(what)

      SetGadgetText(#String_0, "")
        FUBAR$ = GetGadgetText(what)
          SetGadgetText(#String_0, FUBAR$)

EndProcedure

Procedure PLOPIT(what)
  z = 0
  who$ = GetGadgetText(what)
   OpenPreferences("Menu1.pref")
      PreferenceGroup(who$)
         For i = #Button_IT0 To #Button_IT8
            se$ = Str(z)
              SELS$(z) = ReadPreferenceString("SEL"+se$, "")
                SetGadgetText(i, SELS$(z))
                  Debug z
                z = z+1
         Next
    ClosePreferences() 

EndProcedure

Procedure SELCLR()

   For x = #Button_IT0 To #Button_IT8 
     SetGadgetText(x, "") 
   Next 

EndProcedure

Procedure Button_ITCLR_Event(Window, Event, Gadget, Type)
  
  SELCLR()
EndProcedure

Procedure String_0_Event(Window, Event, Gadget, Type)
  Debug "#String_0"
EndProcedure

Procedure Button_IT8_Event(Window, Event, Gadget, Type)
  FUBAR(#Button_IT8)
EndProcedure

Procedure Button_IT7_Event(Window, Event, Gadget, Type)
  FUBAR(#Button_IT7)
EndProcedure

Procedure Button_IT6_Event(Window, Event, Gadget, Type)
  FUBAR(#Button_IT6)
EndProcedure

Procedure Button_IT5_Event(Window, Event, Gadget, Type)
  FUBAR(#Button_IT5)
EndProcedure

Procedure Button_IT4_Event(Window, Event, Gadget, Type)
  FUBAR(#Button_IT4)
EndProcedure

Procedure Button_IT3_Event(Window, Event, Gadget, Type)
  FUBAR(#Button_IT3)
EndProcedure

Procedure Button_IT2_Event(Window, Event, Gadget, Type)
  FUBAR(#Button_IT2)
EndProcedure

Procedure Button_IT1_Event(Window, Event, Gadget, Type)
  FUBAR(#Button_IT1)
EndProcedure

Procedure Button_IT0_Event(Window, Event, Gadget, Type)
  FUBAR(#Button_IT0)
EndProcedure

Procedure Button_MENU1_Event(Window, Event, Gadget, Type)
  Debug "#Button_MENU1"
  PLOPIT(#Button_MENU1)
EndProcedure

Procedure Button_MENU0_Event(Window, Event, Gadget, Type)
  Debug "#Button_MENU0"
  PLOPIT(#Button_MENU0)
EndProcedure

Procedure Open_Window_0()
  
  If OpenWindow(#Window_0, 492, 64, 372, 263, "Window 0",  #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar )
    If CreateGadgetList(WindowID(#Window_0))
      ButtonGadget(#Button_MENU0, 10, 15, 105, 25, "")
      RegisterGadgetEvent(#Button_MENU0, @Button_MENU0_Event())
      ButtonGadget(#Button_MENU1, 10, 45, 105, 25, "")
      RegisterGadgetEvent(#Button_MENU1, @Button_MENU1_Event())
      ButtonGadget(#Button_IT0, 120, 15, 80, 55, "", #PB_Button_MultiLine)
      RegisterGadgetEvent(#Button_IT0, @Button_IT0_Event())
      ButtonGadget(#Button_IT1, 200, 15, 80, 55, "")
      RegisterGadgetEvent(#Button_IT1, @Button_IT1_Event())
      ButtonGadget(#Button_IT2, 280, 15, 80, 55, "",#PB_Button_MultiLine)
      RegisterGadgetEvent(#Button_IT2, @Button_IT2_Event())
      ButtonGadget(#Button_IT3, 120, 70, 80, 55, "",#PB_Button_MultiLine)
      RegisterGadgetEvent(#Button_IT3, @Button_IT3_Event())
      ButtonGadget(#Button_IT4, 200, 70, 80, 55, "",#PB_Button_MultiLine)
      RegisterGadgetEvent(#Button_IT4, @Button_IT4_Event())
      ButtonGadget(#Button_IT5, 280, 70, 80, 55, "",#PB_Button_MultiLine)
      RegisterGadgetEvent(#Button_IT5, @Button_IT5_Event())
      ButtonGadget(#Button_IT6, 120, 125, 80, 55, "",#PB_Button_MultiLine)
      RegisterGadgetEvent(#Button_IT6, @Button_IT6_Event())
      ButtonGadget(#Button_IT7, 200, 125, 80, 55, "",#PB_Button_MultiLine)
      RegisterGadgetEvent(#Button_IT7, @Button_IT7_Event())
      ButtonGadget(#Button_IT8, 280, 125, 80, 55, "",#PB_Button_MultiLine)
      RegisterGadgetEvent(#Button_IT8, @Button_IT8_Event())
      StringGadget(#String_0, 10, 185, 350, 25, "",#PB_Button_MultiLine)
      RegisterGadgetEvent(#String_0, @String_0_Event())
      ButtonGadget(#Button_ITCLR, 10, 215, 350, 35, "CLEAR ITEMS")
      RegisterGadgetEvent(#Button_ITCLR, @Button_ITCLR_Event())
      
    EndIf
  EndIf
EndProcedure
Open_Window_0()

SetGadgetText(#Button_MENU0, ITEMS$(0))
  SetGadgetText(#Button_MENU1, ITEMS$(1))

Repeat
  
  Event  = WaitWindowEvent()
  Gadget = EventGadget()
  Type   = EventType()
  Window = EventWindow()
  
  Select Event
    Case #PB_Event_Gadget
      CallEventFunction(Window, Event, Gadget, Type)
      
  EndSelect
  
Until Event = #PB_Event_CloseWindow

End
The #Button_ITXX has to be in ascending order to prog them in a list sequence in a FOR/NEXT... I didn't check enumeration. Both versions work now...

Thanks Sparkie! You steered me correct!

@PB: I don't know what to say I assume Sparkie got it to work and mine works... I have a suggestion, I don't know if you did this... Windows sometime messes up the file extensions if you tried to save with NOTEPAD.

You may either try adding the filename to your program as menu1.pref.txt of alternately selecting SAVEAS and insisting to the program you want to see ALL FILES (*.*) in the drop down selector and THEN typing menu1.pref :)