• 大小: 49KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-10
  • 语言: C#
  • 标签: listview  

资源简介

使用VS2015 C# 编写的listview程序,可实现单元格的编写,其中代码在其他版本也可运行

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Listviewedit
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender EventArgs e)
        {
            ImageList imgList = new ImageList();
            imgList.ImageSize = new Size(1 20);
            listView1.SmallImageList = imgList;

            listView1.BackColor = Color.DarkGray;

            txtEdit.KeyDown += new KeyEventHandler(txtEdit_KeyDown);
        }

        private void txtEdit_KeyDown(object sender KeyEventArgs e)
        {
            //Enter键 更新项并隐藏编辑框  
            Point tmpPoint = listView1.PointToClient(Cursor.Position);
            ListViewItem.ListViewSubItem subitem = listView1.HitTest(tmpPoint).SubItem;

            if (e.KeyCode == Keys.Enter)
            {
                subitem.Text = txtEdit.Text;
                txtEdit.Visible = false;
            }
            //Esc键 直接隐藏编辑框  
            if (e.KeyCode == Keys.Escape)
                txtEdit.Visible = false;
        }

        private void listView1_MouseClick(object sender MouseEventArgs e)
        {
            txtEdit.Visible = false;
        }

        private void listView1_MouseDoubleClick(object sender MouseEventArgs e)
        {
            string itemText = ““;

            ListViewItem lvi = listView1.GetItemAt(e.X e.Y);
            ListViewItem.ListViewSubItem lvsi = lvi.GetSubItemAt(e.X e.Y);
            int colIndex = lvi.SubItems.IndexOf(lvsi);

            if (lvi != null && colIndex >= 0)
            {
                itemText = listView1.Columns[colIndex].Text;
                Rectangle rect = lvi.GetBounds(ItemBoundsPortion.Entire);
                for(int i = 0;i< colIndex;i++)
                {
                    rect.X += listView1.Columns[i].Width;
                }
                
                rect.Width = listView1.Columns[colIndex].Width;
                txtEdit.Parent = listView1;
                txtEdit.Bounds = rect;
                txtEdit.Multiline = true;
                txtEdit.Visible = true;
                txtEdit.Text = itemText;
                txtEdit.Focus();
                txtEdit.SelectAll();

            }
        }
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

    ..A..H.     41984  2018-06-26 13:43  list\Listviewedit\.vs\Listviewedit\v14\.suo

     文件        189  2018-06-26 13:06  list\Listviewedit\Listviewedit\App.config

     文件      10752  2018-06-26 13:43  list\Listviewedit\Listviewedit\bin\Debug\Listviewedit.exe

     文件        189  2018-06-26 13:06  list\Listviewedit\Listviewedit\bin\Debug\Listviewedit.exe.config

     文件      22016  2018-06-26 13:43  list\Listviewedit\Listviewedit\bin\Debug\Listviewedit.pdb

     文件      22696  2018-06-26 13:43  list\Listviewedit\Listviewedit\bin\Debug\Listviewedit.vshost.exe

     文件        189  2018-06-26 13:06  list\Listviewedit\Listviewedit\bin\Debug\Listviewedit.vshost.exe.config

     文件        490  2017-09-29 21:43  list\Listviewedit\Listviewedit\bin\Debug\Listviewedit.vshost.exe.manifest

     文件       2559  2018-06-26 13:43  list\Listviewedit\Listviewedit\Form1.cs

     文件       4902  2018-06-26 13:32  list\Listviewedit\Listviewedit\Form1.Designer.cs

     文件       5817  2018-06-26 13:32  list\Listviewedit\Listviewedit\Form1.resx

     文件       3922  2018-06-26 13:09  list\Listviewedit\Listviewedit\Listviewedit.csproj

     文件        868  2018-06-26 13:06  list\Listviewedit\Listviewedit\obj\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       7132  2018-06-26 13:06  list\Listviewedit\Listviewedit\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件        568  2018-06-26 13:43  list\Listviewedit\Listviewedit\obj\Debug\Listviewedit.csproj.FileListAbsolute.txt

     文件       1012  2018-06-26 13:32  list\Listviewedit\Listviewedit\obj\Debug\Listviewedit.csproj.GenerateResource.Cache

     文件      10752  2018-06-26 13:43  list\Listviewedit\Listviewedit\obj\Debug\Listviewedit.exe

     文件        180  2018-06-26 13:32  list\Listviewedit\Listviewedit\obj\Debug\Listviewedit.Form1.resources

     文件      22016  2018-06-26 13:43  list\Listviewedit\Listviewedit\obj\Debug\Listviewedit.pdb

     文件        180  2018-06-26 13:09  list\Listviewedit\Listviewedit\obj\Debug\Listviewedit.Properties.Resources.resources

     文件          0  2018-06-26 13:06  list\Listviewedit\Listviewedit\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs

     文件          0  2018-06-26 13:06  list\Listviewedit\Listviewedit\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs

     文件          0  2018-06-26 13:06  list\Listviewedit\Listviewedit\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs

     文件        524  2018-06-26 13:06  list\Listviewedit\Listviewedit\Program.cs

     文件       1332  2018-06-26 13:06  list\Listviewedit\Listviewedit\Properties\AssemblyInfo.cs

     文件       2837  2018-06-26 13:06  list\Listviewedit\Listviewedit\Properties\Resources.Designer.cs

     文件       5612  2018-06-26 13:06  list\Listviewedit\Listviewedit\Properties\Resources.resx

     文件       1099  2018-06-26 13:06  list\Listviewedit\Listviewedit\Properties\Settings.Designer.cs

     文件        249  2018-06-26 13:06  list\Listviewedit\Listviewedit\Properties\Settings.settings

     文件       1003  2018-06-26 13:06  list\Listviewedit\Listviewedit.sln

............此处省略15个文件信息

评论

共有 条评论