Problem Accessing Controls from class

Spilled

Member
Joined
Nov 28, 2008
Messages
17
Location
USA
Programming Experience
Beginner
Hello all i am having a problem accessing my controls from my class. I did quite a bit of research and I'm using delegates but i still seem to be having a problem.

When I run my program first thing i do is load a txt file into my listbox "lbWordlist" Here: (form1.vb)
VB.NET:
    Private Sub btnLoadWord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadWord.Click
        lbWordlist.Items.Clear()
        With OpenFileDialog1
            .Title = "Select Your WordList..."
            .Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
            If .ShowDialog() = Windows.Forms.DialogResult.OK Then
                lbWordlist.Items.AddRange(Split(My.Computer.FileSystem.ReadAllText(.FileName), vbNewLine))
            End If
            Me.tbWordlist.Maximum = lbWordlist.Items.Count
        End With
    End Sub
At the bottom of that function I insert a breakpoint and tbWordlist.Max is set to 3407 (tbWordlist is my trackbar to keep track of my position in my wordlist).

Also in my form1.vb is the public get property i have here:
VB.NET:
Delegate Function GetComboCallBack() As String
VB.NET:
    Public ReadOnly Property GetCombo() As String
        Get
            Dim d As New GetComboCallBack(AddressOf GetTheCombo)
            Return d.Invoke()
        End Get
    End Property
Here i use a function delegate (otherwise crossthreading error) And here is the function.
VB.NET:
    Public Function GetTheCombo() As String
        Dim tmp As String
        tmp = lbWordlist.Items.Item(tbWordlist.Value - 1)
        If tbWordlist.Value = tbWordlist.Maximum Then
            StopBots = True
        Else
            tbWordlist.Value += 1
        End If
        Return tmp
    End Function
Now I get an error at:
tmp = lbWordlist.Items.Item(tbWordlist.Value - 1)
in here saying
argumentoutofrangeexception
"InvalidArgument=Value of '0' is not valid for 'index'."
when there is a listcount of 3047 and also my tbWordlist.maximum is set back to 1 when it should be 3047 because i just loaded the wordlist into it and thats what the count was on my previous breakpoint in btnLoadWord.
Now is there anyone that can help me out here? Sorry i tried to make my problem as clear as possible. Thanks in advance
 
If you are crossthreading you must use control.invoke(delegate) to invoke the method on control thread, not delegate.invoke (this is same as calling method directly).
 
If you are crossthreading you must use control.invoke(delegate) to invoke the method on control thread, not delegate.invoke (this is same as calling method directly).

Ok I tried what you said and I get the error
"Invoke or BeginInvoke cannot be called on a control until the window handle has been created."
On this line: Return tbWordlist.Invoke(d)
VB.NET:
    Public ReadOnly Property GetCombo() As String
        Get
            Dim d As New GetComboCallBack(AddressOf GetTheCombo)
            Return tbWordlist.Invoke(d)
        End Get
    End Property
    Public Function GetTheCombo() As String
        Dim tmp As String
        tmp = lbWordlist.Items.Item(tbWordlist.Value - 1)
        If tbWordlist.Value = tbWordlist.Maximum Then
            StopBots = True
        Else
            tbWordlist.Value += 1
        End If
        Return tmp
    End Function

Can you please explain what im doing wrong? and thank you very much for your help so far
 
Ok I've been playing around with this a little and here is what i just found out
VB.NET:
    Public ReadOnly Property GetCombo() As String
        Get
            If lbWordlist.InvokeRequired Then
                Return GetTheCombo()
            ElseIf tbWordlist.InvokeRequired Then
                Return GetTheCombo()
            Else
                Return GetTheCombo()
            End If
        End Get
    End Property
    Public Function GetTheCombo() As String
        Dim tmp As String
        tmp = lbWordlist.Items.Item(tbWordlist.Value - 1)
        If tbWordlist.Value = tbWordlist.Maximum Then
            StopBots = True
        Else
            tbWordlist.Value += 1
        End If
        Return tmp
    End Function
Both of the controls.invokerequired are false but my problem still exists... im getting an outofrange error when i access lbWordlist.items.item(...
and my tbWordlist.Maximum is back at 1 when i load my txt into lbWordlist i stop and check the values and they are correct. It's like im working with a different control. Does anyone know why it would be doing this?
 
When is your code called?
 
When is your code called?

Public ReadOnly Property GetCombo() As String

Is called from my class "BotClass" where my multithreading is sent. When the user clicks a button an array of threads are sent to BotClass.Begin() and they call
Public ReadOnly Property GetCombo() As String
to get the next combo which are loaded in Form1's control lbWordlist

edit: typo sorry
 
I think you are referencing the wrong Form1 instance, probably because you are using the default instance. Default form instances are thread specific so from other threads a new instance is created and returned to you. You should pass the form instance reference to the BotClass and use this to invoke.
 
Back
Top