将数字从整数列表添加到列表框

时间:2016-03-31 12:40:23

标签: regex vb.net listbox

我目前在尝试获取要在列表框中显示的整数列表时遇到问题。 我有更多的数字要显示,但是我甚至无法显示一个数字。 没有错误,列表框但显示此 Dim URL = New Uri("http://www.hurriyet.com.tr/sans-oyunlari/sans-topu-sonuclari/") Dim WebClient As New HttpClient Dim Source = Await WebClient.GetStringAsync(URL) Dim ListofNumber As List(Of Integer) ListofNumber = New List(Of Integer) Dim WebCode1 As String = "<span id=""_ctl0_ContentPlaceHolder1_lblresutone"" class=""hurriyet2010_so_sanstopu_no_text"">([^>]*)</span></div>" For Each item As Match In (New Regex(WebCode1)).Matches(Source) ListofNumber.Add(item.Groups(1).Value) Next listBox1.Items.Add(ListofNumber)

{
name: "thing1",
gadgets:
[
{
    name: "gadget1",
    components: [
        {
            name: "component1"
            properties: 
                [
                    {id: "I1", value: "V1"}, {id: "I2", value: "V2"},
                    {id: "I3", value: "V3"}, {id: "I4", value: "V2"},...
                ]
        },
        {
            name: "component2"
            properties: 
                [
                    {id: "I1", value: "V1"}, {id: "I2", value: "V4"},
                    {id: "I3", value: "V3"}, {id: "I4", value: "V5"},...
                ]
        }
    ]
},
{
    name: "gadget2",
    components: [
        {
            name: "component1"
            properties: 
                [
                    {id: "I1", value: "V1"}, {id: "I2", value: "V3"},
                    {id: "I3", value: "V3"}, {id: "I4", value: "V2"},...
                ]
        },
        {
            name: "component2"
            properties: 
                [
                    {id: "I1", value: "V1"}, {id: "I2", value: "V7"},
                    {id: "I3", value: "V1"}, {id: "I4", value: "V2"},...
                ]
        }
    ]
}
]
}

3 个答案:

答案 0 :(得分:0)

而不是:

listBox1.Items.Add(ListofNumber)

......应该是:

listBox1.DataSource = ListofNumber

这样您就可以将对象列表(在您的情况下为ListofNumber)绑定到listBox

事实上,您可以绑定任何类型的列表,listBox中显示的结果将是每个项目的.ToString()(在您的情况下为int.ToString(),其中是数字的字符串。

绑定数据源的替代方法是:listBox1.Items.Clear(),然后通过listBox1.Items.Add(yourItem)逐个添加您的项目,或者listBox1.Items.AddRange(ListofNumber)添加您的项目。

答案 1 :(得分:0)

目前,您正在向列表添加单个项目,即List(Of Integer)个对象。您需要单独添加列表中的每个项目,如下所示:

For Each i As Integer In ListOfNumber
    listBox1.Items.Add(i)
Next

或者更简单:

listBox1.Items.AddRange(ListOfNumber)

正如评论中已经提到的那样,但是当你解析HTML时,正则表达式通常是错误的工具。在大多数情况下,最好使用HTML解析器/ DOM。

答案 2 :(得分:0)

我认为问题在于WebCode1上的ID,因为ID会被使用一次,而下载源就是这种情况。

请试试这个

enter image description here

Dim URL = New Uri("http://www.hurriyet.com.tr/sans-oyunlari/sans-topu-sonuclari/")
Dim WebClient As New HttpClient
Dim Source = Await WebClient.GetStringAsync(URL)
Dim WebCode1 As String = "class=""hurriyet2010_so_sanstopu_no_text"">([^>]*)</span></div>"
ListBox1.DataSource =
    (
        From T In (New Regex(WebCode1)).Matches(Source) _
        .Cast(Of System.Text.RegularExpressions.Match)() _
        Select T.Groups(1).Value) _
    .ToList