我想将动态图片框与动态标签对齐

时间:2017-05-10 07:27:53

标签: vb.net

这是我的功能,它允许我从网页获取我的PictureBoxs的图像链接和我的标签的标题

Public Shared Function getPics(website As String, pattern As String)

    Dim tempTitles As New List(Of String)()
    Dim tempTitles2 As New List(Of String)()
    Dim lestitres As New List(Of titlesclass)

    Dim webClient As New WebClient()
    webClient.Headers.Add("user-agent", "null")
    Dim counter As Integer = 0
    Dim counter2 As Integer = 0
    Dim counter3 As Integer = 0
    Dim counter4 As Integer = 1
    Dim counter5 As Integer = 0
    Dim counter6 As Integer = 0
    'If the website happens to go offline, at least your application wont crash.
    Dim content As String = webClient.DownloadString(website)
    Dim query = From title In Regex.Matches(content, pattern).Cast(Of Match)
                Select New With {Key .Link = String.Concat("http://www.gamestop.com", title.Groups("Data").Value),
                         Key .Title = title.Groups("Dataa").Value}
    For Each letitre In query.Distinct
        'MsgBox(letitre.Link & " ======= " & letitre.Title)
    Next
    'For Each title As Match In (New Regex(pattern).Matches(content)) 'Since you are only pulling a few strings, I thought a regex would be better.
    '    Dim letitre As New titlesclass
    '    letitre.Link = title.Groups("Data").Value
    '    letitre.Title = title.Groups("Dataa").Value
    '    lestitres.Add(letitre)
    '    'tempTitles2.Add(title.Groups("Dataa").Value)
    'Next
    Dim titles = tempTitles.Distinct().ToArray() 'remove duplicate titles
    'Dim titles2 = tempTitles2.Distinct().ToArray()
    Dim titles2 = lestitres.Distinct().ToArray()
    lestitres.Clear()

    'For Each title As titlesclass In titles2
    For Each letitre In query.Distinct
        'ListBox.Items.Add(title) 'what you do with the values from here is up to you.
        Dim ImageInBytes() As Byte = webClient.DownloadData(letitre.Link)
        Dim ImageStream As New IO.MemoryStream(ImageInBytes)
        Dim MyPic As New PictureBox
        Dim MyLab As New Label


        If (counter2 > 0 AndAlso ((counter2 Mod 4 = 0) OrElse counter3 = 1)) Then

            counter3 = 1
            counter4 += 1
            If (counter2 Mod 4 = 0) Then
                counter5 = 0
                counter6 += 170
            End If
            MyPic.Location = New Point(counter5, MyPic.Location.Y + counter6)
            MyLab.Location = New Point(counter5, MyPic.Location.Y + counter6)
            If counter4 = 4 Then
                counter3 = 0
            End If


            counter5 += 200
        Else
            MyPic.Location = New Point(counter, MyPic.Location.Y)
            MyLab.Location = New Point(counter, MyPic.Location.Y)
        End If
        counter += 200
        counter2 += 1
        MyPic.SizeMode = PictureBoxSizeMode.AutoSize
        MyLab.Text = letitre.Title
        MyPic.Image = New System.Drawing.Bitmap(ImageStream)
        Form2.Controls.Add(MyPic)
        Form2.Controls.Add(MyLab)

    Next


End Function

名为titlesclass的类包含两个元素,我将链接和标题存储在:

Public Class titlesclass
Public Property Link As String
Public Property Title As String
End Class

我的小按钮可以完成所有工作

Dim websiteURL1 As String = "http://www.gamestop.com/collection/upcoming-video-games"
    Class1.getPics(websiteURL1, "<img src=""(?<Data>[^>]*)""><p>(?<Dataa>[^>]*)<br>")

我想要做的是每行显示4个图片框,每张图片旁边都有标签,现在有些标签没有显示,有些显示在正确的地方,有些显示很远吼叫!我测试了我在消息框中获得的值,它以我需要的顺序向我显示信息,我不确定我是否搞砸了x,y值或它是否已经搞定了别的......

