|
- Option Explicit
- Option Base 1
- Private WithEvents xLbl As MSForms.Label
- Private WithEvents txtBox As MSForms.TextBox
- Private WithEvents comBox As MSForms.ComboBox
- Private Col As New Collection
- Dim mc, d As Object '基础设置表的自定义名称
- Dim i As Long, j%, x%
- Dim SelItem As MSComctlLib.ListItem
- Dim SelRow As Long '数据库工作表的当前行
- Private Sub UserForm_Initialize()
- 自定义名称管理
- With ListView1
- .FullRowSelect = True '选择整行
- .Gridlines = True '显示格线
- .LabelEdit = lvwManual '标签不可修改
- 'Set .SmallIcons = ImageList1 '借助ImageList控件图片调整行高
- .View = lvwReport '列表型
- .HideColumnHeaders = False '不隐藏列头
- End With
- MultiPage1.Value = 0 '指向基础页
- Call MultiPage1_Change
- End Sub
- Private Sub 自定义名称管理()
- mc = Sheet2.Range("A1").CurrentRegion.Value
- Set d = CreateObject("Scripting.Dictionary")
- For j = 1 To UBound(mc, 2)
- d(mc(1, j)) = j '名称及其列号
- Next
- End Sub
- Private Sub ListView1_ItemClick(ByVal Item As MSComctlLib.ListItem)
- Me.Controls("Text1").Text = Item
- For i = 2 To Col.Count
- Me.Controls("Text" & i).Text = Item.SubItems(i - 1)
- Next
- cmd保存.Enabled = False
- cmd修改.Enabled = True
- cmd删除.Enabled = True
- End Sub
- Private Sub MultiPage1_Change()
- Rem 功能:关联对应工作表
- On Error GoTo Err1 '处理工作表不存在的错误
- Rem 清除列表数据
- ListView1.ColumnHeaders.Clear
- ListView1.ListItems.Clear
- Rem 获取相应记录
- xName = MultiPage1.SelectedItem.Caption '页标签
- Set Sh = Sheets(xName) '工作表
- Database = Sh.Range("A1").CurrentRegion.Value '数据记录
- Frame1.Caption = " " & xName & " "
- If UBound(Database) = 0 Then
- MsgBox "“" & xName & "表”没有任何数据!", vbCritical, "提示"
- Exit Sub
- End If
- Rem 移除旧有的控件
- If Col.Count > 0 Then
- For i = Col.Count To 1 Step -1
- Me.Frame1.Controls.Remove "Text" & i
- Me.Frame1.Controls.Remove "xLbl" & i
- Next
- Set Col = Nothing
- End If
-
- Rem 加载列表头,添加控件
- For j = 1 To UBound(Database, 2)
- ListView1.ColumnHeaders.Add , , Database(1, j)
- Call ControlAdd(j, Database(1, j))
- Next
- Frame1.ScrollHeight = (Int((Col.Count - 1) / 3)) * 36
- Me.Controls("Text1").Enabled = False
- Call cmd模糊查询_Click
- Call cmd新建_Click
- Exit Sub
- Err1:
- MsgBox Err.Description, vbExclamation, "错误"
- Err.Clear
- End Sub
- Private Sub ControlAdd(ByVal Index As Integer, ByVal Name As String) '第 Index 组标签+文字框/列表框
- Dim L, T, Ctl As Control
- L = 12 + ((Index - 1) Mod 3) * 240
- T = 12 + (Int((Index - 1) / 3)) * 24
- Rem 添加标签
- With Me.Frame1.Controls.Add("Forms.Label.1", "xLbl" & Index, True)
- .Move L, T + 3, 48, 18
- .Caption = Name & ":"
- End With
- Rem 添加文本框或复合框
- If d.Exists(Name) Then
- Dim a(), n%, ii%, jj%
- jj = d.Item(Name)
- For ii = 2 To UBound(mc)
- If Trim(mc(ii, jj)) = "" Then Exit For
- n = n + 1: ReDim Preserve a(1, n)
- a(1, n) = Trim(mc(ii, jj))
- Next
- Set Ctl = Me.Frame1.Controls.Add("Forms.ComboBox.1", "Text" & Index, True)
- Ctl.List = WorksheetFunction.Transpose(a)
- Ctl.ShowDropButtonWhen = fmShowDropButtonWhenFocus
- Else
- Set Ctl = Me.Frame1.Controls.Add("Forms.TextBox.1", "Text" & Index, True)
- End If
- Ctl.Move L + 57, T, 132, 18
- Ctl.BorderStyle = 1
- Ctl.BorderColor = &H80000002
- Ctl.ForeColor = &HC00000
- Rem 加入集合
- Col.Add Index
- End Sub
- Private Sub 保存记录并显示明细(ByVal myRow As Long)
- If Trim(Me.Controls("Text2").Text) = "" Then
- MsgBox "姓名不得为空!", vbExclamation, "提示"
- Me.Controls("Text2").SetFocus
- Exit Sub
- End If
- ReDim a(1, Col.Count)
- For i = 1 To Col.Count
- a(1, i) = Trim(Me.Controls("Text" & i).Text)
- Next
- Sheets(xName).Range("A" & myRow).Resize(1, Col.Count) = a
- Call cmd新建_Click
- txtFind.Text = ""
- Call cmd模糊查询_Click
- End Sub
- Private Sub cmd新建_Click()
- For i = 1 To Col.Count
- Me.Controls("Text" & i).Text = ""
- Next
- Me.Controls("Text1").Text = WorksheetFunction.Max(Sheets(xName).Range("A:A")) + 1
- cmd保存.Enabled = True
- cmd修改.Enabled = False
- cmd删除.Enabled = False
- End Sub
- Private Sub cmd保存_Click()
- i = Sheets(xName).Range("A65536").End(3).Row + 1
- Call 保存记录并显示明细(i)
- End Sub
- Private Sub cmd修改_Click()
- i = ListView1.SelectedItem.Tag
- If i < 2 Then Exit Sub
- If MsgBox("您正准备修改 1 条记录。" & vbCrLf & vbCrLf & _
- "如果单击“是”,将无法撤消修改操作。" & vbCrLf & _
- "确实要修改这条记录吗?", vbQuestion + vbYesNo, "修改记录") = vbYes Then
- Call 保存记录并显示明细(i)
- End If
- End Sub
- Private Sub cmd删除_Click()
- i = ListView1.SelectedItem.Tag
- If i < 2 Then Exit Sub
- If MsgBox("您正准备删除 1 条记录。" & vbCrLf & vbCrLf & _
- "如果单击“是”,将无法撤消删除操作。" & vbCrLf & _
- "确实要删除这条记录吗?", vbQuestion + vbYesNo, "删除记录") = vbYes Then
- Sheets(xName).Rows(i).Delete Shift:=xlUp
- cmd模糊查询_Click
- End If
- End Sub
- Private Sub cmd清空列表_Click()
- Label5.Caption = ""
- ListView1.ListItems.Clear
- txtFind = ""
- End Sub
- Private Sub txtFind_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
- If KeyCode = 13 Then Call cmd模糊查询_Click
- End Sub
- Private Sub cmd模糊查询_Click()
- Dim Item As MSComctlLib.ListItem
- ListView1.ListItems.Clear
- Database = Sh.Range("A1").CurrentRegion.Value '数据记录
- If UBound(Database) < 2 Then GoTo Err1
- For i = 2 To UBound(Database)
- For j = 1 To UBound(Database, 2)
- If UCase(Database(i, j)) Like "*" & UCase(txtFind) & "*" Then
- Set Item = ListView1.ListItems.Add()
- Item.Tag = i
- Item.Text = Database(i, 1)
- For x = 2 To UBound(Database, 2)
- Item.SubItems(x - 1) = Database(i, x)
- Next
- Exit For
- End If
- Next
- Next
- Err1:
- Label5.Caption = IIf(UBound(Database) - 1 = 0, "", "在 " & UBound(Database) - 1 & " 条记录中找到 " & ListView1.ListItems.Count & " 个。")
- cmd保存.Enabled = True
- cmd修改.Enabled = False
- cmd删除.Enabled = False
- End Sub
复制代码
老师刚刚看了这个窗体很人性化可以自定义,这个表头我想下移2行,代码是修改那个位置的代码,谢谢
|
|