【问题标题】:Add a ComboBox To ListView Column in a Row?连续添加一个组合框到 ListView 列?
【发布时间】:2021-11-14 13:45:02
【问题描述】:

有时我想在特定行的项目列中向 ListView 添加文本。
有时想显示一个 ComboBox 供用户选择条目。

我需要创建一个从 ListView 控件派生的类吗?

这是我目前所拥有的:

ListView lv = new ListViewEx();
lv.Size = new Size(300, 300);
this.Controls.Add(lv);
lv.View = View.Details;
lv.FullRowSelect = true;
lv.Scrollable = true;

lv.Columns.Add("Column 1");
lv.Columns.Add("Column 2");
lv.Columns.Add("Column 3");
lv.Columns.Add("Column 4");

string[] testresult = new string[] { "Pass", "Pass", "Pass", "Pass" };
testresult = new string[] { "Pass", "Pass", "Pass", "Pass" };
ListViewItem itm;
itm = new ListViewItem(testresult);
lv.Items.Add(itm);

// need to add a ComboBox in the 4th column of this list view item and a way to get the value selected from it
testresult = new string[] { "Pass", "Pass", "Pass", "Click To Enter Pass/Fail Result" };
itm = new ListViewItem(testresult);
lv.Items.Add(itm);

    private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
    {
        var lv = sender as ListView;
        var subItem = lv.HitTest(lv.PointToClient(MousePosition)).SubItem;

        if (subItem != null && e.SubItem == subItem)
        {
            using (var brush = new SolidBrush(SystemColors.Highlight))
            {
                e.Graphics.FillRectangle(brush, e.SubItem.Bounds);
            }

            if (e.SubItem.Text == "Click To Enter Pass/Fail Result")
            {
                TextRenderer.DrawText(e.Graphics, e.SubItem.Text, e.SubItem.Font,
                                      e.Bounds, SystemColors.HighlightText, flags);
            }
            else
            {
                TextRenderer.DrawText(e.Graphics, e.SubItem.Text, e.SubItem.Font,
                                      e.Bounds, SystemColors.HighlightText, flags);
            }
        }
        else
        {
            e.DrawDefault = true;
        }
    }

【问题讨论】:

  • DataGridView 可能更适合这个
  • 您可以使用类似这样的东西:How to get the selected SubItem index in a Listview and highlight it?,来检测 SubItem 是否为空并在 subItem.Bounds 坐标处放置一个 ComboBox(代替或除了突出显示内容之外)。 -- 我同意使用 DataGridView 整体上会更简单。
  • 我尝试了 DataGridView,但 ListView 更适合我的应用程序,Jimi 你能告诉我怎么做吗?
  • 好的。我能够添加该方法 listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e) 但是当e.SubItem.Text ==“单击以输入通过/失败结果”时,如何更改该方法以在该单元格中添加一个组合框。并且还添加一个处理程序来获取在 ComboBox 中选择的部分的结果。请参阅上面的更新代码。,

标签: c# winforms listview


【解决方案1】:

我知道由于“链接失效”而总是不鼓励链接到其他网站,但另一方面,我不适合在此处复制和粘贴整个文档作为答案。

查看 Microsoft 的本指南:Use a ComboBox control to edit data in a ListView control in Visual CSharp

【讨论】:

  • 这仅适用于 ListView 的第一列。这就是为什么我在 cmets 中提出该解决方案:获取每个子项的边界,因为如您所见,OP 希望在一个或多个子项中显示一个 ComboBox,而不必只是第一列。
  • 确实,但这是一个好的开始 基本逻辑已经存在,组合框的隐藏和显示以及选择文本,但您只需要使用子项,具体取决于单击的列号:)
  • 是的,但你不能,通常使用子项,因为你不知道它们的界限是什么。这就是我链接的代码所做的。如果你想接受它并使其适应这个问题,那很好。
  • 具有讽刺意味的是,我发现这个问题是因为我想实现同样的目标 :) 将您的代码与 MS 的示例结合起来可以完全满足我的需要 - 谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多