-2
Public Class MainForm

Private Sub exitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles exitButton.Click
    Me.Close()
End Sub

Private Sub guestsTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles guestsTextBox.KeyPress
    ' allows only numbers and the Backspace key

    If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso
        e.KeyChar <> ControlChars.Back Then
        e.Handled = True
    End If
End Sub

Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'fills the list box and selects the first item

    typeListBox.Items.Add("Kid's Birthday")
    typeListBox.Items.Add("21st Birthday")
    typeListBox.Items.Add("40th Birthday")
    typeListBox.Items.Add("Other Birthday")
    typeListBox.SelectedIndex = 0
End Sub

Private Sub calcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles calcButton.Click
    'displays the total charge

    Dim guests As Integer
    Dim typeIndex As Integer
    Dim guestPrice As Integer
    Dim totalCharge As Integer

    Integer.TryParse(guestsTextBox.Text, guests)
    typeIndex = typeListBox.SelectedIndex

    'determine the price per guest
    Select Case typeIndex
        Case 0 'Kid's Birthday
            guestPrice = 11
        Case 1 '21st Birthday
            guestPrice = 20
        Case 2 '40th Birthday
            guestPrice = 25
        Case Else 'other birthdays
            guestPrice = 15
    End Select

    'calculate and display the total charge
    totalCharge = guests * guestPrice
    totalLabel.Text = totalCharge.ToString("C0")
End Sub

Private Sub testDataButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles testDataButton.Click
    Dim guests As Integer
    Dim typeIndex As Integer
    Dim guestPrice As Integer
    Dim totalCharge As Integer
    Dim randomGen As New Random
    Dim setCounter As Integer = 1

    testDataLabel.Text = String.Empty

    Do
        guests = randomGen.Next(1, 51)
        typeIndex = randomGen.Next(0, 4)

        For Each I As Object In typeListBox.SelectedItems
            testDataLabel.Text += I.ToString() & ControlChars.NewLine
        Next

        'determine the price per guest
        Select Case typeListBox.SelectedIndex
            Case 0
                guestPrice = 11
            Case 1
                guestPrice = 20
            Case 2
                guestPrice = 25
            Case Else
                guestPrice = 15
        End Select

        'calculate and display the total charge
        totalCharge = guests * guestPrice
        testDataLabel.Text = testDataLabel.Text &
            typeIndex.ToString & "    " &
            guests.ToString & "    " &
            totalCharge.ToString("C0") &
            ControlChars.NewLine
        setCounter += 1


    Loop Until setCounter > 10

End Sub

Private Sub typeListBox_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles typeListBox.SelectedIndexChanged

End Sub
End Class

当我单击一个名为“生成测试数据”的按钮时,它会在标签中生成一个随机数列表。我希望这些数字说明生日的类型而不是数字。

0 是“孩子的生日”

1 是“21 岁生日”

2是“40岁生日”

和 3 是“其他生日”

我该怎么做呢?

任何帮助将非常感激!

4

2 回答 2

1

如果我正确理解了您的问题,您可以声明一个枚举并将该枚举的字典转换为 String。

Enum 负责处理代码中的数字,而是使用人类可读的结构。字典将确保您的用户也将看到人类可读的结构。

请看下面的代码(需要一个全新的 WinForms 项目和一个ListBox1在主窗体上调用的 ListBox):

Option Strict On

Public Class Form1
  'Declare this enum to avoid dealing with naked numbers in code
  Enum BirthdayTypes
    btKids = 0
    bt21st = 1
    bt40th = 2
    btOther = 3
  End Enum

  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) _
                                                     Handles MyBase.Load
    'Suppose your input value is a number,
    'but you don't want to deal with numbers
    Dim typeIndex As Integer = 2

    'You can convert your number to BirthdayTypes,
    'making your input "correct"
    Dim typeIndexBt As BirthdayTypes = ConvertBirthdayIndexToType(typeIndex)

    'Calculation of guest price is now "human readable"
    Dim guestPrice As Integer = CalculateGuestPrice(typeIndexBt)

    'You can create a dictionary for diplaying the values
    Dim displayDictionary As New Dictionary(Of BirthdayTypes, String)
    With displayDictionary
      .Add(BirthdayTypes.btKids, "Kid's Birthday")
      .Add(BirthdayTypes.bt21st, "21st Birthday")
      .Add(BirthdayTypes.bt40th, "40th Birthday")
      .Add(BirthdayTypes.btOther, "Other Birthday")
    End With

    'Here is how you would those values into a ListBox
    With ListBox1
      .DataSource = displayDictionary.ToList
      .ValueMember = "Key"
      .DisplayMember = "Value"
    End With

    'Now your ListBox displays strings, 
    'but SelectedValue would return an object of type BirthdayTypes

    'You can extract random values from the above dictionary by index,
    'and create a new list from it

    Debug.WriteLine(ListBox1.SelectedValue) 'outputs btKids
  End Sub

  Private Function CalculateGuestPrice(bt As BirthdayTypes) As Integer
    Select Case bt
      Case BirthdayTypes.btKids : Return 11
      Case BirthdayTypes.bt21st : Return 20
      Case BirthdayTypes.bt40th : Return 25
      Case BirthdayTypes.btOther : Return 15
    End Select
    'should never here
    Throw New Exception("Unknown birthday type")
  End Function

  Private Function ConvertBirthdayIndexToType(index As Integer) As BirthdayTypes
    If index < 3 Then Return CType(index, BirthdayTypes)
    Return BirthdayTypes.btOther
  End Function
End Class

免责声明:此代码只是可以做的演示,并不意味着用作完整的解决方案。

于 2013-04-18T14:53:10.207 回答
0

我会使用一个字符串。

Dim thestring() As String = {"Kid's Birthday", _ 
                             "21st Birthday",  _
                             "40th Birthday",  _
                             "Other Birthday"}

现在这些数字中的每一个都将代表字符串中的文本。

thestring(0) = kids birthday
thestring(1) = 21 birthday
thestring(2) = 40th birthday
thestring(3) = other birthday

说得通?我会进一步提供帮助,但我不太明白你想要做什么。

于 2013-04-19T00:54:24.693 回答