ListBox not showing items

  • Thread starter Thread starter koosh34
  • Start date Start date
K

koosh34

VS2005 VB.net 2.0
I have a form called options with a ListBox1 on it, to fill the list box
from several events on the form I put the code to update the ListBox1 items
into a sub in a separate code module.

Private Sub options_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
updateOptionsListView()
End Sub

And in the seperate code module

Public Sub updateOptionsListView()
Dim form As New options()
form.ListBox1.Items.Add(System.IO.Path.GetFileNameWithoutExtension("test"))
End Sub

If the line
"Me.ListBox1.Items.Add(System.IO.Path.GetFileNameWithoutExtension("test"))"
is placed in the options_load sub it works but when I call it from the
separate code module it does not. I think this is because another instance
of the options form is being updated.
I also tried

Public Sub updateOptionsListView()
options.ListBox1.Items.Add(System.IO.Path.GetFileNameWithoutExtension("test"))
End Sub

But I get a warning "Reference to a non-shared member requires an object
reference" and the ListBox does not update either.
How do I get around this problem?
 
You may try this -

Dim frm As options
Dim fn As Form

For Each fn In My.Application.OpenForms
If TypeOf fn Is options Then
frm = DirectCast(fn, options)
frm.ListBox1.Items.Add(System.IO.Path.GetFileNameWithoutExtension("test"))
frm = Nothing
End If
Next
fn = Nothing
 
koosh34,

One option would be to put the sub that updates the listbox in the form's
code, not a separate module.

Another option would be to send the listbox to be updated to the
subprocedure as an argument:

Public Sub updateOptionsListView(ByVal lb as ListBox)

lb.Items.Add(System.IO.Path.GetFileNameWithoutExtension("test"))

End Sub

Kerry Moorman
 
Back
Top