vb.net - 如何将 options.txt 文件中的选项和价格列表读入 vb.net 中的相应数组
问题描述
我创建了一个应用程序来预订和支付旅游费用;
客户可以为他们的旅行选择以下选项中的一个且仅一个: • 教练类型(3 个选项:12 座、21 座、55 座) • 游览长度(3 个选项:3 天、7 天、10 天)
客户可以选择以下一种或多种可选观光选项进行游览: • 布兰城堡 • LiBEARty Sanctuary • Peles Castle • Turda Salt Mine
该应用程序使用一个数组来存储三个教练类型选项。数组还存储每种教练类型每天的价格。
- 我想使用一个数组来存储可选的观光旅游和一个单独的数组来存储可选的观光旅游的价格。
可选的观光旅游取决于一年中的旅行时间,价格可能会发生变化。这些名称和价格应存储在文本文件中,以便于更新和将数据读入程序。
文本文件应名为 options.txt,并应以 Sightseeing Option 1 的名称进行布局,下一行是该选项的价格,四个选项依此类推。例如,文本文件会这样设置: 在此处输入图像描述
这可以通过读取每个观光旅游的名称并将其存储在观光旅游数组中,然后从文本文件的下一行读取其价格并将价格存储在观光旅游中,从而可以从文本文件中填充数组价格数组
请协助;
将 options.txt 文件中的可选观光旅游和价格列表读入适当的数组
从数组中填充组合框或其他选定的控件。您应该使用 for 循环遍历数组并将项目添加到组合框中。
编写事件代码以将观光旅游从最低到最高进行排序。在将游览选项添加到复选框之前执行此操作
在复选框中填写观光旅游的详细信息
见下面的界面; 在此处输入图像描述
请参阅下面的代码;
Public Class frmOptions
Dim CoachType() As String = {"12 Seater", "21 Seater", "55 Seater"} ' an array of 3 strings
Dim CoachCost() As Integer = {150, 100, 70} ' an array of 3 integers
Dim TourLength() As String = {"3 days", "7 days", "10 days"} ' an array of 3 strings
Dim strCoachType As String
Dim intCoachCost As Integer
Dim intTourLength As Integer
Dim strSightseeingOptions() As String 'Array to store selected sight seeing options
Dim intSightseeingPrices As Integer
Dim intTotal As Integer
Private Function getSelectedItems()
'Get selected Coach Type
If rb12Seater.Checked Then
strCoachType = "12 Seater"
'multiply cost per day by number of coach seats
intCoachCost = 150 * 12
ElseIf rb21Seater.Checked Then
strCoachType = "21 Seater"
'multiply cost per day by number of coach seats
intCoachCost = 100 * 21
ElseIf rb55Seater.Checked Then
strCoachType = "55 Seater"
'multiply cost per day by number of coach seats
intCoachCost = 70 * 55
End If
'Get selected Tour Length
If rb3Days.Checked Then
intTourLength = 3
ElseIf rb7Days.Checked Then
intTourLength = 10
ElseIf rb10Days.Checked Then
intTourLength = 11
End If
'Calculate total amount
intTotal = intCoachCost * intTourLength
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
btnPurchase.Enabled = False
rb12Seater.Text = CoachType(0)
rb21Seater.Text = CoachType(1)
rb55Seater.Text = CoachType(2)
rb3Days.Text = (TourLength(0))
rb7Days.Text = TourLength(1)
rb10Days.Text = TourLength(2)
End Sub
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
btnPurchase.Enabled = True
getSelectedItems()
lblTotalCost.Text = "€" + intTotal.ToString
End Sub
Private Sub gbCoachType_Enter(sender As Object, e As EventArgs) Handles gbCoachType.Enter
End Sub
Private Sub btnPurchase_Click(sender As Object, e As EventArgs) Handles btnPurchase.Click
btnPurchase.Enabled = False
frmDetails.ShowDialog()
End Sub
Private Sub CheckedListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CheckedListBox1.SelectedIndexChanged
End Sub
End Class
解决方案
表格设计:
我没有费心给控件起有意义的名字,但你应该这样做。我从您的代码中得出以下内容。为了简化代码,我计算了每日费率。
- 12 座 x 150/座/天 = 1800/天
- 21 座 x 100/座/天 = 2100/天
- 55 座 x 70/座/天 = 3850/天
将(Coach Type)中单选按钮的tag属性分别设置为GroupBox1
1800、2100、3850。
GroupBox2
将(Length of Tour) 中单选按钮的 tag 属性设置为 3、7、10 。
将MaximumSize Width
标签 2 的属性设置为适合您的表单的值。每个选项的标题和描述应该能够适合表格。
删除 和 上的文本Label1
值Label2
。
班上:
而不是并行数组,而是使用一个类作为选项。这将更容易维护。
Public Class Options
Public Property Title As String
Public Property Cost As Decimal 'Use a Decimal type for money
Public Property Description As String
'The parameterized constructor makes it easy to create an instance
Public Sub New(t As String, c As Decimal, d As String)
Title = t
Cost = c
Description = d
End Sub
'The ListBox will call .ToString to display your Options
Public Overrides Function ToString() As String
Return $"{Title} - {Cost}"
End Function
End Class
在表格中:
请注意,没有任何Form
级别变量。
Private Sub Tours_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Read the text file stored in bin\Debug
'.ReadAllLine returns an array of the lines in the text file
Dim lines = File.ReadAllLines("Options.txt")
'Declare a list to hold the contents of the file
Dim olst As New List(Of Options)
'Loop through each line from the text file
For Each line In lines
'split each line into an array by the delimiter comma
'the .Split method expects a Char, the small c following the string tells the compiler it is a Char
Dim splits = line.Split(","c)
'Create an instance of the Options class passing the values of the properties
Dim o As New Options(splits(0), CDec(splits(1)), splits(2))
'Add the Options instance to the List(Of Options)
olst.Add(o)
Next
'Bind the ListBox to the list of Options
'The ListBox calls .ToString() on each Option in the list (the class overrides .ToString)
ListBox1.DataSource = olst
ListBox1.SelectedIndex = -1
End Sub
'The Calculate button
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Find the checked RadioButton in GroupBox1
Dim rButton1 As RadioButton = GetCheckedRadioButton(GroupBox1)
'The Tag property was set to Seat rate X Number of seats at design time
Dim Total As Decimal = CDec(rButton1.Tag)
'The Tag property was set to the number of days at design time
Dim rButton2 As RadioButton = GetCheckedRadioButton(GroupBox2)
'Multiply Total by number of days
Total *= CDec(rButton2.Tag)
'Add costs of optional tours
For Each item As Options In ListBox1.SelectedItems
Total += item.Cost
Next
Label1.Text = "$" & Total.ToString
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
Label2.Text = ""
For Each item As Options In ListBox1.SelectedItems
Label2.Text &= $"{item.Title}{Environment.NewLine}{item.Description}{Environment.NewLine}{Environment.NewLine}"
Next
End Sub
Private Function GetCheckedRadioButton(Container As Control) As RadioButton
'This is a bit of LINQ magic. It can be handy in many situations. Sometimes the Container is a Form which is, indeed, a Control!
Dim rButton As RadioButton = Container.Controls.OfType(Of RadioButton).FirstOrDefault(Function(r) r.Checked = True)
Return rButton
End Function
由于大量的注释,这实际上并没有看起来那么多的代码。
推荐阅读
- python-3.x - 使用 AWS Step Functions 将输入数据存储在容器的 /opt/ml/ 上
- sql - SQL Server 2019 使用 LIMIT、OFFSET、FETCH 时出错
- angular - 检测到循环依赖来自 Angular 本地项目的警告,但在 Stackblitz 上没有
- python - 如何使用 python 将混淆矩阵记录到 azureml 平台
- mongodb - 我该如何解决这个与 Mongoose 相关的问题?
- macos - 如何每晚安排mac codesign(cron/launchd)?
- laravel - Memcache 不能与 Laravel 一起使用,但在直接使用时可以正常工作
- python - Python Create new DF For student grade from 2 DF's ,一个包含 StudentID 和 Marks 其他包含 Marks Series 和 Grade
- android - Chrome自定义标签android中的地理位置
- python-3.x - 我不能在 UPDATE SQL 中使用列表