2014-10-30 42 views
0

我已经完成我的vb.net代码如下步骤:vb.net集图像,图片框一个接一个

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

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

之后,我想1设置从文本文件中读取到pictureboxes 1的图像(图片名称是第二value.jpg),但我还没有找到一种合适的方式来做到这一点。

对此有何建议?

我的文本文件如下:

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 

回答

2

我假设你想读你的文件中的一行,然后设置图片来源那行到你的第一个picturebox,然后读下一行,并将下一个图片设置到下一个picturebox?

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 
+0

嗨索引,非常感谢答复。我把代码放在我的应用程序中,如下所示:For i = 1 To count1 PicBoxArray(i).ImageLocation =“C:\ vb_test \”&value4&“.pdf.jpg” 接下来,每次读一行,所有图像将一起更改为新的线值。我认为在我的代码中还存在一些问题,请你看看这个吗?谢谢! – user30643 2014-10-30 05:06:33

+0

嗨索引,它的作品,非常感谢你! – user30643 2014-10-30 06:19:12

相关问题