我有以下代码:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ListBox1.Items.Add("Celsius to Farenheit")
ListBox1.Items.Add("Farenheit to Celsius")
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
' Get the currently selected item in the list box.
Dim currentItem As String = ListBox1.SelectedItem.ToString()
' Get the currently selected index of the item in the list box.
Dim currentIndex As Integer = ListBox1.FindString(currentItem)
End Sub
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 temperature to convert!")
ElseIf currentItem = "Celsius to Farenheit" Then
'do celsius to farenheit conversion
ElseIf currentItem = "Farenheit to Celsius" Then
'do farenheit to celsius conversion
Else
MsgBox("Please select a conversion first!")
End If
End Sub
End Class
我正在尝试检查是否在 ListBox1 中进行了特定选择,然后在单击 Button1 时执行该特定转换。但是上面的代码不能抛出错误“currentItem is not declared.It may be inaccessible to its protection level”。我怀疑这与
ListBox1_SelectedIndexChanged
作为私人潜艇,但将其更改为公共潜艇似乎没有任何影响。
有人可以指出我正确的方向吗?
谢谢。