Dynamically populate Listbox

M

Marc Claasen

Hi there,

I try to add items to a listbox on a form by populating the
listbox from a class.

Somehow the items are added (items.count = 1 after the first addition), but
I cannot
get them visible. Whereas when I create a button on the form which adds two
items,
the both appear visible in the listbox.

What am I doing wrong?

Thanks for your help.

Marc Claasen
 
S

Sagaert Johan

Use the designer to populate the control ,
then look add the code the form designer has generated, that might help you.

Its a trick i often use too.
Johan
 
M

Marc Claasen

Hi there,

I try to add items to a listbox on a form by populating the
listbox from a class.

Somehow the items are added (items.count = 1 after the first addition), but
I cannot
get them visible. Whereas when I create a button on the form which adds two
items,
the both appear visible in the listbox.

What am I doing wrong?

I included some sample code

'***************************************************
In my form I created a button (button3)

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click

Me.ListBox1.Items.Add("ONE")
Me.ListBox1.Items.Add("TWO")

End Sub

***** WORKS FINE *****

Also on the form:

Public Sub AddListboxItem(ByVal sender As System.Object, ByVal e As
System.EventArgs)

Me.ListBox1.Items.Add("THREE")

End Sub

This method is called from within another class like this:

Me.MainForm.Invoke(New EventHandler(AddressOf Me.MainForm.AddListboxItem))

The problem is: the routine is called OK and it adds the items to the
listbox, but they are not shown in the form.

Can anyone please help me with this problem

Marc Claasen
 
W

William Ryan eMVP

Marc:

Call a Application.DoEvents after your done adding them, you may just need
it to cycle through the message pump.
 
M

Marc Claasen

I already did this, but it doesn't help


William Ryan eMVP said:
Marc:

Call a Application.DoEvents after your done adding them, you may just need
it to cycle through the message pump.
 
W

William Ryan eMVP

Marc:

I've taken the code you posted, created a form with two controls (button_3
and ListBox1) and although I'm guessnig there's a button1 and button2, I
doubt they have any bearing to this. Here's the code I used and it's
working fine - when the form loads, Three is visible in the listbox, when
you click on Button3, "One" and "Two" appear. I know you mention that it's
called from a different class, but in and of itself, I can't recreate the
problem. Can you show me the invocation using the class b/c even if I
create a stub and use the same logic, it's still working.

HTH,

Bill

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

Me.Invoke(New EventHandler(AddressOf Me.AddListboxItem))

End Sub

Public Sub AddListboxItem(ByVal sender As System.Object, ByVal e As
System.EventArgs)

Me.ListBox1.Items.Add("THREE")

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click

Me.ListBox1.Items.Add("ONE")

Me.ListBox1.Items.Add("TWO")

End Sub
 
M

Marc Claasen

William,

i included the call from the other class
Hope you can put me on the right track. I guess this has something to do
with updating a user interface from another class.
But i can't solve it. Maybe you can help.

I have got a main FORM frmMain, with a ListBox1 put on it.
And I have a class ndsClient which responds to socket events (it has got a
socket subclass)
In the dataarrival event from the socket I process the incoming data and
call ProcessAlarm method.
And in the ProcessAlarm method I want to update the form frmMain.

The Pseudo Code

***************************************************************************

Public Class ndsClient

'reference to Main form
Public Shared MainForm As frmMain



Private Sub mProcessAlarm(ByVal isData As String)

Dim sCommand As String

sCommand = isData.Split(New Char() {"|"c})(0).ToString.ToUpper

Select Case sCommand

Case "NEW"



Dim Split As String() = isData.Split(New [Char]() {"|"c})

Dim oFormAlarm As frmAlarmMelding = New frmAlarmMelding

oFormAlarm.Guid = Split(3)

oFormAlarm.msObject = Split(4)

oFormAlarm.msType = Split(5)

Functions.Play("\windows\alarm1.wav")

If oFormAlarm.ShowDialog = DialogResult.OK Then

oFormAlarm.Close()

oFormAlarm = Nothing

Dim oFormMain As New frmMain

Dim oListbox As New ListBox

Me.MainForm = oFormMain

oListbox = Me.MainForm.ListBox1

oListbox.Items.Add(split(4).ToString)


oListbox.Invoke(New EventHandler(AddressOf
Me.MainForm.AddListboxItem))

oListbox.Refresh()

oListbox.Update()

MessageBox.Show(Me.MainForm.ListBox1.Items(0).ToString)

oFormMain = Nothing

Cursor.Current = Cursors.WaitCursor

Application.DoEvents()

Me.Send("ALARM>ACCEPT|" & Split(3) & "<")

'**** einde testcode

Else

Me.Send("ALARM>DECLINE|" & Split(4) & "<")

End If


End Select


End Sub

End Class



***************************************************************************

Hopefully you can help.

Marc
 

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