7. Ví dụ về sử dụng control ListView trong thực tiễn
Sau đây ta sẽ sử dụng ListView để hiển thị/không hiển thị comment trên Excel.
Bạn cần đăng nhập để thấy đính kèm
Khởi tạo UserForm, nạp comment vào ListView:
Private Sub UserForm_Initialize()
With ListView1
''Property
.View = lvwReport ''Hie thi
.LabelEdit = lvwManual ''Bien tap Label
.HideSelection = False ''Tu dong huy select
.AllowColumnReorder = True ''Cho phep thay doi be rong cua cot
.FullRowSelect = True ''Select ca dong du lieu
.Gridlines = True ''Hien thi duong luoi
''Tao truong du lieu
.ColumnHeaders.Add , "_Address", "DiaChi", 46
.ColumnHeaders.Add , "_View", "HienThi", 46
.ColumnHeaders.Add , "_Text", "NoiDung", 126
''Lay comment
Call GetComments
CheckBox1 = Application.DisplayCommentIndicator > 0
End With
End Sub
Dòng code 17:
Để hiển thị toàn bộ comment trong một sheet của Excel ta có thuộc tính
DisplayCommentIndicator của Application Object. Thuộc tính này là 0 thì các comment không hiển thị, nếu là 1 thì các comment sẽ được hiển thị. Giá trị khởi tạo của checkbox1 sẽ phản ánh trạng thái này.
Thủ tục lấy comment:
Sub GetComments() ''Lay tat ca comment
Dim Memo
With ListView1
.ListItems.Clear
For Each Memo In ActiveSheet.Comments
With .ListItems.Add
.Text = Memo.Parent.Address(0, 0)
.SubItems(1) = Memo.Visible
.SubItems(2) = Memo.Text
End With
Next Memo
If .ListItems.Count > 0 Then .ListItems(1).Selected = True
End With
End Sub
Chú ý, thủ tục này được viết trong UserForm.
Xử lý của CheckBox:
1. CheckBox1: Cho hiển thị/không hiển thị các comment trên sheet của Excel.
2. CheckBox2: Cho hiển thị/không hiển thị
Bạn cần đăng nhập để thấy link
của comment trên ListView.
Private Sub CheckBox1_Click() ''[Hien thi comment]
Dim SelectedRow As Long
Application.DisplayCommentIndicator = (CheckBox1 + 1) * (-2) + 1
With ListView1
If .ListItems.Count = 0 Then Exit Sub
SelectedRow = ListView1.SelectedItem.Index
Call GetComments
ListView1.ListItems(SelectedRow).Selected = True
End With
End Sub
Dòng code số 3 sẽ khiến chúng ta hơi phân vân một chút. Nó có tác dụng là làm hiển thị/không hiển thị các comment trên sheet của Excel.
Nếu viết code bằng If hay Select Case thì có lẽ dễ hiểu hơn. CheckBox nếu được select nó có giá trị là -1, nếu không được select nó có giá trị là 0.
Khi thủ tục GetComments được gọi thì ListView sẽ bị Clear, không còn lưu trữ thông tin gì. Cũng tại thủ tục GetComments sẽ nạp lại cho ListView (sau khi đã clear thì nạp lại thông tin).
Vì lý do đã bị Clear trước đó cho nên dòng code số 8 trả lại giá trị dòng đã được selected trước đó.
Private Sub CheckBox2_Click() ''[Khong hien thi Author]
Dim i As Long, buf As String
With ListView1
If .ListItems.Count = 0 Then Exit Sub
If CheckBox2 Then
For i = 1 To .ListItems.Count
buf = Range(.ListItems(i)).Comment.Text
If Right(Left(buf, InStr(buf, vbLf) - 1), 1) = ":" Then
.ListItems(i).SubItems(2) = Mid(buf, InStr(buf, vbLf) + 1)
End If
Next i
Else
For i = 1 To .ListItems.Count
.ListItems(i).SubItems(2) = Range(.ListItems(i)).Comment.Text
Next i
End If
End With
End Sub
Phần code trên sẽ tách bỏ tác giả (Author) hoặc giữ nguyên như bình thường.
Select tới Cells có comment:
Trên UserForm, bạn click vào dòng bất kỳ trên ListView, ta mong muốn trên sheet Excel sẽ được select vào cells chứa comment tương ứng với dòng trên ListView đó:
Private Sub ListView1_DblClick()
Range(ListView1.SelectedItem).Select
End Sub
Delete Comment:
Trên UserForm, bạn click vào dòng bất kỳ trên ListView, bạn ấn phím Delete, đồng thời comment trên sheet của Excel bị xóa và dòng dữ liệu trên ListView cũng bị xóa:
Private Sub ListView1_KeyUp(KeyCode As Integer, ByVal Shift As Integer)
With ListView1
If .ListItems.Count = 0 Then Exit Sub
If KeyCode = 46 Then
Range(.SelectedItem).ClearComments
Call GetComments
End If
End With
End Sub
Download File demo:
Bạn cần đăng nhập để thấy link
.
Nguồn tham khảo:
Bạn cần đăng nhập để thấy link