2013-04-17 38 views
-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是 「其他生日」

我怎麼會去這樣做?

任何幫助將不勝感激!

+1

請澄清你的問題。 「我想讓這些數字說出生日的類型而不是數字」? –

回答

1

如果我正確理解你的問題,你可以聲明一個枚舉並將該枚舉的字典轉換爲字符串。

Enum負責處理代碼中的數字,而是使用人類可讀的構造。詞典將確保您的用戶也將看到人類可讀的結構。

請參見下面的代碼(需要一個全新的WinForms項目和主窗體上稱爲ListBox1列表框):

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 

免責聲明:這個代碼僅僅是一個什麼可以做演示,不意味着使用完整的解決方案。

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 

有意義嗎?我會進一步幫助,但我不太明白你想做什麼。