Coding Question - Use of variables in Purevision Gadget name

Just starting out? Need help? Post your questions and find answers here.
calypso
User
User
Posts: 17
Joined: Sun Mar 26, 2023 7:23 am
Location: Vancouver BC Canada

Coding Question - Use of variables in Purevision Gadget name

Post by calypso »

I have 84 Button Gadgets on a form. I need to hide / unhide some of them based on a value of a variable in an array.

Here is a snippet of the code I have that works. The button gadget caption is first assigned a text string that resides (or not) in an array.

Code: Select all

 SetGadgetText(#Gadget_Launch_VNC_Form1_Button_Row1_Col1, Array$(1,1,1))    ;array row (city), column(node name), IP address
  If Len(Array$(1,1,1)) < 3                                                 ;no data in array element = no button assignment
    HideGadget(#Gadget_Launch_VNC_Form1_Button_Row1_Col1,1)                 ;hide button
  EndIf
There are 14 Rows of buttons with each row having 6 columns. some buttons are not assigned with anything and those are the ones to hide with the above code.

I have tried to find a way to use variables for the gadget row and column name so I don't have to duplicate the example 4 lines of code 84 times and use a simple for next loop to scan through the array and hide the buttons that are not assigned with a node name and ip address.

Does anyone have any suggestions as to how I can minimize the lines of code needed to accomplish the task of first setting the button gadget caption and then hiding the buttons that have no caption text?

Any help is appreciated.

Thank you,

Stephen
wombats
Enthusiast
Enthusiast
Posts: 716
Joined: Thu Dec 29, 2011 5:03 pm

Re: Coding Question - Use of variables in Purevision Gadget name

Post by wombats »

You could use a map by storing all the gadget handles in the map and accessing them via the key.

Code: Select all

EnableExplicit

OpenWindow(0, 0, 0, 170, 50, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

ButtonGadget(0, 10, 10, 30, 30, "0")
ButtonGadget(1, 40, 10, 30, 30, "1")
ButtonGadget(2, 70, 10, 30, 30, "2")
ButtonGadget(3, 100, 10, 30, 30, "3")
ButtonGadget(4, 130, 10, 30, 30, "4")

Define i.i
NewMap buttons.i()
For i = 0 To 4
  buttons("Button" + Str(i)) = i
Next

HideGadget(buttons("Button2"), #True)
HideGadget(buttons("Button4"), #True)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
You could set the key name in the map to "ButtonXY", where X and Y are the row and column, perhaps.
Denis
Enthusiast
Enthusiast
Posts: 778
Joined: Fri Apr 25, 2003 5:10 pm
Location: Doubs - France

Re: Coding Question - Use of variables in Purevision Gadget name

Post by Denis »

I don't know how you create all buttons,
the code below is a way to do the job (certainly not the one and only way to do it)

Code: Select all

;-  Structure person
Structure person
      ButtonGadget_Id.i
      city.s
      node_name.s
      IP_address.s
EndStructure

Macro _EncodeButton_Id(row, col)
   ; encode row/col
      (row|(col<<16))
EndMacro

;-  Map My_Button.person()
Global NewMap My_Button.person()

Procedure CreateButtons()
      Protected row, col
      Protected Gadget
      Protected X, Y, width, heigh
      
      For row = 1 To 14
            For col = 1 To 6
                  
                  ; create Data for each button,
                  X = ?
                  Y = ?
                  width = ?
                  heigh = ?
                  Gadget = ButtonGadget(#PB_Any, X, Y, width, height)
                  
                  If Not(Gadget)
                        ; it's an error, write you own error code here
                        ; ProcedureReturn ???
                  EndIf
                  
                  My_Button.person(Str(_EncodeButton_Id(row, col)))\ButtonGadget_Id = Gadget
                  ; theses field must be filled up  (here ?)
                  My_Button.person(Str(_EncodeButton_Id(row, col)))\city = "???"
                  My_Button.person(Str(_EncodeButton_Id(row, col)))\node_name = "???"
                  My_Button.person(Str(_EncodeButton_Id(row, col)))\IP_address = "???"
                  
                  
            Next
      Next
EndProcedure

To access a button :

example row = 5, col = 3

If Not(FindMapElement(My_Button(), Str(_EncodeButton_Id(5, 3))))
      ; button doesn't exist inside MAP, it's an error
EndIf

If FindMapElement() is OK, you access directly To Data because the Map element With button row = 5 col = 3 becomes the current element :
            
So you get values like this
            
      My_Button()\ButtonGadget_Id.i
      My_Button()\city.s
      My_Button()\node_name.s
      My_Button()\IP_address.s


MAP has 84 elements if all is Ok (14 * 6)

hope it will help you
A+
Denis
User avatar
jacdelad
Addict
Addict
Posts: 1991
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Coding Question - Use of variables in Purevision Gadget name

Post by jacdelad »

You can also use SetGadgetData/GetGadgetData to set a value to each button which indicates whether to handle it or not.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
calypso
User
User
Posts: 17
Joined: Sun Mar 26, 2023 7:23 am
Location: Vancouver BC Canada

Re: Coding Question - Use of variables in Purevision Gadget name

Post by calypso »

After reviewing all of the replies and trying different methods, my old brain decided on this which I am sharing.
It might not be the correct method but I find it is the simplest for me to understand and maintain.

Form is created by PureVision.
I have 7 columns on each of 14 rows, each has a button used to launch an external application and pass a variable value to the external application.
That's 98 buttons on which to set the font name, size, and type (bold) as well as the color and the caption text.

I have a procedure that gets the gadget number assigned by PB into an array(row,column)
e.g.

Code: Select all

nbg(row,col) = #Gadget_Launch_Form1_Button_Row14_Col6
I do this for all the buttons, then once I have all the gadget numbers in an array i use a few lines (shown below) to set things up.
At least it works for me and it reduces the lines of repetitive code. 98 Buttons!

Code: Select all

Procedure SetNodeGadgetButtons(Array Array$(3), Array nbg(2), Array nsg(2))
  For Row =  1 To #Maxrow        
    For Col = 1 To #MaxCol
      tmp.s = Array$(Row,Col,1)                                                          ;need to hide unused node buttons
      If Len(tmp.s) < 3
        HideGadget(nsg(Row,Col),1)                                                       ;hide LED string box
        HideGadget(nbg(Row,Col),1)                                                       ;hide button with no assignment
      Else  
        SetGadgetColor(nsg(Row,Col),#PB_Gadget_BackColor, #FormBackColor)              ;string gadget background same as form
        SetGadgetFont(nbg(Row,Col),LoadFont(nbg(Row,Col),"Calibri",11,#PB_Font_Bold))  ;use Calibri font bold for gadget
        SetGadgetText(nbg(Row,Col), Array$(Row,Col,1))        
      EndIf
    Next Col
  Next Row    
EndProcedure
On the top left corner of each button I have a string gadget (5x4) with no frame that I change background colors on to show if it is reachable (green)
on the local network or not (red). I thought it look kind of nice. :)
I was going to upload a screen cap but I don't know how to do that on this forum.
Post Reply