如何在listView中添加Textbox?
在winform中利用listView显示数据, 想添加Textbox 然后将一列值显示在Textbox中, 默认Textbox为不 可编辑状态 单击选中后 可以编辑 按回车 触发提交按钮
下面是我显示数据的代码
listView textbox winform
Private Sub MSShowData(ByVal obj As Object) Handles mMS.DataRecived
If (Me.InvokeRequired) Then
Me.Invoke(New RecivedDataHandler(AddressOf MSShowData), obj)
Else
Dim value As NewMSEncoder.MSData
value = CType(obj, NewMSEncoder.MSData)
With Me.ListView1
.BeginUpdate()
.Columns.Clear()
.Columns.Add("项目", 160)
.Columns.Add("值", 240)
.Columns.Add("输入值", 240)
.Items.Clear()
Dim item As ListViewItem
item = New ListViewItem(New String() {"值1", value.Value1.ToString("F2"),""})
.Items.Add(item)
item = New ListViewItem(New String() {"值2", value.Value2.ToString("F2"),"")})
.Items.Add(item)
item = New ListViewItem(New String() {"值3", value.Value3.ToString("F2"),"")})
.Items.Add(item)
item = New ListViewItem(New String() {"值4", value.Value4.ToString("F2"),""})
.Items.Add(item)
item = New ListViewItem(New String() {"值5", value.Value4.ToString("F2"),""})
.Items.Add(item)
item = New ListViewItem(New String() {"值6", value.Value5.ToString("F2"),""})
.Items.Add(item)
.EndUpdate()
End With
End If
End Sub
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 EditBox As System.Windows.Forms.TextBox
Private m_fontEdit As Font
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()
EditFont = Me.Font
EditBgColor = Color.LightBlue
AddHandler Me.MouseDown, AddressOf mxh_MouseDown
' Me.MouseDown += New System.Windows.Forms.MouseEventHandler(Me.SMKMouseDown)
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 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
Private Sub EditOver(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
If e.KeyChar = "13" Then
m_currentLVItem.SubItems(m_nSubItemSelected).Text = EditBox.Text
EditBox.Hide()
End If
If e.KeyChar = "27" 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)
' Check the subitem clicked .
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
'编辑状态下显示为文本框
End Enum
Me.ListView1= New MSS.Mylist
Private Sub MSShowData(ByVal obj As Object) Handles mMS.DataRecived
If (Me.InvokeRequired) Then
Me.Invoke(New RecivedDataHandler(AddressOf MSShowData), obj)
Else
Dim value As NewMSEncoder.MSData
value = CType(obj, NewMSEncoder.MSData)
With Me.ListView1
.BeginUpdate()
.Columns.Clear()
Dim header1 As New M_ColumnHeader(M_ListViewColumnStyle.[ReadOnly])
header1.Width = 180
header1.Text = "项目"
.Columns.Add(header1)
Dim header2 As New M_ColumnHeader(M_ListViewColumnStyle.[ReadOnly])
header2.Width = 240
header2.Text = "值"
.Columns.Add(header2)
Dim header3 As New M_ColumnHeader(M_ListViewColumnStyle.[ReadOnly])
header3.Width = 240
header3.Text = "输入值"
.Columns.Add(header3)
.FullRowSelect = True
.Items.Clear()
Dim item As ListViewItem
item = New ListViewItem(New String() {"值1", value.Value1.ToString("F2"),""})
.Items.Add(item)
item = New ListViewItem(New String() {"值2", value.Value2.ToString("F2"),"")})
.Items.Add(item)
item = New ListViewItem(New String() {"值3", value.Value3.ToString("F2"),"")})
.Items.Add(item)
item = New ListViewItem(New String() {"值4", value.Value4.ToString("F2"),""})
.Items.Add(item)
item = New ListViewItem(New String() {"值5", value.Value4.ToString("F2"),""})
.Items.Add(item)
item = New ListViewItem(New String() {"值6", value.Value5.ToString("F2"),""})
.Items.Add(item)
.EndUpdate()
End With
End If
End Sub