vb.net 将图像添加到列表框(visual basic)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1774429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:31:50  来源:igfitidea点击:

add images to listbox (visual basic)

vb.netlistbox

提问by invert

I have sets of web hosted images that I need my user to be able to select 1 from each. I thought a listbox would work for this, but I can't figure out add an image to one. Is this possible? better way of doing this? I am using the latest free vb.

我有一组网络托管图像,我需要我的用户能够从每个图像中选择 1 个。我认为列表框可以用于此目的,但我无法弄清楚向其中添加图像。这可能吗?这样做的更好方法?我正在使用最新的免费 vb。

回答by invert

Use the Listview controlinstead, it provides better functionality, and doesn't suffer from an annoying resize bug. The listbox is carried over from VB6 days. The listview supports column headers, groupings and a bit more.

改用Listview 控件,它提供更好的功能,并且不会遇到烦人的调整大小错误。列表框是从 VB6 天延续下来的。列表视图支持列标题、分组等等。

Add a Imagelist control to your form, to store the images; set it's ColorDepth property to 32-bit, and set the Listview's LargeImagelist property to the imagelist control you just added (this can all be done in code as well).

将 Imagelist 控件添加到您的表单中,以存储图像;将它的 ColorDepth 属性设置为 32 位,并将 Listview 的 LargeImagelist 属性设置为您刚刚添加的图像列表控件(这也可以在代码中完成)。

Add images to the Imagelist via this code:

通过以下代码将图像添加到 Imagelist:

ImageList1.Images.Add("imagekey", Image.FromStream(yourimagestream))

Add items to the Listview via this code:

通过以下代码将项目添加到 Listview:

ListView1.Items.Add("list item title", "imagekey")

The "imagekey" is a way to tell the Listview which image to use. You can also use indexes for icons, but specifying an index that doesn't exist will give an index out of range exception, whereas a key that doesn't exist, will just use no image instead.

“imagekey”是一种告诉 Listview 使用哪个图像的方法。您还可以为图标使用索引,但指定一个不存在的索引会导致索引超出范围异常,而不存在的键将只使用不使用图像。

Oh you also want to set the Listview Multiselect property to False (if you only want them to select one at a time), and access the SelectedIndexChanged() and ItemActivate() events for when the user clicks / double-clicks on items respectively.

哦,您还想将 Listview Multiselect 属性设置为 False(如果您只希望它们一次选择一个),并在用户分别单击/双击项目时访问 SelectedIndexChanged() 和 ItemActivate() 事件。

回答by xpda

Set ListBox1.DrawModeto DrawMode.OwnerDrawFixedor DrawMode.OwnerDrawVariable, and add a handler for drawing the images.

设置ListBox1.DrawModeDrawMode.OwnerDrawFixedor DrawMode.OwnerDrawVariable,并添加用于绘制图像的处理程序。

Private Sub listBox1_DrawItem(ByVal sender As System.Object, ByVal e As   System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
Dim img As Image
img = sender.items(e.Index)
e.Graphics.DrawImage(img, targetsize) 
End Sub

You can add the images to the listbox items collection.

您可以将图像添加到列表框项目集合。

Dim img As Image
img = Image.FromFile("c:\tmp.jpg") ' or whatever
ListBox1.Items.Add(img)
...

回答by Abhishek

Yes, this is possible:

是的,这是可能的:

Dim imgList As New ImageList

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("Gender", 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\Joe\Pictures\Male-Symbol.jpg"))
    imgList.Images.Add("Female", Image.FromFile("C:\Users\Joe\Pictures\Female-Symbol.jpg"))
    ListView1.SmallImageList = imgList
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim index As Integer
    Select Case True
        Case RadioButton1.Checked
            index = 0
        Case RadioButton2.Checked
            index = 1
    End Select

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