不好意思,太匆忙了,忘了最小值的情况,这次应该对了。
Public LVHeaderHeight As Integer
Public LVItemHeight As Integer
Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox_Comment.Font = ListView_Info.Font
LVHeaderHeight = TextBox_Comment.Height + 7
LVItemHeight = TextBox_Comment.Height + 2
If LVItemHeight < 17 Then LVItemHeight = 17
TextBox_Comment.AutoSize = False
TextBox_Comment.Height = LVItemHeight - 1
End Sub
Private Sub ListView_Info_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView_Info.DoubleClick
Dim i As Integer = 0
Dim ColumnsWidth As Long = 0
While i < ListView_Info.Columns.Count - 1 And ListView_Info.Columns(i).Text <> "Comment" 'Seek the "Comment" column
ColumnsWidth += ListView_Info.Columns(i).Width
i += 1
End While
If ListView_Info.SelectedItems.Count < 1 Then Exit Sub 'Exit if no items in listview
TextBox_Comment.Location = New Point(ColumnsWidth + 2, (ListView_Info.Items.IndexOf(ListView_Info.SelectedItems(0)) - ListView_Info.TopItem.Index) * LVItemHeight + LVHeaderHeight)
TextBox_Comment.Text = ListView_Info.SelectedItems(0).SubItems(i).Text
TextBox_Comment.Width = ListView_Info.Columns(i).Width - 1
TextBox_Comment.Visible = True
TextBox_Comment.Focus()
TextBox_Comment.SelectAll()
End Sub
高度的问题解决了,先要改一下listview的click事件
如下:
Private Sub ListView_Info_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView_Info.DoubleClick
Dim i As Integer = 0
Dim ColumnsWidth As Long = 0
While i < ListView_Info.Columns.Count - 1 And ListView_Info.Columns(i).Text <> "Comment" 'Seek the "Comment" column
ColumnsWidth += ListView_Info.Columns(i).Width
i += 1
End While
If ListView_Info.SelectedItems.Count < 1 Then Exit Sub 'Exit if no items in listview
TextBox_Comment.Location = New Point(ColumnsWidth + 2, (ListView_Info.Items.IndexOf(ListView_Info.SelectedItems(0)) - ListView_Info.TopItem.Index) * (TextBox_Comment.Height + 2) + TextBox_Comment.Height + 8)
TextBox_Comment.Text = ListView_Info.SelectedItems(0).SubItems(i).Text
TextBox_Comment.Width = ListView_Info.Columns(i).Width - 1
TextBox_Comment.Visible = True
TextBox_Comment.Focus()
TextBox_Comment.SelectAll()
End Sub
在再form的load事件中增加
TextBox_Comment.Font = ListView_Info.Font
Dim h As Integer = TextBox_Comment.Height '这个h变量一定要,详细原因我们在msn上可以谈谈
If h < 15 Then h = 15
TextBox_Comment.AutoSize = False
TextBox_Comment.Height = h
Private Sub ListView_Info_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView_Info.DoubleClick
Dim i As Integer = 0
Dim ColumnsWidth As Long = 0
While i < ListView_Info.Columns.Count - 1 And ListView_Info.Columns(i).Text <> "Comment" 'Seek the "Comment" column
ColumnsWidth += ListView_Info.Columns(i).Width
i += 1
End While
If ListView_Info.SelectedItems.Count < 1 Then Exit Sub 'Exit if no items in listview
TextBox_Comment.Location = New Point(ColumnsWidth + 2, ListView_Info.Items.IndexOf(ListView_Info.SelectedItems(0)) * 17 + 20)
TextBox_Comment.Text = ListView_Info.SelectedItems(0).SubItems(i).Text
TextBox_Comment.Width = ListView_Info.Columns(i).Width - 1
TextBox_Comment.Visible = True
TextBox_Comment.Focus()
TextBox_Comment.SelectAll()
End Sub
Private Sub TextBox_Comment_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox_Comment.LostFocus
Dim i As Integer = 0
Dim ColumnsWidth As Long = 0
While i < ListView_Info.Columns.Count - 1 And ListView_Info.Columns(i).Text <> "Comment" 'Seek the "Comment" column
ColumnsWidth += ListView_Info.Columns(i).Width
i += 1
End While
TextBox_Comment.Visible = False
ListView_Info.SelectedItems(0).SubItems(i).Text = TextBox_Comment.Text
End Sub
Private Sub TextBox_Comment_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox_Comment.KeyDown
If e.KeyCode = Keys.Return Then ListView_Info.Focus()
End Sub