我有一个包含4个图片框的面板:
我有一个带有以下代码的“加载图像”按钮,可将图像加载到图片框中:
'Procedure to upload images through the button click
'open Dialog box to load image
ofd.FileName = Nothing
'Show dialog box above all froms
ofd.Multiselect = False
ofd.Title = "Select Image to Upload"
ofd.Filter = "Image Files |*.jpg*"
ofd.ShowDialog()
'Select file and place on picture box
If Not ofd.FileName = Nothing Then
picBox.ImageLocation = ofd.FileName
End If
'Show the button to delete image
btn.Visible = True
当我只有一个盒子来填充时,程序运作良好,但现在我有4个,我不想为每个盒子创建一个按钮。我的问题是,如何使用相同的程序(或类似的程序)将图片加载到第一个以外的框中。
他们需要按顺序加载。例如,单击按钮,加载左上方框,再次单击并加载右上方框。因此,如果所有4个盒子都没有图像,那么左上角是第一个,如果左上角有图像,那么右上角是下一个,依此类推。
答案 0 :(得分:1)
尝试类似
的内容dim i as integer
dim file as string
file = OpenFileDialog1.FileNames
For Each file In OpenFileDialog1.FileNames
if i=0
picturebox1. ImageLocation=file
elseif i=1
picturebox2. ImageLocation=file
elseif i=2
picturebox3. ImageLocation=file
elseif i=3
picturebox4. ImageLocation=file
end if
i+=1
Next
按照您想要的顺序给出图片框的顺序。
答案 1 :(得分:1)
你可以做类似下面的事情;只需替换名为" PBs":
的数组中PictureBox的名称Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ofd.Multiselect = False
ofd.Title = "Select Image to Upload"
ofd.Filter = "Image Files |*.jpg*"
If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim PBs() As PictureBox = {PictureBox1, PictureBox2, PictureBox3, PictureBox4}
Dim nextPB = PBs.Where(Function(x) IsNothing(x.Image)).FirstOrDefault
If Not IsNothing(nextPB) Then
nextPB.ImageLocation = ofd.FileName
End If
End If
End Sub
答案 2 :(得分:1)
另一种解决方案:
将OpenFileDialog的Multiselect属性设置为True,然后在OpenFileDialog1_FileOK事件中执行此操作:
For x = 0 To OpenFileDialog1.FileNames.Length - 1
Dim Path As String = OpenFileDialog1.FileNames(x)
Select Case x
Case 0
PictureBox1.Image = Image.FromFile(Path)
Case 1
PictureBox2.Image = Image.FromFile(Path)
Case 2
PictureBox3.Image = Image.FromFile(Path)
Case 3
PictureBox4.Image = Image.FromFile(Path)
End Select
Next