Click here to Skip to main content
15,898,820 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have written the following code, which is supposed to request source code from a webpage with Visual Basic, and return matched text in a ListBox, using regular expressions. But I am getting the following error message:

"An unhandled exception of type 'System.ArgumentException' occurred in System.Windows.Forms.dll

Additional information: Items collection cannot be modified when the DataSource property is set."


It relates to this line of code:

VB
ListBox1.Items.Add(itemcode.Value.Split("><").GetValue(0))


The entire code is as follows:

VB
Imports System.Text.RegularExpressions
Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("http://oxford.craigslist.co.uk/search/hhh")
        Dim response As System.Net.HttpWebResponse = request.GetResponse

        Dim sr As System.IO.StreamReader = New System.IO.StreamReader(response.GetResponseStream())

        Dim rssourcecode As String = sr.ReadToEnd
        Dim r As New System.Text.RegularExpressions.Regex("class=""hdrlnk"">.*?<")
        Dim matches As MatchCollection = r.Matches(rssourcecode)

        For Each itemcode As Match In matches
            ListBox1.Items.Add(itemcode.Value.Split("><").GetValue(0))

        Next


    End Sub
End Class



As suggestions as to what may be going wrong here is much appreciated. (Of course, it could be that the regular expression does not match - suggestions for how to quickly test regular expressions against text, would also be much appreciated)
Posted

1 solution

Seems you should remove ListBox1.DataSource[^].

VB
ListBox1.DataSource = Nothing
'your code here
 
Share this answer
 
Comments
Enigma1001 25-Aug-15 6:33am    
Thanks for your suggestion - please can you expand on this? I want to be adding all the matched elements to listbox. What would be a best alternative approach to ListBox1.DataSource[^]?
Maciej Los 25-Aug-15 6:55am    
You don't get me... You're doing it right, but you have to set DataSource to Nothing before you start adding matched values.
Enigma1001 25-Aug-15 8:47am    
Got you. Brilliant - it works. Thanks.
Maciej Los 25-Aug-15 8:49am    
You're very welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900