获取图片列表到图片框InvalidArgument ='0'的值对'index'无效

时间:2018-03-24 21:01:09

标签: vb.net

我有一个列表视图,其中包含图像名称和图像本身。 我需要将我的图像列表中的所选图像放到图片框中。 我尝试了不同的方法让它在所有方面都成功地完成同样的错误。 我试着制作一个整数变量“index”然后如果所选项目是= 0那么PictureBox1.Image = ImageList1.Images(index) 这是我的代码

Public Class Form1
Private CounterVar As Integer = 0
Dim imgList As New ImageList
Dim index As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    ListView1.View = View.Details
    ListView1.Width = 500

    ListView1.Columns.Add("Image", 75, HorizontalAlignment.Left)
    ListView1.Columns.Add("Name", 100, HorizontalAlignment.Left)
    'ListView1.Columns.Add("Notes", 350, HorizontalAlignment.Left)
    ListView1.AllowColumnReorder = True

    ListView1.Columns(0).DisplayIndex = 1

    imgList.Images.Add("Male", Image.FromFile("C:\Users\Utilizador\Downloads\balloon.jpg"))
    imgList.Images.Add("Female", Image.FromFile("C:\Users\Utilizador\Downloads\cookie.jpg"))
    ListView1.SmallImageList = imgList
    Dim index As Integer
    'Select Case True
    'Case RadioButton1.Checked
    'index = 0
    'Case RadioButton2.Checked
    'index = 1
    'End Select
    index = 1
    Dim lvi As New ListViewItem
    lvi.ImageIndex = index
    lvi.SubItems.Add("Woman")
    ListView1.Items.Add(lvi)

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    index = 0
    Dim lvi As New ListViewItem
    lvi.ImageIndex = index
    lvi.SubItems.Add(TextBox1.Text)
    ListView1.Items.Add(lvi)
End Sub


Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
    If ListView1.Items(0).Selected = True Then

        PictureBox1.Image = ImageList1.Images(index)

    End If
End Sub
End Class

1 个答案:

答案 0 :(得分:0)

我不明白你面临的问题是什么,但你在评论中说过

  

我在我的照片箱上插入他们的小号

首先,您传递图像的方式并不理想。但是,如果您希望图像更大,则可以将PictureboxSizeMode设置为Stretch。示例:

 PictureBox.SizeMode = PictureBoxSizeMode.StretchImage