其他分享
首页 > 其他分享> > vb存储柜的实现,【一个超实用的小软件】

vb存储柜的实现,【一个超实用的小软件】

作者:互联网

vb存储柜的实现,【一个超实用的小软件】

老规矩,先上效果图
在这里插入图片描述
1初始化

Private Sub Form_Load()
    Dim i As Integer
    If Dir(App.Path & "\data.dat") <> "" Then
        Open App.Path & "\data.dat" For Random As 1 Len = Len(Infos(0))
        For i = 0 To 23
            Get #1, , Infos(i)
        Next
        Close 1
    End If
    Set pic_lighton = LoadPicture(App.Path & "\lighton.bmp")
    Set pic_lightoff = LoadPicture(App.Path & "\lightoff.bmp")
    Set pic_cabineton = LoadPicture(App.Path & "\cabineton.bmp")
    Set pic_cabinetoff = LoadPicture(App.Path & "\cabinetoff.bmp")
    For i = 0 To 23
        If Infos(i).Occupied Then
            Set imgCab(i).Picture = pic_cabinetoff
            Set imgLight(i).Picture = pic_lighton
        Else
            Set imgCab(i).Picture = pic_cabinetoff
            Set imgLight(i).Picture = pic_lightoff
        End If
    Next
     
    Shape1.FillColor = RGB(3, 82, 35)
    DrawNumber ("-------")
End Sub

在这里插入图片描述

在一开始加载的时候,需要读入二进制文件的数据,这个数据是保存了存储柜是否保存了东西,保存密码是多少,保存的东西是什么。然后根据数据进行红灯亮起。

2数据保存

Private Sub Form_Unload(Cancel As Integer)
    Dim i As Integer
    Open App.Path & "\data.dat" For Random As 1 Len = Len(Infos(0))
    For i = 0 To 23
        Put #1, , Infos(i)
    Next
    Close 1
End Sub

既然在加载的时候需要把数据读进来,那么卸载的时候就需要把数据保存起来。

3存放东西,随机出一个密码

 For i = 0 To 23
        If Infos(i).Occupied = False Then Exit For
    Next
    If i > 23 Then
        DrawNumber ("0000000")
        Beep
        Exit Sub
    End If
    
    Do
        Randomize
        n = Rnd * 23
        If Not Infos(n).Occupied Then
            Do
                i = Rnd * 10000000
                strPassword = Right("0000000" & CStr(i), 7)
                For i = 0 To 23
                    If Infos(i).Occupied Then
                        If strPassword = Infos(i).Code Then
                            Exit For
                        End If
                    End If
                Next
                If i > 23 Then Exit Do
            Loop
            Infos(n).Occupied = True
            Infos(n).Code = strPassword
            Exit Do
        End If
    Loop
    
    no = n
    state = 1
    MsgBox "箱号为:" & n + 1 & ",密码为:" & strPassword & "。" & vbCrLf & "请放入物品并关上柜门。", vbInformation, "请记录"
    
    
    imgCab(n).Picture = pic_cabineton
    lblCab(n).Visible = False
    Command1.Visible = True
    Command1.Caption = "放入物品"
    Command1.Left = imgCab(n).Left + 38
    Command1.Top = imgCab(n).Top + 40
    imgGoods.Left = imgCab(n).Left + 32
    imgGoods.Top = imgCab(n).Top + 8

在这里插入图片描述
这里面完整的流程是先知道一个有空的箱子,这里只需要找到没有保存密码的数据就行。然后随机出一个密码,让客户记住就行。

4取物品

Private Sub cmdNumber_Click(Index As Integer)
    Dim i As Integer
    
    If state <> 0 Then Exit Sub
    
    If Len(strInput) = 7 Then strInput = ""
    strInput = strInput & Index
    DrawNumber (strInput)
    
    If Len(strInput) = 7 Then
        For i = 0 To 23
            If Infos(i).Occupied And Infos(i).Code = strInput Then Exit For
        Next
        If i > 23 Then
           ' strInput = ""
            Beep
        Else
            no = i
            state = 2
            imgCab(no).Picture = pic_cabineton
            lblCab(no).Visible = False
            Command1.Visible = True
            Command1.Caption = "取出物品"
            Command1.Left = imgCab(no).Left + 38
            Command1.Top = imgCab(no).Top + 40
            imgGoods.Left = imgCab(no).Left + 32
            imgGoods.Top = imgCab(no).Top + 8
            DrawNumber (no + 1)
            imgGoods.Picture = LoadPicture(App.Path & "\" & Infos(no).Goods & ".bmp")
             imgGoods.Visible = True

        End If
    End If
    
   
End Sub

最后只剩下一个动作了,那就是输入密码取物品。只要连续输入的数字和存储的密码是相同的,那就打开对应的柜子。

标签:vb,pic,End,23,imgCab,存储,实用,no,Infos
来源: https://blog.csdn.net/u011718690/article/details/114680845