# Listbox add multiple rows?

**URL:** <https://forum.xojo.com/t/listbox-add-multiple-rows/11596>\
**Category:** General\
**Created:** [October 12, 2013, 9:03am UTC](https://forum.xojo.com/t/listbox-add-multiple-rows/11596 "2013-10-12T09:03:19Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrew\_Gibbs](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/898d66/32.png) [@Andrew\_Gibbs](https://forum.xojo.com/u/Andrew_Gibbs)\
**Post date:** [October 12, 2013, 9:03am UTC](https://forum.xojo.com/t/listbox-add-multiple-rows/11596/1 "2013-10-12T09:03:19Z")

</div>

Is it possible to add multiple rows to a listbox at the same time?

---

<div class="post-metadata">

**Author:** ![Alex\_von\_Siebenthal](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/c67d28/32.png) [@Alex\_von\_Siebenthal](https://forum.xojo.com/u/Alex_von_Siebenthal)\
**Post date:** [October 12, 2013, 9:33am UTC](https://forum.xojo.com/t/listbox-add-multiple-rows/11596/2 "2013-10-12T09:33:30Z")

</div>

Simply use a loop or multiple AddRow statements?

---

<div class="post-metadata">

**Author:** ![Andrew\_Gibbs](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/898d66/32.png) [@Andrew\_Gibbs](https://forum.xojo.com/u/Andrew_Gibbs)\
**Post date:** [October 12, 2013, 9:37am UTC](https://forum.xojo.com/t/listbox-add-multiple-rows/11596/3 "2013-10-12T09:37:39Z")

</div>

> [@39988:@Alex von Siebenthal](#):
>
> Simply use a loop or multiple AddRow statements?

Thanks.

---

<div class="post-metadata">

**Author:** ![Andrew\_Gibbs](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/898d66/32.png) [@Andrew\_Gibbs](https://forum.xojo.com/u/Andrew_Gibbs)\
**Post date:** [October 12, 2013, 9:48am UTC](https://forum.xojo.com/t/listbox-add-multiple-rows/11596/4 "2013-10-12T09:48:32Z")

</div>

App crashes.

My Code

```auto
  Dim TheRandom As New Random
  Dim a As Integer
  Dim b As Integer
  
  Dim HowMany As Integer = TextField3.Text.Val
  
  Do
    a = TheRandom.InRange(TextField1.Text.Val, TextField2.Text.Val)
    Listbox1.AddRow(Str(a))
  Loop Until b = HowMany
```

---

<div class="post-metadata">

**Author:** ![Paul\_Budd](https://forum.xojo.com/user_avatar/forum.xojo.com/paul_budd/32/799_2.png) [@Paul\_Budd](https://forum.xojo.com/u/Paul_Budd)\
**Post date:** [October 12, 2013, 9:56am UTC](https://forum.xojo.com/t/listbox-add-multiple-rows/11596/5 "2013-10-12T09:56:17Z")

</div>

Try below untested.

Dim TheRandom As New Random  
Dim a As Integer  
Dim b As Integer  
Dim HowMany As Integer = val (TextField3.Text)  
Do a = TheRandom.InRange(val (TextField1.Text), val (TextField2.Text))  
Listbox1.AddRow str(a)  
Loop Until b = HowMany

---

<div class="post-metadata">

**Author:** ![Alex\_von\_Siebenthal](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/c67d28/32.png) [@Alex\_von\_Siebenthal](https://forum.xojo.com/u/Alex_von_Siebenthal)\
**Post date:** [October 12, 2013, 10:16am UTC](https://forum.xojo.com/t/listbox-add-multiple-rows/11596/6 "2013-10-12T10:16:25Z")

</div>

[quote=39991:@Shane Gibbs]App crashes.

My Code

[code]  
Dim TheRandom As New Random  
Dim a As Integer  
Dim b As Integer

Dim HowMany As Integer = TextField3.Text.Val

Do  
a = TheRandom.InRange(TextField1.Text.Val, TextField2.Text.Val)  
Listbox1.AddRow(Str(a))  
Loop Until b = HowMany  
[/code][/quote]

What error? Have you set a break point and stepped through the code?

---

<div class="post-metadata">

**Author:** ![Andrew\_Gibbs](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/898d66/32.png) [@Andrew\_Gibbs](https://forum.xojo.com/u/Andrew_Gibbs)\
**Post date:** [October 12, 2013, 10:24am UTC](https://forum.xojo.com/t/listbox-add-multiple-rows/11596/7 "2013-10-12T10:24:50Z")

</div>

[quote=39992:@Paul Budd]Try below untested.

Dim TheRandom As New Random  
Dim a As Integer  
Dim b As Integer  
Dim HowMany As Integer = val (TextField3.Text)  
Do a = TheRandom.InRange(val (TextField1.Text), val (TextField2.Text))  
Listbox1.AddRow str(a)  
Loop Until b = HowMany[/quote]

That makes sense. I’ll try it out in the later today.

~Night~

---

<div class="post-metadata">

**Author:** ![Antonio\_Rinaldi](https://forum.xojo.com/user_avatar/forum.xojo.com/antonio_rinaldi/32/331_2.png) [@Antonio\_Rinaldi](https://forum.xojo.com/u/Antonio_Rinaldi)\
**Post date:** [October 12, 2013, 10:44am UTC](https://forum.xojo.com/t/listbox-add-multiple-rows/11596/8 "2013-10-12T10:44:59Z")

</div>

[quote=39991:@Shane Gibbs]App crashes.

My Code

[code]  
Dim TheRandom As New Random  
Dim a As Integer  
Dim b As Integer

Dim HowMany As Integer = TextField3.Text.Val

Do  
a = TheRandom.InRange(TextField1.Text.Val, TextField2.Text.Val)  
Listbox1.AddRow(Str(a))  
Loop Until b = HowMany  
[/code][/quote]  
Maybe your loop is missing an update of the b value

`Do
    a = TheRandom.InRange(TextField1.Text.Val, TextField2.Text.Val)
    Listbox1.AddRow(Str(a))
b=b+1
  Loop Until b = HowMany`

---

<div class="post-metadata">

**Author:** ![Andrew\_Gibbs](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/898d66/32.png) [@Andrew\_Gibbs](https://forum.xojo.com/u/Andrew_Gibbs)\
**Post date:** [October 12, 2013, 10:16pm UTC](https://forum.xojo.com/t/listbox-add-multiple-rows/11596/9 "2013-10-12T22:16:15Z")

</div>

[quote=39997:@Antonio Rinaldi]Maybe your loop is missing an update of the b value

`Do
    a = TheRandom.InRange(TextField1.Text.Val, TextField2.Text.Val)
    Listbox1.AddRow(Str(a))
b=b+1
  Loop Until b = HowMany`[/quote]

That works! Thanks!

---

<div class="post-metadata">

**Author:** ![Wayne\_Golding](https://forum.xojo.com/user_avatar/forum.xojo.com/wayne_golding/32/165_2.png) [@Wayne\_Golding](https://forum.xojo.com/u/Wayne_Golding)\
**Post date:** [October 12, 2013, 10:26pm UTC](https://forum.xojo.com/t/listbox-add-multiple-rows/11596/10 "2013-10-12T22:26:09Z")

</div>

If you’re starting with an empty listbox each time you could use the listcount property & do away with b altogether.

---

<div class="post-metadata">

**Author:** ![Andrew\_Gibbs](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/898d66/32.png) [@Andrew\_Gibbs](https://forum.xojo.com/u/Andrew_Gibbs)\
**Post date:** [October 12, 2013, 11:16pm UTC](https://forum.xojo.com/t/listbox-add-multiple-rows/11596/11 "2013-10-12T23:16:32Z")

</div>

> [@40072:@Wayne Golding](#):
>
> If you’re starting with an empty listbox each time you could use the listcount property & do away with b altogether.

Thanks for the code optimization.

---

<div class="post-metadata">

**Author:** ![Merv\_Pate](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/76d3ee/32.png) [@Merv\_Pate](https://forum.xojo.com/u/Merv_Pate)\
**Post date:** [October 13, 2013, 1:29am UTC](https://forum.xojo.com/t/listbox-add-multiple-rows/11596/12 "2013-10-13T01:29:17Z")

</div>

Subclass the ListBox, and put the loop in method, AddRows(n) code looks cleaner.

---

<div class="post-metadata">

**Author:** ![Eric\_de\_La\_Rochette](https://forum.xojo.com/user_avatar/forum.xojo.com/eric_de_la_rochette/32/3604_2.png) [@Eric\_de\_La\_Rochette](https://forum.xojo.com/u/Eric_de_La_Rochette)\
**Post date:** [October 14, 2013, 7:20am UTC](https://forum.xojo.com/t/listbox-add-multiple-rows/11596/13 "2013-10-14T07:20:59Z")

</div>

> [@40077:@Merv Pate](#):
>
> Subclass the ListBox, and put the loop in method, AddRows(n) code looks cleaner.

For things like this, you may consider extending the Listbox instead of subclassing it.

---

<div class="post-metadata">

**Author:** ![Merv\_Pate](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/76d3ee/32.png) [@Merv\_Pate](https://forum.xojo.com/u/Merv_Pate)\
**Post date:** [October 14, 2013, 4:01pm UTC](https://forum.xojo.com/t/listbox-add-multiple-rows/11596/14 "2013-10-14T16:01:10Z")

</div>

I’ve always subclassed ListBoxes, what is the advantage of extending it?

---

<div class="post-metadata">

**Author:** ![Eric\_de\_La\_Rochette](https://forum.xojo.com/user_avatar/forum.xojo.com/eric_de_la_rochette/32/3604_2.png) [@Eric\_de\_La\_Rochette](https://forum.xojo.com/u/Eric_de_La_Rochette)\
**Post date:** [October 14, 2013, 4:23pm UTC](https://forum.xojo.com/t/listbox-add-multiple-rows/11596/15 "2013-10-14T16:23:33Z")

</div>

> [@40311:@Merv Pate](#):
>
> I’ve always subclassed ListBoxes, what is the advantage of extending it?

See the [Extends Keyword page](http://documentation.xojo.com/index.php/Extends) on the Wiki and/or the book 1 of the user guide ( UserGuide-Fundamentals.pdf ) page 124.

---

<div class="post-metadata">

**Author:** ![Merv\_Pate](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/76d3ee/32.png) [@Merv\_Pate](https://forum.xojo.com/u/Merv_Pate)\
**Post date:** [October 14, 2013, 4:54pm UTC](https://forum.xojo.com/t/listbox-add-multiple-rows/11596/16 "2013-10-14T16:54:27Z")

</div>

OK, thanks. I have so much other custom stuff I do with events and properties, I’ve always just subclassed the ListBox. Thanks for the tip!

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 4:53pm UTC](https://forum.xojo.com/t/listbox-add-multiple-rows/11596/17 "2020-10-29T16:53:42Z")

</div>


