我遇到了.NET Listview的一个尴尬(即对我来说意外)的行为。该问题可能已经发布过。如果是这样,请为重复道歉:我没有找到它,并希望被指向"""讨论
背景信息:我使用VisualStudio 2013,VB,目标框架4.5
使用列表视图和几个按钮构建表单。 ListView(详细信息视图)最初配置有1列标记为" 1"和2行,每行包含" 1"。
然后有一个按钮,用于添加带标题的列#34; A" (如果它还没有)并且对于每个listviewItem一个子项" a"。同样,对于一个专栏" B"与subitem" b"。 最后有按钮可以删除列" A"和" B"分别
现在,如果我执行以下操作:
我结束了一个专栏" B" (标题)列内容" a",即"删除"栏" A"。 在你周围玩耍时总会发现删除一个列(例如listview.columns.removeBykey(' key'))后,子项目似乎仍然存在,并且不可见#34;如果"任何"之后会添加columnHeader。
我没有寻求帮助来解决编程问题:有一个简单的方法(每当你删除一个列时,也明确地从每个listviewItem中删除相应的子项),但它让我感到困惑这种笨拙(在我看来)的方法是必要的。 也许我没有正确使用/配置列表视图,应该知道这一切。任何评论都表示赞赏。
我复制下面的代码,以便更容易验证任何感兴趣的人。
(设计师代码)
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.lvStd = New System.Windows.Forms.ListView()
Me.btnAdd_A = New System.Windows.Forms.Button()
Me.btnDel_A = New System.Windows.Forms.Button()
Me.btnAdd_B = New System.Windows.Forms.Button()
Me.btnDel_B = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'lvStd
'
Me.lvStd.Dock = System.Windows.Forms.DockStyle.Top
Me.lvStd.Location = New System.Drawing.Point(0, 0)
Me.lvStd.Name = "lvStd"
Me.lvStd.Size = New System.Drawing.Size(661, 207)
Me.lvStd.TabIndex = 0
Me.lvStd.UseCompatibleStateImageBehavior = False
Me.lvStd.View = System.Windows.Forms.View.Details
'
'btnAdd_A
'
Me.btnAdd_A.Location = New System.Drawing.Point(12, 235)
Me.btnAdd_A.Name = "btnAdd_A"
Me.btnAdd_A.Size = New System.Drawing.Size(75, 23)
Me.btnAdd_A.TabIndex = 1
Me.btnAdd_A.Text = "Add A"
Me.btnAdd_A.UseVisualStyleBackColor = True
'
'btnDel_A
'
Me.btnDel_A.Location = New System.Drawing.Point(12, 264)
Me.btnDel_A.Name = "btnDel_A"
Me.btnDel_A.Size = New System.Drawing.Size(75, 23)
Me.btnDel_A.TabIndex = 2
Me.btnDel_A.Text = "Del A"
Me.btnDel_A.UseVisualStyleBackColor = True
'
'btnAdd_B
'
Me.btnAdd_B.Location = New System.Drawing.Point(159, 235)
Me.btnAdd_B.Name = "btnAdd_B"
Me.btnAdd_B.Size = New System.Drawing.Size(75, 23)
Me.btnAdd_B.TabIndex = 3
Me.btnAdd_B.Text = "Add B"
Me.btnAdd_B.UseVisualStyleBackColor = True
'
'btnDel_B
'
Me.btnDel_B.Location = New System.Drawing.Point(159, 264)
Me.btnDel_B.Name = "btnDel_B"
Me.btnDel_B.Size = New System.Drawing.Size(75, 23)
Me.btnDel_B.TabIndex = 4
Me.btnDel_B.Text = "Del B"
Me.btnDel_B.UseVisualStyleBackColor = True
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(661, 474)
Me.Controls.Add(Me.btnDel_B)
Me.Controls.Add(Me.btnAdd_B)
Me.Controls.Add(Me.btnDel_A)
Me.Controls.Add(Me.btnAdd_A)
Me.Controls.Add(Me.lvStd)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
Friend WithEvents lvStd As System.Windows.Forms.ListView
Friend WithEvents btnAdd_A As System.Windows.Forms.Button
Friend WithEvents btnDel_A As System.Windows.Forms.Button
Friend WithEvents btnAdd_B As System.Windows.Forms.Button
Friend WithEvents btnDel_B As System.Windows.Forms.Button
End Class
和&#34; Form&#34;代码:
Public Class Form1
Protected Overrides Sub OnLoad(e As EventArgs)
MyBase.OnLoad(e)
If (Me.DesignMode) Then Return
Me.Configure()
Me.FillList()
End Sub
Private Sub Configure()
Me.lvStd.BeginUpdate()
Me.lvStd.Clear()
Me.lvStd.Columns.Add("1")
Me.lvStd.EndUpdate()
End Sub
Private Sub FillList()
Dim LI As ListViewItem
For k As Integer = 1 To 2
LI = Me.lvStd.Items.Add("1")
Next
End Sub
Private Sub btnAdd_A_Click(sender As Object, e As EventArgs) Handles btnAdd_A.Click
Dim col As ColumnHeader = Me.lvStd.Columns("A")
If (col IsNot Nothing) Then
Return
End If
Me.lvStd.Columns.Add("A", "A")
For Each LI As ListViewItem In Me.lvStd.Items
LI.SubItems.Add("a")
Next
End Sub
Private Sub btnDel_A_Click(sender As Object, e As EventArgs) Handles btnDel_A.Click
Dim col As ColumnHeader = Me.lvStd.Columns("A")
If (col IsNot Nothing) Then
Me.lvStd.Columns.Remove(col)
End If
End Sub
Private Sub btnAdd_B_Click(sender As Object, e As EventArgs) Handles btnAdd_B.Click
Dim col As ColumnHeader = Me.lvStd.Columns("B")
If (col IsNot Nothing) Then
Return
End If
Me.lvStd.Columns.Add("B", "B")
For Each LI As ListViewItem In Me.lvStd.Items
LI.SubItems.Add("b")
Next
End Sub
Private Sub btnDel_B_Click(sender As Object, e As EventArgs) Handles btnDel_B.Click
Dim col As ColumnHeader = Me.lvStd.Columns("B")
If (col IsNot Nothing) Then
Me.lvStd.Columns.Remove(col)
End If
End Sub
End Class