vb.net 在 ListView 中使单元格可编辑

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

Make a cell editable in a ListView

vb.netvisual-studio-2010listview

提问by gubbfett

I'm sitting in a VB-project and i have a ListView with 3 cells; a checkbox, a name and another text value. I want the last cell to be editable for the user. The second cell is also okay to have editable, no problems at all.

我坐在一个 VB 项目中,我有一个包含 3 个单元格的 ListView;一个复选框、一个名称和另一个文本值。我希望最后一个单元格可供用户编辑。第二个单元格也可以编辑,完全没有问题。

How can i set cells to be editable?

如何将单元格设置为可编辑?

Right now i'm having this code to fill the ListBox:

现在我有这个代码来填充列表框:

Dim row(3) As String
Dim itm As ListViewItem
Dim txtbox As New TextBox

row(0) = ""
row(1) = "some value"
row(2) = "0"


itm = New ListViewItem(row)
itm.SubItems.Add("txtbox")

ListView1.Items.Add(itm

Any tips how to let the user change the last (or both second and last) cell?

任何提示如何让用户更改最后一个(或第二个和最后一个)单元格?

回答by Steve

The Winforms ListView control cannot be used like a DataGrid.
Only the first item can be made editable setting the property LabelEdit=True.

Winforms ListView 控件不能像 DataGrid 一样使用。
只有第一项可以通过设置属性 LabelEdit=True 来编辑。

If you really want to be able to edit any 'cells' of the ListView when in Detail mode, you have two possibilities:

如果您真的希望能够在 Detail 模式下编辑 ListView 的任何“单元格”,您有两种可能性:

  • Switch to a DataGrid
  • Use one of the many extension available for free on the net (An example can be found here)
  • 切换到数据网格
  • 使用网上免费提供的众多扩展程序之一(可以在此处找到示例)

回答by Eddy Jawed

You can't edit a particular row in a listview but you can remove, and then re-add a value. Unfortunately I have no idea how to add multiple cells to the same row but know how to remove and add a single column cell in a listview control. Maybe this could be a start.

您不能编辑列表视图中的特定行,但您可以删除,然后重新添加一个值。不幸的是,我不知道如何将多个单元格添加到同一行,但知道如何在列表视图控件中删除和添加单列单元格。也许这可能是一个开始。

Dim item As ListViewItem = ListView1.SelectedItems(0)
Dim intIndex As Integer = item.Index

item.Remove()
ListView1.Items.Insert(intIndex, "New Text")

回答by Penguin

There is trick with editing cells. You have to create offset textbox and put it to place of cell you want to edit (hit test, set location and size).

编辑单元格有技巧。您必须创建偏移文本框并将其放在要编辑的单元格的位置(命中测试,设置位置和大小)。

Dim iRow, iCol as Integer

Private Sub TextOverlay_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        ListView1.Items(iRow).SubItems(iCol).Text = TextOverlay.Text
End Sub

Private Sub ListView1_Click(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseClick
        Dim hit As ListViewHitTestInfo = ListView1.HitTest(e.X, e.Y)
        Dim iWidth As Integer
        For iCol = 0 To hit.Item.SubItems.Count - 1
            If hit.Item.SubItems(iCol).Bounds.Left <= e.X Then
                If iCol = 0 And hit.Item.SubItems.Count > 1 Then
                    If e.X <= hit.Item.SubItems(1).Bounds.Left Then
                        iWidth = hit.Item.SubItems(1).Bounds.Left
                        Exit For
                    End If
                ElseIf e.X <= hit.Item.SubItems(iCol).Bounds.Right Then
                    iWidth = hit.Item.SubItems(iCol).Bounds.Width
                    Exit For
                End If
            End If
        Next
        iRow = hit.Item.Index
        TextOverlay.Left = ListView1.Left + hit.SubItem.Bounds.Left + 3
        TextOverlay.Top = ListView1.Top + hit.SubItem.Bounds.Top
        TextOverlay.Width = iWidth
        TextOverlay.Height = 18
        TextOverlay.Text = hit.SubItem.Text
        TextOverlay.Visible = True
        TextOverlay.ReadOnly = False
End Sub

回答by Ivan P.

There is way to get currently selected item and then use BeginEdit() call on ListViewItem . To dispatch change: ListView.AfterLabelEdit Event

有一种方法可以获取当前选定的项目,然后在 ListViewItem 上使用 BeginEdit() 调用。调度更改:ListView.AfterLabelEdit 事件