Public Class Form1
' 定义商品类
Class Commodity
Public Name As String
Public Quantity As Integer
Public Price As Double
End Class
' 商品列表
Private commodities As New List(Of Commodity)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' 初始化一些商品数据
commodities.Add(New Commodity With {.Name = "商品 1",.Quantity = 100,.Price = 20.0})
commodities.Add(New Commodity With {.Name = "商品 2",.Quantity = 80,.Price = 30.0})
commodities.Add(New Commodity With {.Name = "商品 3",.Quantity = 120,.Price = 25.0})
' 初始化界面元素
Button1.Text = "添加商品"
Button2.Text = "查看库存"
Button3.Text = "出库商品"
Button4.Text = "继续"
Button5.Text = "退出"
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' 添加商品的逻辑
Dim name As String = InputBox("请输入商品名称")
Dim quantity As Integer = CInt(InputBox("请输入商品数量"))
Dim price As Double = CDbl(InputBox("请输入商品价格"))
Dim newCommodity As New Commodity
newCommodity.Name = name
newCommodity.Quantity = quantity
newCommodity.Price = price
commodities.Add(newCommodity)
MessageBox.Show("商品添加成功!")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
' 查看库存的逻辑
Dim listText As String = "库存列表:" & vbCrLf
For Each commodity In commodities
listText &= $"名称:{commodity.Name},数量:{commodity.Quantity},价格:{commodity.Price}" & vbCrLf
Next
MessageBox.Show(listText)
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
' 出库商品的逻辑
Dim outName As String = InputBox("请输入要出库的商品名称")
Dim foundCommodity As Commodity = Nothing
For Each commodity In commodities
If commodity.Name = outName Then
foundCommodity = commodity
Exit For
End If
Next
If foundCommodity IsNot Nothing Then
Dim outQuantity As Integer = CInt(InputBox("请输入出库数量"))
If outQuantity <= foundCommodity.Quantity Then
foundCommodity.Quantity -= outQuantity
MessageBox.Show($"出库成功,剩余数量:{foundCommodity.Quantity}")
Else
MessageBox.Show("库存不足,无法出库。")
End If
Else
MessageBox.Show("未找到该商品。")
End If
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
' 继续(返回主菜单)的逻辑
MessageBox.Show("返回主菜单")
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
' 退出的逻辑
Me.Close()
End Sub
End Class
标签:Dim,VB,Name,Commodity,commodity,commodities,Visual,窗体,Sub From: https://blog.csdn.net/qq_32257509/article/details/141355554