vb.net将图像逐个设置到picturebox

时间:2014-10-30 02:53:17

标签: vb.net picturebox

我已在我的vb.net代码中执行了以下步骤:

  1. 从文本文件中获取包含我想要的值的行,在我的情况下,我需要最后一个值为" Group"

  2. 创建一个图片框数组,图片框的数量是步骤1中的数字.1行有2个图片框。

  3. 之后我想将从文本文件中读取的图像设置为1到1的图片框(图片名称是第2个value.jpg),但我还没有找到合适的方法来做到这一点。 / p>

    有什么建议吗?

    我的文本文件如下:

    enter image description here

    我的代码到现在为止:

        Private PicBoxArray(9) As PictureBox        
    
        Dim value0 As String
        Dim value1 As String
        Dim value2 As String
        Dim value3 As String
    
        Dim fileName As String = "D:\local\MyTest1.txt"
        Dim subString1 As String = "Group"
        Dim lines As String() = File.ReadAllLines(fileName)
        For i As Integer = 0 To lines.Length - 1
            If lines(i).Contains(subString1) Then
                count1 += 1
        end if
        Next
    
        For i = 1 To count1
            PicBoxArray(i) = New PictureBox
            With PicBoxArray(i)
                .Tag = i
                .Size = New Size(330, 280)
                If i Mod 2 = 0 Then
                    .Location = New Point(430, 80 + 300 * ((i - 2) / 2))                   
                Else
                    .Location = New Point(80, 80 + 300 * ((i - 1) / 2))
                End If
    
                .Parent = Me
                .Visible = True
            End With
        Next
    
      Using MyReader As New Microsoft.VisualBasic.
                    FileIO.TextFieldParser("D:\PUB_GIS_Tagging\local\MyTest.txt")
    
            Dim lineCount = File.ReadAllLines("D:\PUB_GIS_Tagging\local\MyTest.txt").Length
    
            MyReader.TextFieldType = FileIO.FieldType.Delimited
            MyReader.SetDelimiters(",")
            Dim currentRow As String()
            While Not MyReader.EndOfData
                Try
                    currentRow = MyReader.ReadFields()
    
                    value0 = currentRow(0)  
                    value1 = currentRow(1)  
                    value2 = currentRow(2) 
                    value3 = currentRow(3)
    
    
       If value3 = "Group"
           '======= put the picture from PicBoxArray(1) to PicBoxArray(count1) i by 1========
           '====================== I am stuck here=============
       End if 
    
       Catch ex As Microsoft.VisualBasic.
             FileIO.MalformedLineException
             MsgBox("Line " & ex.Message &
                    "is not valid and will be skipped.")
       End Try
     End While
    End Using
    

1 个答案:

答案 0 :(得分:2)

我假设您要从文件中读取一行,然后将该行的图片设置为您的第一个图片框,然后阅读下一行并将下一张图片设置为下一个图片框?

Dim linecounter as Integer = 0 
While Not MyReader.EndOfData
        Try
            currentRow = MyReader.ReadFields()

            value0 = currentRow(0)  
            value1 = currentRow(1)  
            value2 = currentRow(2) 
            value3 = currentRow(3)


If value3 = "Group" Then
    PicBoxArray(linecounter).ImageLocation = "C:\vb_test\" & value1 & ".pdf.jpg" 
    linecounter += 1
End if 
next