我有以下代码:
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未声明。它的保护级别可能无法访问”。我怀疑这与
有关ListBox1_SelectedIndexChanged
成为私人子公司,但将其改为公开似乎没有任何影响。
有人能指出我正确的方向吗?
感谢。
答案 0 :(得分: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
答案 1 :(得分: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