PDA

Click to See Complete Forum and Search --> : ListBox Question


Ken Whiteman
Jan 11th, 2002, 10:09 AM
OK,

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

How do you do this in VB.Net?


Thanks,
Ken

Bananafish
Jan 14th, 2002, 04:53 AM
With ListBox1
.Items.Clear()
.Items.Add("Apples")
.Items.Add("Oranges")
End With

Ken Whiteman
Jan 14th, 2002, 02:15 PM
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

Bananafish
Jan 15th, 2002, 05:36 AM
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


Public Class YourClass

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...

Ken Whiteman
Jan 15th, 2002, 06:47 PM
Duh!!

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

Thanks,
Ken