For loop struggles

Post

Posted
Rating:
#1 (In Topic #1254)
Regular
rj71 is in the usergroup ‘Regular’
Hi All,
Despite some years experience with VB and now getting my feet wet with Gambas, I've always struggled with for loops. I don't know why, it just doesn't always click for me.
Say I have a listbox with 200 items in it and I want to do something with that data with a button click but only 5 (exact number is unknown right now) at a time. On top of that, the next button click would pick up after whatever the last item was. So first click of button would be listbox items 1 to 5, second button click would be listbox items 6-10 and so on and so on until the last item of the listbox is reached. Here's what I have so far:

Code (gambas)

  1.   Dim $hButton As Button
  2. For i = 0 To ListBox1.count - 1
  3.   ListBox1.index = i
  4.  
  5. 'dynamically creating buttons with text from items in the listbox (example on here that BruceSteers had but i changed a little to meet my needs - thanks Bruce!)
  6. TextBox1.Text = ListBox1.Current.Text
  7.   With $hButton = New Button(Panel1) As "MyButton"
  8.       '.Text = s
  9.       .width = 150
  10.       .Height = 150
  11.       .Text = ListBox1.Current.Text
  12.       .AutoResize = True
  13.       '.Name = s
  14.       .Name = ListBox1.Current.Text
  15.     End With
  16.  

How would I change that for loop to do what I'm trying here?
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
You'll need two control variables and at the end of your loop add 5 to each:

Code (gambas)

  1. ' Global
  2. Private llimit as integer = 0
  3. Private ulimit as integer = 4
  4. ' button handler
  5. For i as integer = llimit to ulimit
  6.   ' do whatever
  7. llimit+=5
  8. ulimit+=5
(or you could do it with 1 control var and

Code (gambas)

  1. For i as integer = limit to limit+5
)

b

Online now: No Back to the top

Post

Posted
Rating:
#3
Guru
BruceSteers is in the usergroup ‘Guru’
I might just Increment the Index 5 times with each button press,

Code (gambas)

  1.  
  2.   Dim $hButton As Button
  3.  
  4.   ' set index at 0 if nothing is selected
  5.   If ListBox1.Index = -1 Then ListBox1.Index = 0
  6.  
  7.   For i = 0 To 4 ' do this 5 times
  8.  
  9. 'dynamically creating buttons with text from items in the listbox (example on here that BruceSteers had but i changed a little to meet my needs - thanks Bruce!)  (You're welcome :) )
  10.   TextBox1.Text = ListBox1.Current.Text
  11.   With $hButton = New Button(Panel1) As "MyButton"
  12.       '.Text = s
  13.       .width = 150
  14.       .Height = 150
  15.       .Text = ListBox1.Current.Text
  16.       .AutoResize = True
  17.       '.Name = s
  18.       .Name = ListBox1.Current.Text
  19.     End With
  20.  
  21.   If ListBox1.Index = ListBox1.Count-1 Then  ' if at end of list then deselect and exit
  22.    ListBox1.Index = -1
  23.    Break
  24.   Else
  25.     Inc ListBox1.Index  ' or goto next item
  26.  
  27. [code numbers="1" param="gambas" scroll="1"]
Online now: No Back to the top

Post

Posted
Rating:
#4
Regular
rj71 is in the usergroup ‘Regular’
There seems to be a lot of Bruce's on this website  :lol: Thanks to both of you and I'll try both to see what fits best. I'm really grateful for the help I get from the people on this site, I've learned so much!
Online now: No Back to the top

Post

Posted
Rating:
#5
Regular
rj71 is in the usergroup ‘Regular’
 Hey BruceSteers, in your dynamic button creation code, how do I change the font color on the buttons? I  figured out how to set the font size but using .Foreground = white I get unknown identifier error. If this were just a button on the form that foreground= white would have worked. What am I not understanding? Also having trouble setting the buttons with a transparent background (.Background = Transparent throws unknown identifier error)as well as a border around the button. .Border = True doesn't throw an error but I don't see a border on the button.
Online now: No Back to the top

Post

Posted
Rating:
#6
Guru
BruceSteers is in the usergroup ‘Guru’

rj71 said

Hey BruceSteers, in your dynamic button creation code, how do I change the font color on the buttons? I  figured out how to set the font size but using .Foreground = white I get unknown identifier error. If this were just a button on the form that foreground= white would have worked. What am I not understanding? Also having trouble setting the buttons with a transparent background (.Background = Transparent throws unknown identifier error)as well as a border around the button. .Border = True doesn't throw an error but I don't see a border on the button.

All those color related constants are from Color.class

You are correct to use .Foreground but you must use color constants like this…

.Foreground = Color.White
.Background = Color.Transparent

/comp/gb.qt4/color - Gambas Documentation

You can also try direct color codes (hexidecimal RGB) ,  (hint, press Ctrl+Shift+C)
.Foreground = &FF00FF

Or make partially transparent.
.Background = Color.SetAlpha(Color.Blue, 200)

Setting Button1.Border = True "should" render a border.

Note: some object use an integer value for border and can use Border.Plain , Border.Raised, Border.None, Etc.
It also depends on the toolkit/theme being used, some toolkit themes just do not render borders.

You should really see a difference with a button having a border or not.
the default value is true for buttons to have a border , only ToolButton does not have a border by default.

Try setting .Border  = False and see if you see a difference
Online now: No Back to the top

Post

Posted
Rating:
#7
Regular
rj71 is in the usergroup ‘Regular’

BruceSteers said

rj71 said

Hey BruceSteers, in your dynamic button creation code, how do I change the font color on the buttons? I  figured out how to set the font size but using .Foreground = white I get unknown identifier error. If this were just a button on the form that foreground= white would have worked. What am I not understanding? Also having trouble setting the buttons with a transparent background (.Background = Transparent throws unknown identifier error)as well as a border around the button. .Border = True doesn't throw an error but I don't see a border on the button.

All those color related constants are from Color.class

You are correct to use .Foreground but you must use color constants like this…

.Foreground = Color.White
.Background = Color.Transparent

/comp/gb.qt4/color - Gambas Documentation

You can also try direct color codes (hexidecimal RGB) ,  (hint, press Ctrl+Shift+C)
.Foreground = &FF00FF

Or make partially transparent.
.Background = Color.SetAlpha(Color.Blue, 200)

Setting Button1.Border = True "should" render a border.

Note: some object use an integer value for border and can use Border.Plain , Border.Raised, Border.None, Etc.
It also depends on the toolkit/theme being used, some toolkit themes just do not render borders.

You should really see a difference with a button having a border or not.
the default value is true for buttons to have a border , only ToolButton does not have a border by default.

Try setting .Border  = False and see if you see a difference

Thanks Bruce! That works except it seems that sometimes it doesn't apply to all the buttons that get created, some have the white font color and some don't…same for transparent background. I assume there's something else going on in my code so I'll figure it out. Thanks again!
Online now: No Back to the top
1 guest and 0 members have just viewed this.