![]() |
Click here to advertise with us
|
|
|||||||
| Windows Forms Discussion related to Winforms application development |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
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) Code:
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
Also in my form1.vb is the public get property i have here: Code:
Delegate Function GetComboCallBack() As String Code:
Public ReadOnly Property GetCombo() As String
Get
Dim d As New GetComboCallBack(AddressOf GetTheCombo)
Return d.Invoke()
End Get
End Property
Code:
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
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).
__________________
Some useful links: Learning videoes, Code Samples, WMI Code Creator, MSDN, The Code Project, WindowsClient.net, ASP.net, W3 Schools, Regular-Expressions.info, GDI+ FAQ
How to format posts with code blocks etc - present the problem/post properly ![]() |
|
|||
|
Quote:
"Invoke or BeginInvoke cannot be called on a control until the window handle has been created." On this line: Return tbWordlist.Invoke(d) Code:
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
|
|
|||
|
Ok I've been playing around with this a little and here is what i just found out
Code:
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
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?
__________________
Some useful links: Learning videoes, Code Samples, WMI Code Creator, MSDN, The Code Project, WindowsClient.net, ASP.net, W3 Schools, Regular-Expressions.info, GDI+ FAQ
How to format posts with code blocks etc - present the problem/post properly ![]() |
|
|||
|
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.
__________________
Some useful links: Learning videoes, Code Samples, WMI Code Creator, MSDN, The Code Project, WindowsClient.net, ASP.net, W3 Schools, Regular-Expressions.info, GDI+ FAQ
How to format posts with code blocks etc - present the problem/post properly ![]() |
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|