Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I check when a listitem is selected and then do something if it is?
    primarykey
    data
    text
    <p>I have the following code:</p> <pre><code>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 </code></pre> <p>I am trying to check so that if a specific selection is made in ListBox1, then it performs that specific conversion when Button1 is clicked. However the above code cannot throws the error "currentItem is not declared.It may be inaccessible to its protection level". I suspect this has something to do with the</p> <pre><code>ListBox1_SelectedIndexChanged </code></pre> <p>being a private sub, however changing it to public does not seem to have any affect.</p> <p>Can somebody point me in the right direction?</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload