can not display in listbox in button1_click routine...

dtvonly

Member
Joined
Jan 3, 2013
Messages
22
Programming Experience
5-10
VB.NET:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.ListBox1 = New System.Windows.Forms.ListBox
        ListBox1.Items.Add("in button routine")
    End Sub

I know that my listbox1 is in a different thread. That's why I created a new instance. That still did not work. How do I use listbox, textbox, etc from a subroutine?
 
Your ListBox is not in a different thread. Get rid of the first line. The second line is all you need.

Even if your ListBox was in a different thread, how could creating a new instance help? If you had a piece of paper and you then got a new piece of paper and wrote on it, would you expect that writing to magically appear on the first piece of paper?
 
thanks for the advice. that worked. Here is a cross-threading issue involving textbox, listbox, etc.

Private Sub DataReceivedHandler(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
Dim sp As SerialPort = CType(sender, SerialPort)
Dim indata As String = sp.ReadExisting()
Dim Serial_RX As String
Me.ListBox1 = New System.Windows.Forms.ListBox
ListBox1.Items.Add("in ISR")
ListBox1.Items.Add(indata)
Serial_RX = indata
End Sub

I got rid of the Me.ListBox1 = New System.Windows.Forms.ListBox and that got an error of cross threading.
Please advise on how to gain access to textboxes, listboxes, from serialport received interrupt routine. thanks.
 
This is a different kettle of fish, because the DataReceived event of a SerialPort is raised on a secondary thread, while a Button Click event is not. Also, it's not that your ListBox exists in a different thread because all data exists in all threads. The issue is that accessing the handle of a control on a thread that didn't create that handle is illegal. The solution is to marshal a method call back to the thread that did create that handle and access it there. Here's one I prepared earlier:

Accessing Controls from Worker Threads
 
Back
Top