我需要帮助从ListBox(标记为lstAge(获取输入。我需要知道,这样我就可以将其转换为双精度,因为我想为正在使用的值生成一个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