简体   繁体   English

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

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

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? 我如何将这样的项目添加到listview中?如果我需要在运行时在触发事件时进行编辑,我将如何编辑让我们说一个按行[]排序的汽车名称?

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. 如果您正在寻找ListView内容的“就地”编辑(特别是详细信息视图模式下ListView的子项),您需要自己实现,或使用第三方控件。

By default, the best you can achieve with a "standard" ListView is to set it's LabelEdit property 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 C#可编辑ListView
In-place editing of ListView subitems 就地编辑ListView子项

I use a hidden textbox to edit all the listview items/subitems. 我使用隐藏的文本框来编辑所有listview项目/子项目。 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 = "";
}

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. 我从第一个链接 - C#Editable ListView找到了解决方案,非常容易使用。 The general idea is to: 一般的想法是:

  • identify the SubItem that was selected and overlay a TextBox with the SubItem 's text over the SubItem 识别SubItem中选择了和覆盖一个TextBoxSubItem的文本在SubItem
  • give this TextBox focus 给这个TextBox焦点
  • change SubItem 's text to that of TextBox 's when TextBox loses focus TextBox失去焦点时,将SubItem的文本更改为TextBoxTextBox

What a workaround for a seemingly simple operation :-| 什么解决方法看似简单的操作: - |

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{}
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";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM