Add Index to Combo Box List Items

New to Gambas? Post your questions here. No question is too silly or too simple.
Post Reply
tailkinker
Posts: 10
Joined: Tuesday 17th September 2024 11:06am

Add Index to Combo Box List Items

Post by tailkinker »

I am trying to create a program where, depending on the item selected in a combo box, a certain number of checkboxes appear in an hpanel. How do I add the optional index value to each combo box list item? I created the list in the properties dialog, but don't see a way to add the index value.

My list is something like:

5 Ingredient Recipe
6 Ingredient Recipe
7 Ingredient Recipe

So if I click 7 Ingredient Recipe in the combo box at run time, I want to give me 7 checkboxes with various labels in the hpanel. The problem is no matter what I click it just gives me the first option - 5 checkboxes.

I am using a case statement that is

Select case numingredients.index

Case 1

dispcheck = 5

Case 2

dispcheck = 6

Etc

End select
tailkinker
Posts: 10
Joined: Tuesday 17th September 2024 11:06am

Re: Add Index to Combo Box List Items

Post by tailkinker »

PS x 2

Update - solved it by setting the case statement starting with 0 for the first item in the list.

I will leave this here in case anyone else has the same quandary as well as my observation below.

Thx.

PS - just kind of found an answer.

The first item in the list seems to be throwing all the others off. So it was returning zero, and then each one was displaying the number of check boxes for the item above. Not sure why it's doing that, but when I added "Select from one option below" in the list in properties, all the other ones come up with the right number of checkboxes.

It works, but now I want to know if there is a neater solution.

Thx.
User avatar
cogier
Site Admin
Posts: 1181
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Add Index to Combo Box List Items

Post by cogier »

Well done on fixing it. I came up with this:-

' Gambas class file

ComboBox1 As ComboBox
CheckBox1 As CheckBox
CheckBoxes As New CheckBox[]
HBox1 As HBox
HBox2 As HBox
bReturn As Boolean = True

Public Sub Form_Open()
  
  BuildForm
  
End

Public Sub ComboBox1_Change()
  
  Dim iLoop As Integer
  
  If bReturn = True Then Return 
  
  For iLoop = 0 To 5
    CheckBoxes[iLoop].Visible = False
  Next
  
  For iLoop = 0 To ComboBox1.Index - 1
    CheckBoxes[iLoop].Visible = True
  Next
  
End

Public Sub BuildForm()
  
  Dim iLoop As Integer
  
  With Me
    .Arrangement = Arrange.Vertical
    .Padding = 5
    .H = 350
    .W = 1000
  End With
  
  HBox1 = New HBox(Me)
  HBox1.h = 42
  
  With ComboBox1 = New ComboBox(HBox1) As "ComboBox1"
    .List = ["0 x CheckBox", "1 x CheckBox", "2 x CheckBoxs", "3 x CheckBoxs", "4 x CheckBoxs", "5 x CheckBoxs", "6 x CheckBoxs"]
    .Text = .List[0]
    .W = 250
    .Expand = True
  End With
  
  HBox2 = New HBox(Me) As "HBox2"
  HBox2.h = 42
  
  For iLoop = 0 To 5
    With CheckBox1 = New CheckBox(HBox2) As "CheckBoxes"
      .Expand = True
      .Text = "CheckBox " & Str(iLoop + 1)
      .Visible = False
    End With
    CheckBoxes.Add(CheckBox1)
  Next
  
  bReturn = False
  
End
User avatar
BruceSteers
Posts: 2001
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Add Index to Combo Box List Items

Post by BruceSteers »

Hi , welcome to the wonderful world of programming where counting generally starts at zero not one :)

For ComboBox.Index (and most other things) the first item is always 0 not 1

For a neater solution maybe rather than a Select block you can just add the Index value?

So instead of...
Select numingredients.Index
Case 0
dispcheck = 5
Case 1
dispcheck = 6
and so on

Just do this...

dispcheck = numingredients.index + 5
If at first you don't succeed , try doing something differently.
BruceS
tailkinker
Posts: 10
Joined: Tuesday 17th September 2024 11:06am

Re: Add Index to Combo Box List Items

Post by tailkinker »

Bruce -

That zero vs one thing used to snag me quite a bit with arrays in VB, especially with applications where my counters were more logical starting at 1.

Should have realized that this list was an array and would likely start at zero.

I will get this eventually!

Lucille
Post Reply