2

我有以下代码:

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

作为私人潜艇,但将其更改为公共潜艇似乎没有任何影响。

有人可以指出我正确的方向吗?

谢谢。

4

2 回答 2

1

SelectedIndexChanged以下是当用户更改选定列表框项时如何使用该事件来捕获其索引的方法:

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 ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged存根方法。

更新:

要使用SelectedIndexChanged事件中收集的值,您必须在类级别 ( Form1) 声明这些变量,下面是代码:

Public Class Form1
    Dim currentItem As String
    Dim currentIndex As Integer

    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. 
        currentItem = ListBox1.SelectedItem.ToString()

        ' Get the currently selected index of the item in the list box.
        currentIndex = 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
于 2013-08-26T23:10:26.117 回答
1

感谢 Karl Anderson,我使用以下代码解决了我的问题。

Public Class Form1
Dim currentItem As String
Dim currentIndex As Integer

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. 
    currentItem = ListBox1.SelectedItem.ToString()

    ' Get the currently selected index of the item in the list box.
   currentIndex = 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 currentIndex = 0 Then
        Label2.Text = TextBox1.Text & " Celsius = " & (TextBox1.Text * 1.8) + 32 & " Farenheit"
    ElseIf currentIndex = 1 Then
        Label2.Text = TextBox1.Text & " Farenheit = " & (TextBox1.Text - 32) / 1.8 & " Celsius"
    Else
        MsgBox("Please select a conversion first!")
    End If
End Sub
End Class
于 2013-08-26T23:39:43.800 回答