VBA 类代码详解:ListViewClass
这段 VBA 代码定义了一个名为 'ListViewClass' 的类,它包含了一些公共属性和方法,以及一些私有属性和方法。以下是每行代码的详细解释:
Public UserFormName As String
Public DisplayCount As Integer
Public QueryOnAction As String
Public QueryOnAction1 As String
Public ListControls As Collection
这段代码定义了类的公共属性。
- UserFormName: 表示用户窗体的名称。
- DisplayCount: 表示每页显示的行数。
- QueryOnAction: 表示执行查询操作的过程名称。
- QueryOnAction1: 表示执行第二个查询操作的过程名称。
- ListControls: 表示控件集合。
Private AddMethodName_ As String
Private DataSource_ As Variant
Private StartRow_ As Integer
Private EndRow_ As Integer
Private Count As Integer
Private WithEvents ListView_ As ListView
Private WithEvents Left_ As MSForms.CommandButton
Private WithEvents Right_ As MSForms.CommandButton
Private WithEvents HomePage_ As MSForms.CommandButton
Private WithEvents LastPage_ As MSForms.CommandButton
Private WithEvents Newly_ As MSForms.CommandButton
Private WithEvents Query_ As MSForms.CommandButton
Private WithEvents Reset_ As MSForms.CommandButton
Private WithEvents StartDate_ As MSForms.TextBox
Private WithEvents EndDate_ As MSForms.TextBox
这段代码定义了类的私有属性。
- AddMethodName_: 表示添加方法的名称。
- DataSource_: 表示数据源。
- StartRow_: 表示当前页的起始行。
- EndRow_: 表示当前页的结束行。
- Count: 表示数据行数。
- ListView_: 表示 ListView 控件。
- Left_: 表示左翻页按钮。
- Right_: 表示右翻页按钮。
- HomePage_: 表示首页按钮。
- LastPage_: 表示尾页按钮。
- Newly_: 表示新建按钮。
- Query_: 表示查询按钮。
- Reset_: 表示重置按钮。
- StartDate_: 表示开始日期文本框。
- EndDate_: 表示结束日期文本框。
Public Property Let Newly(ByVal x As MSForms.CommandButton)
Set Newly_ = x
End Property
Public Property Let Query(ByVal x As MSForms.CommandButton)
Set Query_ = x
End Property
Public Property Let Reset(ByVal x As MSForms.CommandButton)
Set Reset_ = x
End Property
Public Property Let ListView(ByVal x As ListView)
Set ListView_ = x
End Property
Public Property Let Left(ByVal x As MSForms.CommandButton)
Set Left_ = x
End Property
Public Property Let Right(ByVal x As MSForms.CommandButton)
Set Right_ = x
End Property
Public Property Let HomePage(ByVal x As MSForms.CommandButton)
Set HomePage_ = x
End Property
Public Property Let LastPage(ByVal x As MSForms.CommandButton)
Set LastPage_ = x
End Property
Public Property Let StartDate(ByVal x As MSForms.TextBox)
Set StartDate_ = x
End Property
Public Property Let EndDate(ByVal x As MSForms.TextBox)
Set EndDate_ = x
End Property
Public Property Let AddMethodName(ByVal x As String)
AddMethodName_ = x
End Property
这段代码定义了类的公共属性访问器,用于设置各种控件。
Public Property Get GetDatSourceID() As String
GetDatSourceID = ListView_.SelectedItem.Tag
End Property
这段代码定义了一个公共属性访问器,用于获取 ListView 控件中选定行的标签值。
Public Property Let DataSource(ByVal x As Variant)
DataSource_ = x
Count = UBound(DataSource_, 2)
StartRow_ = 0
If Count > DisplayCount Then
EndRow_ = DisplayCount
Right_.Enabled = True
LastPage_.Enabled = True
Else
EndRow_ = Count
Right_.Enabled = False
LastPage_.Enabled = False
End If
Left_.Enabled = False
HomePage_.Enabled = False
AddListView
End Property
这段代码定义了一个公共属性访问器,用于设置数据源。它还根据数据源中的行数计算出当前页的起始行和结束行,并设置各种控件的 Enabled 属性,最后调用 AddListView 方法将数据添加到 ListView 控件中。
Public Sub CommandButtonEnabled_False()
Right_.Enabled = False
Left_.Enabled = False
HomePage_.Enabled = False
LastPage_.Enabled = False
ListView_.ListItems.Clear
End Sub
这段代码定义了一个公共方法,用于禁用所有的命令按钮,并清空 ListView 控件中的行。
Private Sub AddListView()
Dim i As Integer, r As Integer, iCount As Integer
With ListView_.ListItems
.Clear
iCount = UBound(DataSource_)
For i = StartRow_ To EndRow_
If Not IsNull(DataSource_(0, i)) Then
With .Add
.Tag = DataSource_(0, i)
.Text = DataSource_(1, i)
For r = 2 To iCount
If Not IsNull(DataSource_(r, i)) Then
.SubItems(r - 1) = DataSource_(r, i)
End If
Next
End With
End If
Next
End With
End Sub
这段代码定义了一个私有方法,用于将数据添加到 ListView 控件中。
Private Sub Query__Click()
Application.Run QueryOnAction
If QueryOnAction1 <> '' Then
Application.Run QueryOnAction1
End If
End Sub
这段代码定义了一个私有方法,用于执行查询操作。
Private Sub Reset__Click()
Dim i As Integer
For i = 1 To ListControls.Count
ListControls(i).Text = ''
Next
Query__Click
End Sub
这段代码定义了一个私有方法,用于重置所有的输入控件,并执行查询操作。
Private Sub Newly__Click()
Dim i As Integer
For i = 0 To VBA.UserForms.Count - 1
If VBA.UserForms(i).Name = UserFormName Then
Unload VBA.UserForms(i)
Exit For
End If
Next
VBA.UserForms.Add(UserFormName).Newly
End Sub
这段代码定义了一个私有方法,用于关闭当前窗体并打开一个新的窗体。
'Private Sub ListView__DblClick()
' Dim i As Integer
' If ListView_.SelectedItem Is Nothing Then Exit Sub
' For i = 0 To VBA.UserForms.Count - 1
' If VBA.UserForms(i).Name = UserFormName Then
' Unload VBA.UserForms(i)
' Exit For
' End If
' Next
' CallByName VBA.UserForms.Add(UserFormName), AddMethodName_, VbMethod, ListView_.SelectedItem.Tag
'End Sub
这段代码是注释掉的,原本是用于处理 ListView 控件的双击事件。
Private Sub HomePage__Click()
Right_.Enabled = True
LastPage_.Enabled = True
Left_.Enabled = False
HomePage_.Enabled = False
StartRow_ = 0
EndRow_ = DisplayCount
AddListView
End Sub
这段代码定义了一个私有方法,用于将 ListView 控件的当前页设置为第一页。
Private Sub Left__Click()
Right_.Enabled = True
LastPage_.Enabled = True
StartRow_ = StartRow_ - (DisplayCount + 1)
If StartRow_ = 0 Then
Left_.Enabled = False
HomePage_.Enabled = False
EndRow_ = DisplayCount
Else
EndRow_ = StartRow_ + DisplayCount
End If
AddListView
End Sub
这段代码定义了一个私有方法,用于将 ListView 控件的当前页向左翻一页。
Private Sub Right__Click()
StartRow_ = StartRow_ + DisplayCount + 1
Left_.Enabled = True
HomePage_.Enabled = True
If StartRow_ + DisplayCount < Count Then
EndRow_ = StartRow_ + DisplayCount
Right_.Enabled = True
LastPage_.Enabled = True
Else
EndRow_ = Count
Right_.Enabled = False
LastPage_.Enabled = False
End If
AddListView
End Sub
这段代码定义了一个私有方法,用于将 ListView 控件的当前页向右翻一页。
Private Sub LastPage__Click()
Dim S As Integer
Right_.Enabled = False
LastPage_.Enabled = False
Left_.Enabled = True
HomePage_.Enabled = True
S = Int(Count / (DisplayCount + 1)) * (DisplayCount + 1)
StartRow_ = S
EndRow_ = Count
AddListView
End Sub
这段代码定义了一个私有方法,用于将 ListView 控件的当前页设置为最后一页。
Private Sub StartDate__MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal y As Single)
Dim dateValue As Date
If Button = 1 Then
dateValue = CalendarForm.GetDate
If dateValue > 0 Then
StartDate_.Text = dateValue
End If
End If
End Sub
这段代码定义了一个私有方法,用于弹出日历控件,让用户选择开始日期。
Private Sub EndDate__MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal y As Single)
Dim dateValue As Date
If Button = 1 Then
dateValue = CalendarForm.GetDate
If dateValue > 0 Then
EndDate_.Text = dateValue
End If
End If
End Sub
这段代码定义了一个私有方法,用于弹出日历控件,让用户选择结束日期。
Private Sub Class_Initialize()
AddMethodName_ = 'Add'
Set ListControls = New Collection
End Sub
这段代码定义了一个类的初始化方法,用于初始化 AddMethodName_ 和 ListControls 属性。
总而言之,这段 VBA 代码定义了一个用于管理 ListView 控件的类,它包含了数据源处理、分页、查询、重置等功能。该类可以方便地用于各种应用程序中,以实现数据列表的展示和操作。
原文地址: https://www.cveoy.top/t/topic/lBhA 著作权归作者所有。请勿转载和采集!