ListBox question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kronecker@yahoo.co.uk

    #16
    Re: ListBox question

    On May 5, 3:26 pm, "Steve Gerrard" <mynameh...@com cast.netwrote:
    kronec...@yahoo .co.uk wrote:
    On May 5, 12:24 pm, "Steve Gerrard" <mynameh...@com cast.netwrote:
    >
    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?
    >
    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(ByVa l sender As Object, ByVal e As System.EventArg s)
    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("Hel lo, 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(ByVa l sender As System.Object, ByVal e As
    System.EventArg s) 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(B yVal sender As System.Object, ByVal e
    As System.EventArg s) Handles Button1.Click

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

    End Sub
    End Class

    Comment

    Working...