Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Adding Items from one class to a listbox in another

okay, So i have form1, with listbox1
and class Kws.vb which scrapes web elements from a specified page with a lot of other settings.

I created another class, because I wanted to practice using other threads. and to make multithreading much better.

The problem is this:
Code
Form1.Allkws.Items.Add(allkws(i).ToString)

For some reason it doesn't work. when I instead put msgbox(allkws(i).ToString)
it will spam me with all the elements

What is causing it to not add items across classes?

Form1:
Code
Public Class Form1
    Dim pages As New ListBox
    Public Allkws As New ListBox

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        Dim Thread1 As System.Threading.Thread = New System.Threading.Thread(AddressOf quickStart)
        Thread1.Start()
        'quickStart()

    End Sub

    Sub quickStart()
        Allkws = New ListBox
        For Each item In GetList(TextBox1.Text).Items
            Dim kws As New Kws
            kws.Page = item
            ThreadPool.QueueUserWorkItem(AddressOf kws.getKws)
        Next

        waitTillThreadPoolisIdle()
        MsgBox(Allkws.Items.Count.ToString)

    End Sub

Form2:
Code
If allstuff.Contains(", ") Then
            Dim allkws As String() = allstuff.Split(", ")
            Dim i As Integer = 0
            While i <= allstuff.Split(", ").Length - 1
                Form1.Allkws.Items.Add(allkws(i).ToString)
                i = i + 1
            End While
        End If

Please help
I believe you're declaring the wrong memory, although it's hard to tell because I'm on my iPhone, but I think that's the case. I can only help you out more when I get home from work in 5 hours.
I would recommend using a for loop instead of a while here if you really want, but I would use AddRange instead of Add.
Also, you just declared a variable for allstuf.split and then you call that again?
just do
Code
Dim allkws As String() = allstuf.split(", ")
Form1.Allkws.Items.AddRange(allkws)
I just recreated everything of form Form2.Show() http://prntscr.com/9hxjzw
I think your "Problem" is actually showing it in a listbox visible on Form1
Also it newer is shown how Form2 is started.

This may seem like an attempt to confuse you. But here is an other way of doing it.
http://prntscr.com/9hxp21


For later, a "for loop" will be more tidier looking and will function quicker
http://prntscr.com/9hxlyk
(Dec 23, 2015 10:17 PM)Moen Wrote:  I just recreated everything of form Form2.Show() -cant post links yet-
I think your "Problem" is actually showing it in a listbox visible on Form1
Also it newer is shown how Form2 is started.

This may seem like an attempt to confuse you. But here is an other way of doing it.
-cant post links yet-


For later, a "for loop" will be more tidier looking and will function quicker
-cant post links yet-

For some reason it really bothers me that you have a variable named 'ThisIsAArray' while it should be 'ThisIsAnArray'.
(Dec 24, 2015 02:06 PM)Programming Enthousiast Wrote:  For some reason it really bothers me that you have a variable named 'ThisIsAArray' while it should be 'ThisIsAnArray'.

Must be cause you are a Programming Enthousiast 😏


Also OP when you do this Code:

Form1.Allkws.Items.Add(allkws(i).ToString)

If "allkws(i)" Is more than 1 item you should use AddRange instead off Add.
🤝
Keep your classes independent of your forms. Have it return a collection and populate your listbox back on the main thread.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by lockheadApr 22, 2021 07:10 AM 0 147 Apr 22, 2021 07:10 AM
Last Post: lockhead
[.NET] by GhostenigmaJun 12, 2019 05:52 AM 5 226 Jun 12, 2019 05:52 AM
Last Post: HFDone
[.NET] by System[X]Apr 2, 2019 04:27 PM 17 688 Apr 2, 2019 04:27 PM
Last Post: Aristocles