我创建了一个简单的比萨计划(针对夏季项目而非教育相关),它使用复选框来确定客户想要的内容。基本上是一个简单的披萨订购计划。 我想知道因为我已经使用了If语句而不是其他参数和变量,是否可以使用乘法数组来简单地使用VB代码......
我被建议在VB.Net中使用Constructors,但目前没有经验,你能帮忙吗? 或者有更简单的方法来创建这个程序......或者更好的编程方式? 如何减少代码中的重复?
Option Strict On
Public Class Form1
Dim CurrentBalance As String
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
Dim SmallPizza As String
SmallPizza = "5"
CurrentBalance = Label4.Text
If CheckBox1.Checked Then
ListBox1.Items.Add("Small Pizza")
Label4.Text = CStr(Val(CurrentBalance) + Val(SmallPizza))
Else
ListBox1.Items.Remove("Small Pizza")
Label4.Text = CStr(Val(CurrentBalance) - Val(SmallPizza))
End If
End Sub
Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
Dim MediumPizza As String
MediumPizza = "7"
CurrentBalance = Label4.Text
If CheckBox2.Checked Then
ListBox1.Items.Add("Medium Pizza")
Label4.Text = CStr(Val(CurrentBalance) + Val(MediumPizza))
Else
ListBox1.Items.Remove("Medium Pizza")
Label4.Text = CStr(Val(CurrentBalance) - Val(MediumPizza))
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
CurrentBalance = Label4.Text
MessageBox("Total cost: £" & CurrentBalance)
End Sub
Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged
Dim LargePizza As String
LargePizza = "9"
CurrentBalance = Label4.Text
If CheckBox3.Checked Then
ListBox1.Items.Add("Large Pizza")
Label4.Text = CStr(Val(CurrentBalance) + Val(LargePizza))
Else
ListBox1.Items.Remove("Large Pizza")
Label4.Text = CStr(Val(CurrentBalance) - Val(LargePizza))
End If
End Sub
Private Sub CheckBox4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox4.CheckedChanged
Dim ExtraLargePizza As String
ExtraLargePizza = "7"
CurrentBalance = Label4.Text
If CheckBox4.Checked Then
ListBox1.Items.Add("Extra Large Pizza")
Label4.Text = CStr(Val(CurrentBalance) + Val(ExtraLargePizza))
Else
ListBox1.Items.Remove("Extra Large Pizza")
Label4.Text = CStr(Val(CurrentBalance) - Val(ExtraLargePizza))
End If
End Sub
Private Sub CheckBox5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox5.CheckedChanged
Dim Ham As String
Ham = "1"
CurrentBalance = Label4.Text
If CheckBox5.Checked Then
ListBox1.Items.Add("Ham")
Label4.Text = CStr(Val(CurrentBalance) + Val(Ham))
Else
ListBox1.Items.Remove("Ham")
Label4.Text = CStr(Val(CurrentBalance) - Val(Ham))
End If
End Sub
Private Sub CheckBox6_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox6.CheckedChanged
Dim Pineapple As String
Pineapple = "1"
CurrentBalance = Label4.Text
If CheckBox6.Checked Then
ListBox1.Items.Add("Pineapple")
Label4.Text = CStr(Val(CurrentBalance) + Val(Pineapple))
Else
ListBox1.Items.Remove("Pineapple")
Label4.Text = CStr(Val(CurrentBalance) - Val(Pineapple))
End If
End Sub
Private Sub CheckBox7_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox7.CheckedChanged
Dim Bananna As String
Bananna = "1"
CurrentBalance = Label4.Text
If CheckBox7.Checked Then
ListBox1.Items.Add("Bananna")
Label4.Text = CStr(Val(CurrentBalance) + Val(Bananna))
Else
ListBox1.Items.Remove("Bananna")
Label4.Text = CStr(Val(CurrentBalance) - Val(Bananna))
End If
End Sub
Private Sub CheckBox8_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox8.CheckedChanged
Dim Meat As String
Meat = "1"
CurrentBalance = Label4.Text
If CheckBox8.Checked Then
ListBox1.Items.Add("Meat")
Label4.Text = CStr(Val(CurrentBalance) + Val(Meat))
Else
ListBox1.Items.Remove("Meat")
Label4.Text = CStr(Val(CurrentBalance) - Val(Meat))
End If
End Sub
Private Sub CheckBox9_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox9.CheckedChanged
Dim ExtraCheese As String
ExtraCheese = "1"
CurrentBalance = Label4.Text
If CheckBox9.Checked Then
ListBox1.Items.Add("Extra Cheese")
Label4.Text = CStr(Val(CurrentBalance) + Val(ExtraCheese))
Else
ListBox1.Items.Remove("Extra Cheese")
Label4.Text = CStr(Val(CurrentBalance) - Val(ExtraCheese))
End If
End Sub
Private Sub CheckBox10_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox10.CheckedChanged
Dim Pepperoni As String
Pepperoni = "1"
CurrentBalance = Label4.Text
If CheckBox10.Checked Then
ListBox1.Items.Add("Pepperoni")
Label4.Text = CStr(Val(CurrentBalance) + Val(Pepperoni))
Else
ListBox1.Items.Remove("Pepperoni")
Label4.Text = CStr(Val(CurrentBalance) - Val(Pepperoni))
End If
End Sub
Private Sub CheckBox11_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox11.CheckedChanged
Dim Special As String
Special = "1"
CurrentBalance = Label4.Text
If CheckBox11.Checked Then
ListBox1.Items.Add("Special")
Label4.Text = CStr(Val(CurrentBalance) + Val(Special))
Else
ListBox1.Items.Remove("Special")
Label4.Text = CStr(Val(CurrentBalance) - Val(Special))
End If
End Sub
Private Sub CheckBox12_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox12.CheckedChanged
Dim Pickup As String
Pickup = "1"
CurrentBalance = Label4.Text
If CheckBox12.Checked Then
ListBox1.Items.Add("Pickup")
Label4.Text = CStr(Val(CurrentBalance) - Val(Pickup))
Else
Label4.Text = CStr(Val(CurrentBalance) + Val(Pickup))
ListBox1.Items.Remove("Pickup")
End If
End Sub
Private Sub CheckBox13_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox13.CheckedChanged
Dim Deliver As String
Deliver = "5"
CurrentBalance = Label4.Text
If CheckBox13.Checked Then
ListBox1.Items.Add("Deliver")
Label4.Text = CStr(Val(CurrentBalance) + Val(Deliver))
Else
Label4.Text = CStr(Val(CurrentBalance) - Val(Deliver))
ListBox1.Items.Remove("Deliver")
End If
End Sub
Private Sub ChesseToastToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChesseToastToolStripMenuItem.Click
Dim CheeseToast As String
CheeseToast = "8"
CurrentBalance = Label4.Text
ListBox1.Items.Add("Cheese Toast")
Label4.Text = CStr(Val(CurrentBalance) + Val(CheeseToast))
End Sub
Private Sub GarlicToastToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GarlicToastToolStripMenuItem.Click
Dim GarlicToast As String
GarlicToast = "11"
CurrentBalance = Label4.Text
ListBox1.Items.Add("Garlic Toast")
Label4.Text = CStr(Val(CurrentBalance) + Val(GarlicToast))
End Sub
Private Sub BreadSticksToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BreadSticksToolStripMenuItem.Click
Dim Breadsticks As String
Breadsticks = "14"
CurrentBalance = Label4.Text
ListBox1.Items.Add("Bread Sticks")
Label4.Text = CStr(Val(CurrentBalance) + Val(Breadsticks))
End Sub
Private Sub CashToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CashToolStripMenuItem.Click
ListBox1.Items.Add("Paying by Cash")
End Sub
Private Sub ChequeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChequeToolStripMenuItem.Click
ListBox1.Items.Add("Paying by Cheque")
End Sub
Private Sub DebitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DebitToolStripMenuItem.Click
ListBox1.Items.Add("Paying by Debit Card")
End Sub
Private Sub CreditCardToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CreditCardToolStripMenuItem.Click
ListBox1.Items.Add("Paying by Crebit Card")
End Sub
Private Sub VoucherToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VoucherToolStripMenuItem.Click
Dim Voucher As String
Voucher = "5"
CurrentBalance = Label4.Text
ListBox1.Items.Add("Voucher")
Label4.Text = CStr(Val(CurrentBalance) - Val(Voucher))
End Sub
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
End
End Sub
Private Sub MessageBox(ByVal p1 As String)
Throw New NotImplementedException
End Sub
非常感谢帮助。
答案 0 :(得分:5)
VB.NET的一些原理:
Dim s As String = "MyString"
Microsoft.VisualBasic
- 命名空间并开始使用新类(例如MessageBox
而不是MsgBox
)。* 到您的项目:
一种更好的重构方法可能就是将所有内容都填入Dictionary(Of String, Decimal)
,这可能会更好。
Private prices As New Dictionary(Of String, Decimal)
Private balance As Decimal = 0D
' Add this to the constructor '
prices.Add("Ham", 1D)
prices.Add("Banana", 1D)
prices.Add("Pineapple", 1D)
' etc. ... '
For Each item As KeyValuePair(Of String, Decimal) In prices
CheckedListBox1.Add(item.Key)
Next
' End of Constructor '
' Don't forget to give your Controls meaningful names!
Private Sub CheckedListBox1_ItemCheck(ByVal sender As Object, ByVal e As ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
If e.NewValue Then
balance += prices(CheckedListBox1.Item(e.Index))
Else
balance -= prices(CheckedListBox1.Item(e.Index))
End if
Label4.Text = balance.ToString("C2")
End Sub
*:正如Konrad Rudolph所指出的,如果您不想包含Forms
- 命名空间,这可能是Console-Projects的一个很好的解决方案。虽然,我对此持不同意见。
答案 1 :(得分:3)
您可以使用参数添加/删除项目:name,price,checkboxInstance
名称将包含商品名称(凭证,取件,......)
价格将用于更改Label4.Text中的值
checkboxInstance 可帮助您决定要执行的操作:
所以函数的主体看起来像这样:
If checkboxInstance.Checked Then
ListBox1.Items.Add(name)
Label4.Text = Val(CurrentBalance) - Val(price)
Else
Label4.Text = Val(CurrentBalance) + Val(price)
ListBox1.Items.Remove(name)
End If
我真的不记得VB.NET的语法。但是因为这是你的作业,我想最好提供一个提示而不是代码。
编辑:还有一件事如何从根本上简化代码。您可以使用复选框的“tag”属性来保存商品名称,而不是使用checkbox1,checkbox2,..您可以在Visual Studio的设计器中创建一个复选框数组,以便您可以为它们编制索引(myCheckboxes [0] ,myCheckboxes [1],...)。 http://www.thevbprogrammer.com/VBNET_09/09-05-ControlArrays.htm - 查看此网站。
答案 2 :(得分:0)
某些选项(大小和付款方式)似乎是互斥的,因此您最好将这些RadioButton放在两个单独的GroupBox中。我还建议不要在更新中执行任何计算,而是使用CalculateCost Sub,当成本需要重新计算时,所有事件都会延迟。你应该避免定义,例如ExtraLargePizza作为一个字符串 - 你可以把它变成一个整数(或十进制) - 然后在执行计算时你不需要将它包装在Val(...)中。
答案 3 :(得分:0)
您的代码文件似乎有很多重复。您可能需要考虑使用factory pattern来更轻松地创建您正在创建的不同种类的披萨。
祝你好运,并希望这有助于一些人。