ListBox question

K

kronecker

Is it possible to address a listbox in a general manner depending on
it's number

eg

ListBox1.
ListBox2.

etc

so we have ListBoxi where i can be 1,2,3,4 etc

I suppose it is like having an array of listboxes.

k.
 
C

Cor Ligthert[MVP]

Hi,
ListBox1.
ListBox2.

etc

so we have ListBoxi where i can be 1,2,3,4 etc

I suppose it is like having an array of listboxes.

Yes and if you really want to use 1,2,3,4 then

\\\
Dim LitstBoxArray as Listbox = {nothing, ListBox1, ListBox2, ListBox3,
ListBox4}
///

Cor
 
K

kronecker

Hi,



Yes and if you really want to use 1,2,3,4 then

\\\
Dim LitstBoxArray as Listbox = {nothing, ListBox1, ListBox2, ListBox3,
ListBox4}
///

Cor

That's brilliant but I am getting an error

Error 1 Array initializers are valid only for arrays, but the type of
'ListBoxArray' is 'System.Windows.Forms.ListBox'

K.
 
T

Tom Shelton

That's brilliant but I am getting an error

Error 1 Array initializers are valid only for arrays, but the type of
'ListBoxArray' is 'System.Windows.Forms.ListBox'

K.

He had a small syntax error :) It should be:

Dim ListBoxArray() As ListBox = {Nothing, ListBox1, ListBox2, ListBox3}
 
K

kronecker

He had a small syntax error :) It should be:

Dim ListBoxArray() As ListBox = {Nothing, ListBox1, ListBox2, ListBox3}

That works but how do I use it?

I tried LisBoxarray(1) and 2 etc to no evail.
I am really looking to adress it like
ListBoxi.extensions

K.
 
C

Cor Ligthert[MVP]

He had a small syntax error :) It should be:

Dim ListBoxArray() As ListBox = {Nothing, ListBox1, ListBox2, ListBox3}
For 4

Dim ListBoxArray() as Listbox = {Nothing, ListBox1, ListBox2, ListBox3,
ListBox4}

:)

Thanks,

Cor
 
C

Cor Ligthert[MVP]

I tried LisBoxarray(1) and 2 etc to no evail.

Probably as you want the Text property

ListBoxArray(1).Text

Cor
 
K

kronecker

Probably as you want the Text property

ListBoxArray(1).Text

Cor

Ok so now I have put

Dim i As Integer
For i = 1 To 10
ListBoxArray(1).Items.Add(i)
Next

to add the numbers 1 to 10 to ListBox1 and it comes up with an error
at runtime saying that


System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."

From which I gather I need to have a dim something as new
something....but what?!!

k.
 
K

kronecker

Ok so now I have put

Dim i As Integer
For i = 1 To 10
ListBoxArray(1).Items.Add(i)
Next

to add the numbers 1 to 10 to ListBox1 and it comes up with an error
at runtime saying that

System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."

From which I gather I need to have a dim something as new
something....but what?!!

k.

I tried this

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button4.Click
Dim i, j As Integer

For i = 1 To 2
ListBoxArray(i) = New ListBox
'Populate the dataset
For j = 1 To 10
ListBoxArray(i).Items.Add(j)

Next
Next


End Sub

so that when I press the switch it is supposed to fill both text
boxes. In fact the error now is gone but nothing happens!!

K.
 
H

Herfried K. Wagner [MVP]

I tried this

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button4.Click
Dim i, j As Integer

For i = 1 To 2
ListBoxArray(i) = New ListBox
'Populate the dataset
For j = 1 To 10
ListBoxArray(i).Items.Add(j)

Next
Next


End Sub

so that when I press the switch it is supposed to fill both text
boxes. In fact the error now is gone but nothing happens!!

The code above creates new listbox controls and populates them with items.
The listboxes are however never added to the form's or container control's
'Controls' collection.
 
C

Cor Ligthert[MVP]

Hi,

We assumed that you had already created your textboxes.
As it are new textboxes then it will be.

\\\
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) _
Handles MyBase.Load
Dim listBoxArray() As ListBox = _
{Nothing, New ListBox, New ListBox, New ListBox, New ListBox, New
ListBox}
listBoxArray(1).Items.Add("1")
For i = 1 To 4
Controls.Add(listBoxArray(i))
listBoxArray(i + 1).Top = listBoxArray(i).Top +
listBoxArray(i).Height
Next
'Because that I am a little bit lazy I created one extra dummy
listbox
End Sub
////

Hi Tom as you read this, this time checked

:)

Cor
 
K

kronecker

Hi,

We assumed that you had already created your textboxes.
As it are new textboxes then it will be.

\\\
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) _
Handles MyBase.Load
Dim listBoxArray() As ListBox = _
{Nothing, New ListBox, New ListBox, New ListBox, New ListBox, New
ListBox}
listBoxArray(1).Items.Add("1")
For i = 1 To 4
Controls.Add(listBoxArray(i))
listBoxArray(i + 1).Top = listBoxArray(i).Top +
listBoxArray(i).Height
Next
'Because that I am a little bit lazy I created one extra dummy
listbox
End Sub
////

Hi Tom as you read this, this time checked

:)

Cor

You know that is brilliant - it works great. How do I get more
control over the list boxes though instead of them on top of one
another -say side by side with a space?

K.
 
K

kronecker

You don't have to use new listboxes added during form load. You can do the same
thing with listboxes you have already placed on the form at design time:

Dim listBoxArray() As ListBox = _
{Nothing, ListBox1, ListBox2, ListBox3, ListBox4, ListBox5}

Great - but what do I do with the ones that it creates? Do I make them
invisible?

K.
 
K

kronecker

Sorry. I see that my suggestion was already made a few posts back.

You don't seem to understand scope. If you declare an array inside of a sub, it
will only be useable inside of that sub.

You should have an array declared at the top of the form code, before any
procedures:

Private ListBoxArray() As ListBox

Then you should put your listboxes on the form, laying them out as desired.
Assume they are named ListBox1, ListBox2, etc.

Then you put code in the Load event:

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
ListBoxArray = New ListBox() {Nothing, ListBox1, ListBox2}
End Sub

Now code in other subs in the same form can use
ListBoxArray(1).Items.Add("Hello, World")

Thank you so much. It works great. The code on the "special custom"
listbox array was a bit of a red herring really and threw me off the
track.

k.
(here it is)

Public Class Form1

Private ListBoxArray() As ListBox


Dim i, j As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

' Array of ListBoxes must be defined in the Load section
ListBoxArray = New ListBox() {Nothing, ListBox1, ListBox2}





End Sub




Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click

For j = 1 To 2
For i = 1 To 10
listBoxArray(j).Items.Add(i)
Next
Next

End Sub
End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top