练习-用户界面设计

Ag
  1. 界面跨框架搭建,将列表框放入框架中,借助框架的好处是移动时可以一起移动


    效果框

    2.展示第一个框




    3.展示第二和第三个框

逻辑:点击第一个框时,第二框会被展示,第三个框同理

完整代码

Dim arr()
Dim ID As String
Dim price As Long

Private Sub ListBox4_Click()

End Sub

Private Sub UserForm_Activate()

Dim dic As New Dictionary
arr = Sheet2.Range("a2:e" & Sheet2.Range("a65536").End(xlUp).Row)

For i = LBound(arr) To UBound(arr)
    dic(arr(i, 2)) = 1
Next
'1.展示第一个框'
Me.ListBox1.List = dic.Keys

End Sub

Private Sub ListBox1_Click()
'点击第一个框时展示第二个框'
Dim dic As New Dictionary

For i = LBound(arr) To UBound(arr)
    If arr(i, 2) = Me.ListBox1.Value Then
        dic(arr(i, 3)) = 1
    End If
Next

Me.ListBox2.List = dic.Keys
'点击第一个框时清空第三个框'
Me.ListBox3.Clear
'价格也需清空为0'
Me.Label2.Caption = 0

End Sub

Private Sub ListBox2_Click()
'点击第二框时展示第三个框
'
Dim dic As New Dictionary

For i = LBound(arr) To UBound(arr)
    If arr(i, 2) = Me.ListBox1.Value And arr(i, 3) = Me.ListBox2.Value Then
        dic(arr(i, 4)) = 1
    End If
Next

Me.ListBox3.List = dic.Keys
'价格清空为0'
Me.Label2.Caption = 0
End Sub

Private Sub ListBox3_Click()
'点击到第三个框时显示单价'
For i = LBound(arr) To UBound(arr)
    If arr(i, 2) = Me.ListBox1.Value And arr(i, 3) = Me.ListBox2.Value And arr(i, 4) = Me.ListBox3.Value Then
        ID = arr(i, 1)
        price = arr(i, 5)
    End If
Next
Me.Label2.Caption = price
End Sub

Private Sub CommandButton1_Click()
'点击选择按钮是将所选的商品放到购物篮'

'开始前加个判断,如果列表框中没东西则不执行'
If Me.ListBox1.Value <> "" And Me.ListBox2.Value <> "" And Me.ListBox3.Value <> "" And Me.TextBox1.Value > 0 Then
    '增加一个空行行'
    Me.ListBox4.AddItem
    '0行0列相当于第一行第一列'
    Me.ListBox4.List(Me.ListBox4.ListCount - 1, 0) = ID
    Me.ListBox4.List(Me.ListBox4.ListCount - 1, 1) = Me.ListBox1.Value
    Me.ListBox4.List(Me.ListBox4.ListCount - 1, 2) = Me.ListBox2.Value
    Me.ListBox4.List(Me.ListBox4.ListCount - 1, 3) = Me.ListBox3.Value
    Me.ListBox4.List(Me.ListBox4.ListCount - 1, 4) = Me.TextBox1.Value
    Me.ListBox4.List(Me.ListBox4.ListCount - 1, 5) = Me.TextBox1.Value * Me.Label2.Caption
Else

MsgBox "请正确选择商品"
End If

Private Sub CommandButton2_Click()
'删除商品'
'遍历时时从0开始'
For i = 0 To Me.ListBox4.ListCount - 1
    If Me.ListBox4.Selected(i) = True Then
        '删除后减去对应的价格'
        Me.Label5.Caption = Me.Label5.Caption - Me.ListBox4.List(i, 5)
        '移除删除后的这一行'
        Me.ListBox4.RemoveItem i
    End If
Next
End Sub

Private Sub CommandButton3_Click()
'点击结算按钮时进行结算并记录购买的数据'
'订单ID'
Dim DDID As String
Dim i
If Me.ListBox4.ListCount > 0 Then

'往下一行写数据'
i = Sheet2.Range("a65536").End(xlUp).Row + 1
DDID = "D" & Format(VBA.Now, "yyyymmddhhmmss")



For j = 0 To Me.ListBox4.ListCount - 1
Sheet2.Range("a" & i) = DDID
Sheet2.Range("b" & i) = Date
'取ID'
Sheet2.Range("c" & i) = Me.ListBox4.List(j, 0)
'取数量'
Sheet2.Range("d" & i) = Me.ListBox4.List(j, 4)
'取金额'
Sheet2.Range("e" & i) = Me.ListBox4.List(j, 5)
'下一次循环时 i 的行数是下一行 即 i + 1'
i = i + 1
Next


MsgBox "结算成功"
'关闭窗体'
Unload Me


Else

MsgBox "购物清单为空"
End If

End Sub

End Sub


你可能感兴趣的:(练习-用户界面设计)