VB中如何在ListView中添加ImageList中的图像

VB中如何在ListView中添加ImageList中的图像,第1张

方法如下

(1)新建一个VB工程

(2)在控件工具箱中添加ListView和ImageList控件

在工具箱上鼠标右键单击-->调出菜单

(3)在窗体上布置一个ListView和一个ImageList

(4)向ImageList1中插入图片

鼠标右键单击ImageList1-->调出菜单-->属性

(5)将ListView1与ImageList关联起来

鼠标右键单击ListView1-->调出菜单-->属性

(6)窗体代码

Option Explicit

Private Sub Form_Load()

    '向ListView1中添加项

    Dim itemX As ListItem

    With ListView1

        Set itemX = .ListItems.Add(, , "项目1")

        '使用ImageList1中的#1图

        itemX.Icon = 1

        

        Set itemX = .ListItems.Add(, , "项目2")

        '使用ImageList1中的#2图

        itemX.Icon = 2

    End With

End Sub

(7)运行效果

第一步:在窗体中拖入ListView控件和imageList控件;

第二步:设置imageList控件的Images属性,添加你想要的图片;

第三步:设置ListView控件的SmallImageList、LargeImageList、StateImageList属性为imageList;

第四步:编辑ListView控件项的ImageIndex行为你就会发现图片成功显示出来了!

附:在ListView控件中添加选项的代码

private void button1_Click(object sender, EventArgs e)

{

if (textBox1.Text == "")

{

MessageBox.Show("添加的内容不能为空")

textBox1.Focus() //获取焦点

}

else

{

if (listView1.Items.Count >0) //判断列表框中是否有项

{

//循环比较是否有重复项,有则放弃添加

for (int i = 0i <listView1.Items.Counti++)

{

if (string.Compare(listView1.Items[i].Text.ToString(), textBox1.Text) == 0)

{

MessageBox.Show("项目重复,不能添加!")

textBox1.Text = "" //清空文本框

textBox1.Focus()

return

}

}

listView1.Items.Add(textBox1.Text.ToString())

textBox1.Text = ""

}

else

{

listView1.Items.Add(textBox1.Text.ToString()) //将文本框中的数据添加到列表框

textBox1.Text = ""

}

}

}

如果你是想问怎么样通过资源名获取资源的话,可以用反射。例:已知图片名picture.pngField field = R.drawable.class.getField("picture")int sourceID = field.getInt(R.drawable.class)


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11446960.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存