Add Index to Combo Box List Items

Post

Posted
Rating:
#1 (In Topic #1407)
Regular
tailkinker is in the usergroup ‘Regular’
 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
Online now: No Back to the top

Post

Posted
Rating:
#2
Regular
tailkinker is in the usergroup ‘Regular’
 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.
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Guru
cogier is in the usergroup ‘Guru’
Well done on fixing it. I came up with this:-

Code (gambas)

  1. ' Gambas class file
  2.  
  3. ComboBox1 As ComboBox
  4. CheckBox1 As CheckBox
  5. CheckBoxes As New CheckBox[]
  6. HBox1 As HBox
  7. HBox2 As HBox
  8. bReturn As Boolean = True
  9.  
  10. Public Sub Form_Open()
  11.  
  12.   BuildForm
  13.  
  14.  
  15. Public Sub ComboBox1_Change()
  16.  
  17.   Dim iLoop As Integer
  18.  
  19.   If bReturn = True Then Return
  20.  
  21.   For iLoop = 0 To 5
  22.     CheckBoxes[iLoop].Visible = False
  23.   Next
  24.  
  25.   For iLoop = 0 To ComboBox1.Index - 1
  26.     CheckBoxes[iLoop].Visible = True
  27.   Next
  28.  
  29.  
  30. Public Sub BuildForm()
  31.  
  32.   Dim iLoop As Integer
  33.  
  34.   With Me
  35.     .Arrangement = Arrange.Vertical
  36.     .Padding = 5
  37.     .H = 350
  38.     .W = 1000
  39.  
  40.   HBox1 = New HBox(Me)
  41.   HBox1.h = 42
  42.  
  43.   With ComboBox1 = New ComboBox(HBox1) As "ComboBox1"
  44.     .List = ["0 x CheckBox", "1 x CheckBox", "2 x CheckBoxs", "3 x CheckBoxs", "4 x CheckBoxs", "5 x CheckBoxs", "6 x CheckBoxs"]
  45.     .Text = .List[0]
  46.     .W = 250
  47.     .Expand = True
  48.  
  49.   HBox2 = New HBox(Me) As "HBox2"
  50.   HBox2.h = 42
  51.  
  52.   For iLoop = 0 To 5
  53.     With CheckBox1 = New CheckBox(HBox2) As "CheckBoxes"
  54.       .Expand = True
  55.       .Text = "CheckBox " & Str(iLoop + 1)
  56.       .Visible = False
  57.     End With
  58.     CheckBoxes.Add(CheckBox1)
  59.   Next
  60.  
  61.   bReturn = False
  62.  
  63.  
Online now: No Back to the top

Post

Posted
Rating:
#4
Guru
BruceSteers is in the usergroup ‘Guru’
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
Online now: No Back to the top

Post

Posted
Rating:
#5
Regular
tailkinker is in the usergroup ‘Regular’
 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
Online now: No Back to the top
1 guest and 0 members have just viewed this.