Results 1 to 5 of 5

Thread: ListBox Question

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 1999
    Location
    Fort Worth, Texas, USA
    Posts
    264

    ListBox Question

    OK,

    VB6:
    ListBox1.Clear
    ListBox1.AddItem "Apples"
    ListBox1.AddItem "Oranges"

    How do you do this in VB.Net?


    Thanks,
    Ken

  2. #2
    Hyperactive Member Bananafish's Avatar
    Join Date
    Jan 2001
    Posts
    394
    VB Code:
    1. With ListBox1
    2.          .Items.Clear()
    3.          .Items.Add("Apples")
    4.          .Items.Add("Oranges")
    5.       End With

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 1999
    Location
    Fort Worth, Texas, USA
    Posts
    264
    Thanks Bananafish,

    That solved that problem. However, now I don't have access to the ListBox except within the routine where I used the Dim statement.

    Public Sub Template_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim ListBox1 As New ListBox()

    End Sub

    This next routine generates an error!

    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click

    With listbox1
    .Visible = False
    End With

    End Sub

    What am I doing wrong?

    Thanks,
    Ken

  4. #4
    Hyperactive Member Bananafish's Avatar
    Join Date
    Jan 2001
    Posts
    394
    It's a scope problem. The Dim statement makes the variable (in this case a listbox) only available in the sub routine it is declared.

    If you wanted the scope of the listbox to be available to the entire class (a form is also a class) then you would have to declare it at the start. ie

    VB Code:
    1. Public Class YourClass
    2.  
    3.    Private ListBox1 as New ListBox()

    However, If your class is a form is there any reason you aren't adding the Listbox control to the form? you should do this at design time by dragging the ListBox from the ToolBox and on to your form...

    or if you actually wish to do it at run time, you can add it toi the forms control collection...

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 1999
    Location
    Fort Worth, Texas, USA
    Posts
    264
    Duh!!

    Ok,
    I'm tired, I have headache, my back hurts, the Cowboys suck ---

    Thanks,
    Ken

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width