C#:如何编辑列表视图中的项目和子项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/471859/
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-08-04 04:43:55  来源:igfitidea点击:

C#: How do you edit items and subitems in a listview?

c#listview

提问by

How do you edit items and subitems in a listview? Let's say I have a listview with 3 columns,and subitems,

如何编辑列表视图中的项目和子项目?假设我有一个包含 3 列和子项的列表视图,

Car Brand | Car Name | Car Year
Ford      | Mustang  | 2000
Dodge     | Charger  | 2007

How would I Add items like that to listview and how would I edit let's say the Car Name on which ever row by index[] if I needed to edit at runtime at firing of an event?

如果我需要在运行时在触发事件时进行编辑,我将如何将这样的项目添加到列表视图中,以及如何编辑让我们通过索引 [] 在哪一行上说汽车名称?

回答by CraigTP

If you're looking for "in-place" editing of a ListView's contents (specifically the subitems of a ListView in details view mode), you'll need to implement this yourself, or use a third-party control.

如果您正在寻找“就地”编辑 aListView的内容(特别是详细视图模式下 ListView 的子项),您需要自己实现,或使用第三方控件。

By default, the best you can achieve with a "standard" ListViewis to set it's LabelEditproperty to true to allow the user to edit the text of the first column of the ListView(assuming you want to allow a free-format text edit).

默认情况下,使用“标准”可以实现的最佳效果ListView是将其LabelEdit属性设置为 true 以允许用户编辑第一列的文本ListView(假设您希望允许自由格式的文本编辑)。

Some examples (including full source-code) of customized ListView's that allow "in-place" editing of sub-items are:

ListView允许对子项进行“就地”编辑的自定义的一些示例(包括完整的源代码)是:

C# Editable ListView
In-place editing of ListView subitems

C# 可编辑 ListView
就地编辑 ListView 子项

回答by lazo

Sorry, don't have enough rep, or would have commented on CraigTP's answer.

抱歉,没有足够的代表,或者会对CraigTP 的回答发表评论。

I found the solution from the 1st link - C# Editable ListView, quite easy to use. The general idea is to:

我从第一个链接中找到了解决方案 - C# Editable ListView,非常易于使用。总体思路是:

  • identify the SubItemthat was selected and overlay a TextBoxwith the SubItem's text over the SubItem
  • give this TextBoxfocus
  • change SubItem's text to that of TextBox's when TextBoxloses focus
  • 识别SubItem被选中的 ,并TextBoxSubItem的文本覆盖SubItem
  • 给这个TextBox重点
  • 失去焦点时将SubItem的文本更改为' 的文本TextBoxTextBox

Whata workaround for a seemingly simple operation :-|

对于看似简单的操作来说,这是一种怎样的解决方法:-|

回答by ahmad

private void listView1_MouseDown(object sender, MouseEventArgs e)
{
    li = listView1.GetItemAt(e.X, e.Y);
    X = e.X;
    Y = e.Y;
}

private void listView1_MouseUp(object sender, MouseEventArgs e)
{
    int nStart = X;
    int spos = 0;
    int epos = listView1.Columns[1].Width;
    for (int i = 0; i < listView1.Columns.Count; i++)
    {
        if (nStart > spos && nStart < epos)
        {
            subItemSelected = i;
            break;
        }

        spos = epos;
        epos += listView1.Columns[i].Width;
    }
    li.SubItems[subItemSelected].Text = "9";
}

回答by Ryan

Click the items in the list view. Add a button that will edit the selected items. Add the code

单击列表视图中的项目。添加将编辑所选项目的按钮。添加代码

try
{              
    LSTDEDUCTION.SelectedItems[0].SubItems[1].Text = txtcarName.Text;
    LSTDEDUCTION.SelectedItems[0].SubItems[0].Text = txtcarBrand.Text;
    LSTDEDUCTION.SelectedItems[0].SubItems[2].Text = txtCarName.Text;
}
catch{}

回答by Nemesis

I use a hidden textbox to edit all the listview items/subitems. The only problem is that the textbox needs to disappear as soon as any event takes place outside the textbox and the listview doesn't trigger the scroll event so if you scroll the listview the textbox will still be visible. To bypass this problem I created the Scroll event with this overrided listview.

我使用隐藏的文本框来编辑所有列表视图项/子项。唯一的问题是文本框需要在文本框外发生任何事件时立即消失,并且列表视图不会触发滚动事件,因此如果滚动列表视图,文本框仍然可见。为了绕过这个问题,我用这个覆盖的列表视图创建了 Scroll 事件

Here is my code, I constantly reuse it so it might be help for someone:

这是我的代码,我经常重用它,所以它可能对某人有所帮助:

ListViewItem.ListViewSubItem SelectedLSI;
private void listView2_MouseUp(object sender, MouseEventArgs e)
{
    ListViewHitTestInfo i = listView2.HitTest(e.X, e.Y);
    SelectedLSI = i.SubItem;
    if (SelectedLSI == null)
        return;

    int border = 0;
    switch (listView2.BorderStyle)
    {
        case BorderStyle.FixedSingle:
            border = 1;
            break;
        case BorderStyle.Fixed3D:
            border = 2;
            break;
    }

    int CellWidth = SelectedLSI.Bounds.Width;
    int CellHeight = SelectedLSI.Bounds.Height;
    int CellLeft = border + listView2.Left + i.SubItem.Bounds.Left;
    int CellTop =listView2.Top + i.SubItem.Bounds.Top;
    // First Column
    if (i.SubItem == i.Item.SubItems[0])
        CellWidth = listView2.Columns[0].Width;

    TxtEdit.Location = new Point(CellLeft, CellTop);
    TxtEdit.Size = new Size(CellWidth, CellHeight);
    TxtEdit.Visible = true;
    TxtEdit.BringToFront();
    TxtEdit.Text = i.SubItem.Text;
    TxtEdit.Select();
    TxtEdit.SelectAll();
}
private void listView2_MouseDown(object sender, MouseEventArgs e)
{
    HideTextEditor();
}
private void listView2_Scroll(object sender, EventArgs e)
{
    HideTextEditor();
}
private void TxtEdit_Leave(object sender, EventArgs e)
{
    HideTextEditor();
}
private void TxtEdit_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
        HideTextEditor();
}
private void HideTextEditor()
{
    TxtEdit.Visible = false;
    if (SelectedLSI != null)
        SelectedLSI.Text = TxtEdit.Text;
    SelectedLSI = null;
    TxtEdit.Text = "";
}