-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

「Generate Test Data」という名前のボタンをクリックすると、ラベルに乱数のリストが生成されます。これらの数字に、数字ではなく誕生日の種類を言ってもらいたいです。

0 は「子供の誕生日」

1は「21歳の誕生日」

2位は「40歳の誕生日」

3つは「その他の誕生日」

どうすればこれを行うことができますか?

どんな助けでも大歓迎です!

4

2 に答える 2

1

私があなたの質問を正しく理解していれば、列挙型を宣言し、その列挙型の辞書を文字列にすることができます。

列挙型は、コード内の数値の処理を処理し、むしろ人間が読める構造を使用します。辞書は、ユーザーが人間が読める構造も確実に見るようにします。

以下のコードを参照してください (新しい 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 に答える