【问题标题】:Making a certain listView sub-item column editable使某个listView子项列可编辑
【发布时间】:2019-11-21 13:27:58
【问题描述】:

我发现了大量有用的信息(包括此解决方案),可以使 listView 列可编辑。但是,我找不到太多关于如何将某些功能锁定到特定子项列的信息,并且在我的尝试中没有成功。

请考虑以下代码:

    // Make ListView Editable
    ListViewItem.ListViewSubItem SelectedLSI;
    private void DwgList_MouseUp(object sender, MouseEventArgs e)
    {
        ListViewHitTestInfo i = DwgList.HitTest(e.X, e.Y);
        SelectedLSI = i.SubItem;
        if (SelectedLSI == null)
            return;

        int border = 0;
        switch (DwgList.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 + DwgList.Left + i.SubItem.Bounds.Left;
        int CellTop = DwgList.Top + i.SubItem.Bounds.Top;

        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 DwgList_MouseDown(object sender, MouseEventArgs e)
    {
        HideTextEditor();
    }
    private void DwgList_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 = "";
    }

我尝试了以下方法,但产生了错误和不可预知的结果:

    private void DwgList_MouseUp(object sender, MouseEventArgs e)
    {
        ListViewHitTestInfo i = DwgList.HitTest(e.X, e.Y);
        SelectedLSI = i.SubItem;
        // Lock to fourth column
        if (i.SubItem == i.Item.SubItems[3])
        {
            ... rest of code here
        }
    }

    private void HideTextEditor()
    {
        if (i.SubItem == i.Item.SubItems[3])
        {
            TxtEdit.Visible = false;
            if (SelectedLSI != null)
                SelectedLSI.Text = TxtEdit.Text;
            SelectedLSI = null;
            TxtEdit.Text = "";
        }
    }

编辑: 请参阅下面的图片以获得直观的解释。

我可以使用我的代码编辑列,如下所示:

但我可以编辑任何列,我想将此功能锁定到图 1 中显示的第 4 列(每个项目的子项目 3)。

【问题讨论】:

  • 只能编辑一个,即第一列,主列。但是您可以根据需要对列重新排序。 - 然后有解决方法..
  • 是的,这是一种解决方法。它允许使用隐藏的文本框编辑所有列。不过,我只想将功能限制在第 4 列(子项 3)
  • 这是 WPF、Winforms、ASP、Net 还是其他东西。请以有用的方式标记问题。
  • 我希望你会更好地使用一个如果许多第 3 方网格控件然后尝试让一个 ListView 来做复杂的工作人员。 (在鼠标按下时在正确的位置显示一个文本框等会起作用,但需要很长时间才能正确查看。)

标签: c# winforms listview


【解决方案1】:

问题是你真的不知道你在哪个专栏。我可以想到几个方法来解决这个问题:

1) 实现本文中的代码以获取列号并测试:http://csharphelper.com/blog/2014/09/find-the-listview-row-and-column-under-the-mouse-in-c/

2) 设置子项标签为真或假判断可编辑性,然后在命中测试上检查标签:

var item1 = new ListViewItem(new[] { "i123", "Joe", "55" });

for (int i=0; i<3; i++)
{
    if (i == 1)
       item1.SubItems[i].Tag = true;
    else
       item1.SubItems[i].Tag = false;
}

然后进行命中测试:

if (SelectedLSI == null || (bool)SelectedLSI.Tag == false)
   return;

我自己喜欢子项标签法……

【讨论】:

  • 凯文,非常感谢您的建议。我刚刚了解了 subItem 标签 :)
  • 很高兴它有帮助!
猜你喜欢
  • 2015-07-26
  • 1970-01-01
  • 2011-02-21
  • 2022-08-24
  • 2011-05-09
  • 2019-05-11
  • 1970-01-01
  • 2023-02-19
  • 1970-01-01
相关资源
最近更新 更多