C# winform listView 控件的使用

将listview按表格显示,动态读取ini文件写入

上代码:

 if (ExistINIFile())
            {
                string str = IniReadValue("Test", "label_commandnum");
                int num = str.Length > 0 ? Int32.Parse(str) : 0;

                listView1.Items.Clear();
                for (i = 0; i < num; i++)
                {
                    string[] labelArray = new string[num];
                    string[] bitArray = new string[8];
                    labelArray = IniReadValue("Test", "label_command" + i.ToString()).Split(',');

                    ListViewItem viewItem = new ListViewItem(labelArray[0].ToString());
                    viewItem.SubItems.Add(labelArray[1].ToString());
                    viewItem.SubItems.Add(labelArray[2].ToString());
                    listView1.Items.Add(viewItem);
                  
                }

这里注意的是当我写入数据发现数据从第二列写入,第一列被空着,解决方法是将第一列数据直接写入ListViewItem viewItem = new ListViewItem(labelArray[0].ToString());这样第一列数据就显示了。

将listview按表格显示,每行根据不同条件显示不同图标,以及字体颜色

上代码:

 ListViewItem viewItem = new ListViewItem("  "+textArray[0]);
                   listView1.SmallImageList = imageList1;
                   
                    if (textArray[0]=="出错")
                    {
                        
                         viewItem.ForeColor = Color.Red;
                         viewItem.ImageIndex = 0;
                        
                    }
                    else if (textArray[0] == "异常")
                    {
                        viewItem.ForeColor = Color.Red;
                        viewItem.ImageIndex = 1;
                       
                    }
                    else if (textArray[0] == "提示")
                    {
                        viewItem.ForeColor = Color.Orange;
                        viewItem.ImageIndex = 2;
                    }
                    else if (textArray[0] == "发送")
                    {
                        viewItem.ForeColor = Color.Black;
                        viewItem.ImageIndex = 3;
                    }
                    else if (textArray[0] == "读取")
                    {
                        viewItem.ForeColor = Color.Black;
                        viewItem.ImageIndex = 4;
                    }
                    else if (textArray[0] == "开始")
                    {
                        viewItem.ForeColor = Color.Black;
                        viewItem.ImageIndex = 5;
                    }
                    else if (textArray[0] == "结束")
                    {
                        viewItem.ForeColor = Color.Black;
                        viewItem.ImageIndex = 6;
                    }
                    else if (textArray[0] == "等待")
                    {
                        viewItem.ForeColor = Color.Black;
                        viewItem.ImageIndex = 7;
                    }
                   // listView2.SmallImageList = imageListSmall;
                   // viewItem.ImageList.Images.Add();
                    viewItem.SubItems.Add(textArray[1]);
                    viewItem.SubItems.Add(textArray[2]);
                    listView2.Items.Add(viewItem);

//界面加上imageList1控件,在属性Images里面添加你的图标。
ImageIndex 就是你的属性中图片集合的索引,对应显示。
ForeColor 对应条目的字体颜色


C# winform listView 控件的使用_第1张图片
imageList1属性

C# winform listView 控件的使用_第2张图片
图像集合

listView上移

if (listView1.SelectedItems.Count == 0)
            {
                return;
            }
            listView1.BeginUpdate();
            if (listView1.SelectedItems[0].Index > 0)
            {
                foreach (ListViewItem lvi in listView1.SelectedItems)
                {
                    ListViewItem lviSelectedItem = lvi;
                    int indexSelectedItem = lvi.Index;
                    listView1.Items.RemoveAt(indexSelectedItem);
                    listView1.Items.Insert(indexSelectedItem - 1, lviSelectedItem);
                }
            }
            listView1.EndUpdate();
            if (listView1.Items.Count > 0 && listView1.SelectedItems.Count > 0)
            {
                listView1.Focus();
                listView1.SelectedItems[0].Focused = true;
                listView1.SelectedItems[0].EnsureVisible();
            }
        }

listView下移

if (listView1.SelectedItems.Count == 0)
            {
                return;
            }
            listView1.BeginUpdate();
            int indexMaxSelectedItem = listView1.SelectedItems[listView1.SelectedItems.Count - 1].Index;
            if (indexMaxSelectedItem < listView1.Items.Count - 1)
            {
                for (int i = listView1.SelectedItems.Count - 1; i >= 0; i--)
                {
                    ListViewItem lviSelectedItem = listView1.SelectedItems[i];
                    int indexSelectedItem = lviSelectedItem.Index;
                    listView1.Items.RemoveAt(indexSelectedItem);
                    listView1.Items.Insert(indexSelectedItem + 1, lviSelectedItem);
                }
            }
            listView1.EndUpdate();
            if (listView1.Items.Count > 0 && listView1.SelectedItems.Count > 0)
            {
                listView1.Focus();
                listView1.SelectedItems[listView1.SelectedItems.Count - 1].Focused = true;
                listView1.SelectedItems[listView1.SelectedItems.Count - 1].EnsureVisible();
            }

listView删除

 for (int i = listView1.SelectedItems.Count - 1; i >= 0; i--)
                {
                    ListViewItem item = listView1.SelectedItems[i];
                    listView1.Items.Remove(item);
                }

你可能感兴趣的:(C# winform listView 控件的使用)