繁体   English   中英

如何将 options.txt 文件中的选项和价格列表读入 vb.net 中的相应 arrays

[英]how to Read in the list of options and prices from the options.txt file into the appropriate arrays in vb.net

我已经创建了一个预订和支付旅游费用的应用程序;

客户可以拨打 select 为他们的旅行选择以下每个选项中的一个且仅一个: • 大巴类型(3 个选项:12 座、21 座、55 座) • 旅行长度(3 个选项:3 天、7 天、10 天)

客户可以选择 select 以下一个或多个可选的旅游观光选项: • 布兰城堡 • LiBEARty 保护区 • 派勒斯城堡 • 图尔达盐矿

该应用程序使用一个数组来存储三个教练类型选项。 Arrays 还存储每种客车类型的每日价格。

  • 我想使用一个数组来存储可选的观光旅游和一个单独的数组来存储可选的观光旅游的价格。

可选的观光旅游取决于一年中旅行的时间,价格可能会发生变化。 这些名称和价格应存储在文本文件中,以便于更新和将数据读入程序。

文本文件应命名为 options.txt,并应在下一行列出观光选项 1 的名称和该选项的价格,以此类推四个选项。 例如,文本文件将这样设置: enter image description here

这使得 arrays 可以从文本文件中读取每个观光旅游的名称并将其存储在观光旅游数组中,然后从文本文件的下一行读取其价格并将价格存储在观光旅游中价格数组

请协助;

  1. 将 options.txt 文件中的可选观光旅游和价格列表读入相应的 arrays

  2. 填充组合框或其他从 arrays 中选择的控件。您应该使用 for 循环遍历 arrays 并将项目添加到组合框。

  3. 编写事件代码以将观光旅游从最低到最贵排序。 在将游览选项添加到复选框之前执行此操作

  4. 在复选框中填写观光旅游的详细信息

见下图界面; 在此处输入图像描述

请参阅下面的代码;

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/天

GroupBox1 (Coach Type)中单选按钮的标签属性分别设置为 1800、2100 和 3850。

GroupBox2 (游览长度)中单选按钮的标签属性设置为 3、7、10。

将 Label 2 的MaximumSize Width属性设置为适合您的表单的值。 每个选项的标题和描述应该能够适合表格。

删除Label1Label2上的文本值。

Class:

而不是并行 arrays 使用 class 作为选项。 这将更容易维护。

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

由于大量的注释,实际上代码并不像看起来那么多。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM