Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使某个listView子项列可编辑_C#_Winforms_Listview - Fatal编程技术网

C# 使某个listView子项列可编辑

C# 使某个listView子项列可编辑,c#,winforms,listview,C#,Winforms,Listview,我已经找到了很多使listView列可编辑的好信息(包括这个解决方案)。但是,我找不到关于如何将某些功能锁定到特定列的子项的太多信息,我的尝试失败了 请考虑下面的代码: // Make ListView Editable ListViewItem.ListViewSubItem SelectedLSI; private void DwgList_MouseUp(object sender, MouseEventArgs e) { ListViewH

我已经找到了很多使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 = "";
        }
    }
编辑: 请参见下面的图片以获取视觉解释

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


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

问题是您确实不知道自己在哪一列。我可以想出几个方法来解决这个问题:

1) 实现本文中的代码以获取列号并测试:

2) 将子项标记设置为true或false以确定编辑能力,然后在命中测试中检查标记:

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;

我自己也喜欢子项标记方法…

只有一个,即第一个主列可以编辑。但是你可以根据需要重新排序列。-还有一些变通办法……是的,这是一个变通办法。它允许使用隐藏文本框编辑所有列。我只想将功能限制在第4列,不过(子项3)是WPF、Winforms、ASP、Net或其他内容。请以一种有用的方式标记问题。我希望如果有很多第三方网格控件,然后尝试使用ListView来执行复杂的任务,那么使用一个会更好。(在鼠标下方的正确位置显示文本框等会起作用,但需要很长时间才能正确查看。)may Help Kevin,非常感谢您的建议。我刚刚了解了子项标记:)很高兴它有帮助!