【问题标题】:vb.net set images to picturebox one by onevb.net 将图片一一设置到图片框
【发布时间】:2014-10-30 02:53:16
【问题描述】:

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

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

  2. 创建一个picturebox数组,picturebox的个数就是步骤1的个数。1行有2个picturebox。

之后,我想将从文本文件中读取的图像设置为 1 到 1 的图片框(图片名称是第二个 value.jpg),但我还没有找到合适的方法。

有什么建议吗?

我的文本文件如下:

到目前为止我的代码是:

    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

【问题讨论】:

    标签: vb.net picturebox


    【解决方案1】:

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

    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
    

    【讨论】:

    • 嗨索引,非常感谢您的回答。我将代码放在我的应用程序中,如下所示: For i = 1 To count1 PicBoxArray(i).ImageLocation = "C:\vb_test\" & value4 & ".pdf.jpg" 接下来,但每次读取一行,所有图像将一起更改为新的行值。我觉得我的代码还是有问题,你能看看这个吗?谢谢!
    猜你喜欢
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 2012-11-09
    • 1970-01-01
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多