本文介绍了C#将图像与listview1中的项目一起添加?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



以下是在listView1中从textBox1和TextBox2添加项目的代码,它可以正常工作。

我希望当我将一个项目添加到listView1然后始终在listView1中添加相同的图像(可能来自imageList1)(在第一列中)。我怎么做?

提前致谢。



类似下面的链接





我尝试过:



Hello everyone,

This below is the code to add items from textBox1 and TextBox2 in listView1, and it works.
I wish that when I add an item to listView1 to then always add a same image (maybe from imageList1) in listView1 (in the first column). How do I do this?
Thanks in advance.

something like the link below
https://s30.postimg.org/jypx9z5wx/image.gif

What I have tried:

public Form1()
        {
            InitializeComponent();
            listView1.Columns.Add("Image", 30);
            listView1.Columns.Add("Column1", 150);
            listView1.Columns.Add("Column2", 300);
            listView1.View = View.Details;
        }


  private void add(string name, String pos)
        {
            ListViewItem item = new ListViewItem();

            if (!IsExistsItem(name))
            {
                item.Text = name;
            }
            else
            {             
                MsgBox.Show("This item already exists in the list!", this.Text",
                MsgBox.Buttons.OK, MsgBox.Icon.Warning);
                return;
            }
            if (!IsExistsSubItem(pos))
            {
                item.SubItems.Add(pos);
            }
            else
            {                  
                MsgBox.Show("This item already exists in the list!", this.Text",
                MsgBox.Buttons.OK, MsgBox.Icon.Warning);
                return;
            }
            listView1.Items.Add(item);       
        }

   private bool IsExistsItem(string text)
        {
            foreach (ListViewItem item in listView1.Items)
            {
                if (item.Text == text)

                    return true;
            }
            return false;
        }
        private bool IsExistsSubItem(string text)
        {
            foreach (ListViewItem item in listView1.Items)
            {
                if (item.SubItems[1].Text == text)
                    return true;
            }
            return false;
        }


private void button1_Click(object sender, EventArgs e)
        {
          add(textBox1.Text, textBox2.Text);          
        }

推荐答案


这篇关于C#将图像与listview1中的项目一起添加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 03:39