自定义listview可编辑列 如何通过可编辑行执行指定的操作?
--------------------编程问答-------------------- 补充:自定义listview 实现可编辑列
--------------------编程问答-------------------- 没人知道怎么解决吗? 高手都死哪去了 --------------------编程问答-------------------- 除 --------------------编程问答--------------------
Public Class Mylist : Inherits ListView
Private m_currentLVItem As ListViewItem
Private m_nX As Integer = 0
Private m_nY As Integer = 0
Private m_strSubItemText As String
Private m_nSubItemSelected As Integer = 0
Private m_arrComboBoxes As ComboBox() = New ComboBox(19) {}
Private EditBox As System.Windows.Forms.TextBox
Private m_fontComboBox As Font
Private m_fontEdit As Font
Private m_bgcolorComboBox As Color
Private m_bgcolorEdit As Color
Public Sub New()
MyBase.New()
SetStyle(ControlStyles.DoubleBuffer Or ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer, True)
UpdateStyles()
Mylist()
End Sub
Public Sub Mylist()
EditBox = New System.Windows.Forms.TextBox()
ComboBoxFont = Me.Font
EditFont = Me.Font
EditBgColor = Color.LightBlue
Me.m_bgcolorComboBox = Color.LightBlue
AddHandler Me.MouseDown, AddressOf mxh_MouseDown
AddHandler Me.DoubleClick, AddressOf mxh_DoubleClick
Me.GridLines = True
EditBox.Size = New System.Drawing.Size(0, 0)
EditBox.Location = New System.Drawing.Point(0, 0)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.EditBox})
AddHandler EditBox.KeyPress, AddressOf EditOver
AddHandler EditBox.LostFocus, AddressOf FocusOver
EditBox.AutoSize = True
EditBox.Font = Me.EditFont
EditBox.BackColor = Me.EditBgColor
EditBox.BorderStyle = BorderStyle.FixedSingle
EditBox.Hide()
EditBox.Text = ""
End Sub
Public Property ComboBoxFont() As Font
Get
Return Me.m_fontComboBox
End Get
Set(ByVal value As Font)
Me.m_fontComboBox = value
End Set
End Property
Public Property ComboBoxBgColor() As Color
Get
Return Me.m_bgcolorComboBox
End Get
Set(ByVal value As Color)
Me.m_bgcolorComboBox = value
For i As Integer = 0 To Me.m_arrComboBoxes.Length - 1
If m_arrComboBoxes(i) IsNot Nothing Then
m_arrComboBoxes(i).BackColor = Me.m_bgcolorComboBox
End If
Next
End Set
End Property
Public Property EditFont() As Font
Get
Return Me.m_fontEdit
End Get
Set(ByVal value As Font)
Me.m_fontEdit = value
Me.editBox.Font = Me.m_fontEdit
End Set
End Property
Public Property EditBgColor() As Color
Get
Return Me.m_bgcolorEdit
End Get
Set(ByVal value As Color)
Me.m_bgcolorEdit = value
Me.editBox.BackColor = Me.m_bgcolorEdit
End Set
End Property
Public Sub SetColumn(ByVal columnIndex As Integer, ByVal cs As M_ListViewColumnStyle)
If columnIndex < 0 OrElse columnIndex > Me.Columns.Count Then
Throw New Exception("超出列索引范围")
End If
DirectCast(Columns(columnIndex), M_ColumnHeader).ColumnStyle = cs
End Sub
Public Sub BoundListToColumn(ByVal columnIndex As Integer, ByVal items As String())
If columnIndex < 0 OrElse columnIndex > Me.Columns.Count Then
Throw New Exception("超出列索引范围")
End If
If DirectCast(Columns(columnIndex), M_ColumnHeader).ColumnStyle <> M_ListViewColumnStyle.ComboBox Then
Throw New Exception("列应该是ComboBox风格")
End If
Dim newbox As New ComboBox()
For i As Integer = 0 To items.Length - 1
newbox.Items.Add(items(i))
Next
newbox.Size = New System.Drawing.Size(0, 0)
newbox.Location = New System.Drawing.Point(0, 0)
Me.Controls.AddRange(New System.Windows.Forms.Control() {newbox})
AddHandler newbox.SelectedIndexChanged, AddressOf CmbSelected
AddHandler newbox.LostFocus, AddressOf CmbFocusOver
AddHandler newbox.KeyPress, AddressOf CmbKeyPress
newbox.Font = Me.ComboBoxFont
newbox.BackColor = Me.ComboBoxBgColor
newbox.DropDownStyle = ComboBoxStyle.DropDownList
newbox.Hide()
Me.m_arrComboBoxes(columnIndex) = newbox
End Sub
Private Sub CmbKeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
Dim cmbBox As ComboBox = DirectCast(sender, ComboBox)
If e.KeyChar = "13" OrElse e.KeyChar = "27" Then
'CR or ESC press
cmbBox.Hide()
End If
End Sub
Private Sub CmbSelected(ByVal sender As Object, ByVal e As System.EventArgs)
Dim cmbBox As ComboBox = DirectCast(sender, ComboBox)
Dim sel As Integer = cmbBox.SelectedIndex
If sel >= 0 Then
Dim itemSel As String = cmbBox.Items(sel).ToString()
m_currentLVItem.SubItems(m_nSubItemSelected).Text = itemSel
End If
End Sub
Private Sub CmbFocusOver(ByVal sender As Object, ByVal e As System.EventArgs)
Dim cmbBox As ComboBox = DirectCast(sender, ComboBox)
cmbBox.Hide()
End Sub
Private Sub EditOver(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
Dim a As Char = Convert.ToChar(Keys.Enter)
Dim b As Char = Convert.ToChar(Keys.Escape)
If e.KeyChar = a Then '"&H0d"
m_currentLVItem.SubItems(m_nSubItemSelected).Text = EditBox.Text
MessageBox.Show(EditBox.Text, "提示", MessageBoxButtons.OK, MessageBoxIcon.None)
EditBox.Hide()
End If
If e.KeyChar = b Then
EditBox.Hide()
End If
End Sub
Private Sub FocusOver(ByVal sender As Object, ByVal e As System.EventArgs)
m_currentLVItem.SubItems(m_nSubItemSelected).Text = editBox.Text
editBox.Hide()
End Sub
Public Sub mxh_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs)
Dim nStart As Integer = m_nX
'current mouse down X position
Dim spos As Integer = 0
Dim epos As Integer = Me.Columns(0).Width
For i As Integer = 0 To Me.Columns.Count - 1
If nStart > spos AndAlso nStart < epos Then
m_nSubItemSelected = i
Exit For
End If
spos = epos
epos += Me.Columns(i).Width
Next
m_strSubItemText = m_currentLVItem.SubItems(m_nSubItemSelected).Text
Dim column As M_ColumnHeader = DirectCast(Columns(m_nSubItemSelected), M_ColumnHeader)
If column.ColumnStyle = M_ListViewColumnStyle.EditBox Then
Dim r As New Rectangle(spos, m_currentLVItem.Bounds.Y, epos, m_currentLVItem.Bounds.Bottom)
EditBox.Size = New System.Drawing.Size(epos - spos, m_currentLVItem.Bounds.Height)
EditBox.Location = New System.Drawing.Point(spos, m_currentLVItem.Bounds.Y)
EditBox.Show()
EditBox.Text = m_strSubItemText
EditBox.SelectAll()
EditBox.Focus()
End If
End Sub
Public Sub mxh_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
m_currentLVItem = Me.GetItemAt(e.X, e.Y)
m_nX = e.X
m_nY = e.Y
End Sub
End Class
Public Class M_ColumnHeader
Inherits ColumnHeader
Private cs As M_ListViewColumnStyle
'本列的风格
Public Sub New()
MyBase.New()
cs = M_ListViewColumnStyle.[ReadOnly]
End Sub
Public Sub New(ByVal _cs As M_ListViewColumnStyle)
cs = _cs
End Sub
Public Property ColumnStyle() As M_ListViewColumnStyle
Get
Return cs
End Get
Set(ByVal value As M_ListViewColumnStyle)
cs = value
End Set
End Property
End Class
Public Enum M_ListViewColumnStyle
[ReadOnly]
'只读
EditBox
'编辑状态下显示为文本框
ComboBox
'编辑状态下显示为组合框
End Enum
补充:.NET技术 , VB.NET