我有以下代码:
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
如果索引为null,我要求它发出错误消息,但是我无法解决这个问题。 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
答案 0 :(得分:0)
如果currentIndex是下拉框中的索引,则值为-1表示未选择任何内容。
答案 1 :(得分: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