标签中文本的随机数 [英] Random Numbers to Text in Label

查看:142
本文介绍了标签中文本的随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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





http://i49.tinypic.com/293c405.png



当我点击名为生成测试数据的按钮时它会在标签中生成一个随机数列表。我想要这些数字来表示生日的类型而不是数字。



0是孩子的生日



1为21岁生日



2为40岁生日



和3是其他生日



我该怎么做呢?



任何帮助都会非常感谢!



http://i49.tinypic.com/293c405.png

When I click on a button named "Generate Test Data" It generates a list of random numbers in a label. I want these numbers to say the type of birthday instead of the number.

0 being "Kid''s Birthday"

1 being "21st Birthday"

2 being "40th Birthday"

and 3 being "Other Birthday"

How would I go about doing this?

Any help would be much appreciated!

推荐答案

testDataLabel.Text = testDataLabel.Text &
    typeListBox.Items(typeIndex).ToString & "    " &
    guests.ToString & "    " &
    totalCharge.ToString("C0") & vbNewLine 


这篇关于标签中文本的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