0

我有以下代码:

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ' calculate button
    If String.IsNullOrEmpty(TextBox1.Text) Then
        MsgBox("Please enter a value to convert!")
    End If

    If currentIndex = vbNull Then
        MsgBox("Please select a conversion!")
    End If

    Select Case currentIndex
        Case 1
            Label2.Text = TextBox1.Text & " Celsius = " & Math.Round(((TextBox1.Text * 1.8) + 32), 2) & " Fahrenheit"
        Case 2
            Label2.Text = TextBox1.Text & " Fahrenheit = " & Math.Round(((TextBox1.Text - 32) / 1.8), 2) & " Celsius"
        Case 3
            Label2.Text = TextBox1.Text & " Kelvin = " & Math.Round((TextBox1.Text - 273.15), 2) & " Celsius"
        Case 4
            Label2.Text = TextBox1.Text & " Celius = " & Math.Round((TextBox1.Text + 273.15), 2) & " Kelvin"
        Case 5
            Label2.Text = TextBox1.Text & " Kelvin = " & Math.Round((((TextBox1.Text - 273.15) * 1.8) + 32), 2) & " Fahrenheit"
        Case 6
            Label2.Text = TextBox1.Text & " Fahrenheit = " & Math.Round(((((TextBox1.Text - 32) * 5) / 9) + 273.15), 2) & " Kelvin"
        Case 8
            Label2.Text = TextBox1.Text & " Miles P/H = " & Math.Round((TextBox1.Text * 1.609344), 2) & " Kilometers P/H"
        Case 9
            Label2.Text = TextBox1.Text & " Kilometers P/H = " & Math.Round((TextBox1.Text / 1.609344), 2) & " Miles P/H"
        Case 11
            Label2.Text = TextBox1.Text & " Kilograms = " & Math.Round((TextBox1.Text * 2.20462), 2) & " Pounds"
        Case 12
            Label2.Text = TextBox1.Text & " Pounds = " & Math.Round((TextBox1.Text / 2.20462), 2) & " Kilograms"
        Case 14
            Label2.Text = TextBox1.Text & " Meters = " & Math.Round((TextBox1.Text * 3.2808399), 2) & " Feet"
        Case 15
            Label2.Text = TextBox1.Text & " Feet = " & Math.Round((TextBox1.Text / 3.2808399), 2) & " Meters"
    End Select
End Sub

如您所见,我有一个变量 (currentIndex) 并有一个 select case 语句检查它们是否针对各种转换,但是我的问题在于上面的代码段。

If currentIndex = vbNull Then
    MsgBox("Please select a conversion!")
End If

如果索引为空,我需要它吐出一条错误消息,但是我无法找到一种方法来做到这一点。0 不能使用,因为这是索引中的第一个条目,并且 vbNull 等似乎不起作用。谁能指出我正确的方向?谢谢。

编辑:这是当前索引的创建方式:

Dim currentIndex As Integer

这就是它的分配方式:

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

    ' Get the currently selected index of the item in the list box.
    currentIndex = ListBox1.FindString(currentItem)
End Sub
4

2 回答 2

0

如果 currentIndex 是下拉框中的索引,则值为 -1 表示未选择任何内容。

于 2013-08-29T00:14:29.743 回答
0

我建议改用 SelectedIndex。

在您的代码中:

    If ListBox1.SelectedIndex = -1 Then
        MsgBox("Please select a conversion!")
    End If

这是该属性的参考:http: //msdn.microsoft.com/en-us/library/system.windows.forms.listbox.selectedindex.aspx

于 2013-08-29T00:20:36.197 回答