编辑:我已经可以显示每行4个图片框,标签也是,但是某些标签的Y没有经过很好的调整,它可以远远低于它!

以下是一些可以帮助您了解我的情况的图片不要介意按钮和列表框,它只是用于测试: 我的清单中包含很多图片,所以我只是向你展示了一些东西,当它表现得很好,当它显示远离设计图片的几行时

http://img110.xooimage.com/files/f/a/d/picture1-5239f7c.png
http://img110.xooimage.com/files/8/f/8/picture-2-5239f7e.png
http://img110.xooimage.com/files/4/7/b/picture-3-5239f80.png
http://img110.xooimage.com/files/f/0/f/picture4-5239f82.png

1 个答案:

答案 0 :(得分:1)

因此,我使用PictureBox原则和增量清理了生成row位置的方式:

注意:

  • 如果您需要在顶部添加空间来添加信息,请将row计数设为1而不是0

注2

  • 此处图像尺寸已编码,但您可以使用更流畅的动态图像尺寸。我刚刚清理了定位代码而不是其他代码。

用这个替换你的功能:

Public Shared Sub getPics(website As String, pattern As String)
    Dim tempTitles As New List(Of String)()
    Dim tempTitles2 As New List(Of String)()
    Dim lestitres As New List(Of titlesclass)
    Dim webClient As New WebClient()
    webClient.Headers.Add("user-agent", "null")
    Dim counter As Integer = 0
    Dim counter2 As Integer = 0
    Dim counter3 As Integer = 0
    Dim counter4 As Integer = 1
    Dim counter5 As Integer = 0
    Dim counter6 As Integer = 0
    'If the website happens to go offline, at least your application wont crash.
    'Handle default proxy
    Dim proxy As IWebProxy = WebRequest.GetSystemWebProxy()
    proxy.Credentials = CredentialCache.DefaultCredentials
    webClient.Proxy = proxy
    Dim content As String = webClient.DownloadString(website)
    Dim query = From title In Regex.Matches(content, pattern).Cast(Of Match)
                Select New With {Key .Link = String.Concat("http://www.gamestop.com", title.Groups("Data").Value),
                         Key .Title = title.Groups("Dataa").Value}

    Dim titles = tempTitles.Distinct().ToArray() 'remove duplicate titles
    Dim titles2 = lestitres.Distinct().ToArray()
    lestitres.Clear()
    'Count Items
    Dim item As Integer = 0
    'Count Row
    Dim row As Integer = 0
    'image: 222*122
    For Each letitre In query.Distinct
        Dim ImageInBytes() As Byte = webClient.DownloadData(letitre.Link)
        Dim ImageStream As New IO.MemoryStream(ImageInBytes)
        Dim MyPic As New PictureBox
        Dim MyLab As New Label
        'x = numéro item fois largeur image
        'y = numéro de ligne fois hauteur image
        MyPic.Location = New Point(item * 222, row * 122)
        MyLab.Location = New Point(item * 222, row * 122)
        MyPic.SizeMode = PictureBoxSizeMode.AutoSize
        MyLab.Text = letitre.Title
        MyPic.Image = New System.Drawing.Bitmap(ImageStream)
        Form1.Controls.Add(MyPic)
        Form1.Controls.Add(MyLab)
        'Bring Labels to front
        For Each ctrl As Control In Form1.Controls
            'If the control is of type label
            If TypeOf ctrl Is Label Then
                'Then bring to front 
                ctrl.BringToFront()
            End If
        Next
        'Increment the item count
        item = item + 1
        'If item is multiple of 4 or could check 4 then
        If item Mod 4 = 0 Then
            'Reset counter
            item = 0
            'Increment Row
            row = row + 1
        End If
    Next
End Sub

返回示例:

games