簡體   English   中英

如何從visual basic中的列表框中獲取用戶輸入?

[英]How do I get the users input from a list box in visual basic?

我需要幫助從 ListBox(標記為 lstAge)獲取輸入。 我需要知道所以我可以將它轉換為 double 因為我想為正在使用的值做一個 If 語句。 我會附上我所擁有的以防萬一。

'''
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
     For count As Double = 16 To 100
     count = lstAge.Items.Add(count.ToString())
Next
    lstAge.SelectedIndex = 0
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
        Dim age As Double

    If txtTicket.Text = "N" Or txtTicket.Text = "n" Then
        If age < 30 Then
            lblResult.Text = txtName.Text & ", your insurance cost will be $45. Keep Up the good driving!"
        Else
            lblResult.Text = txtName.Text & ", your insurance cost will be $30."
        End If
    Else
        lblResult.Text = txtName.Text & ", your insurance cost will be $85."
    End If
End Sub

'''

像這樣嘗試:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
     For count As Double = 16 To 100
     count = lstAge.Items.Add(count.ToString())
Next
    lstAge.SelectedIndex = 0
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
    Dim age As Double

    age = CDbl(lstAge.SelectedItem.ToString())

    If txtTicket.Text = "N" Or txtTicket.Text = "n" Then
        If age < 30 Then
            lblResult.Text = txtName.Text & ", your insurance cost will be $45. Keep Up the good driving!"
        Else
            lblResult.Text = txtName.Text & ", your insurance cost will be $30."
        End If
    Else
        lblResult.Text = txtName.Text & ", your insurance cost will be $85."
    End If
End Sub

Button1_Click函數中,通過使用ListBox.SelectedItem獲取列表框中所選項目的值,將其轉換為字符串,然后使用CDbl將其轉換為雙精度值來設置age

這假設您的列表框沒有多列並且您一次選擇一項。

如果我理解你,你不知道如何從 ListBox 所選項目中獲取價值...

要從列表框中獲取值,您可以使用ListBox.SelectedItem 屬性

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
    Dim currentage As Integer = DirectCast(lst.SelectedItem, Integer)
    Dim message As String = txtName.Text

    If txtTicket.Text.ToLower() = "n" Then
        Select Case currentage
            Case  16 to 30
                message &= ", your insurance cost will be $45. Keep Up the good driving!"
            Case else
                message &= ", your insurance cost will be $30."
        End Select
    Else
        message = &= ", your insurance cost will be $85."
    End If

    lblResult.Text =  message
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM