Use of Selected ListItems

I am not sure if this can be done, and at least done in a way where a noob can understand it.

In my current VB class I am working on a project where we are supposed to call on some text in a text file and then use it to produce some results based on user selections.

I am wondering if I can use a ListBox to show the choices, then by having the user click on "Submit" button call the right information.

As I am learning this for the first time, and have never programmed before, I hope that you all take it easy on me.

As a test to see if this works I tried the following:

Public Class Form1

Private Sub Button1_Click (ByVal Sender as System.Object, ByVal e System.Event Args) Handles Button1.Click

If ListBox1.Selected Items Then MsgBox (" Good Choice")

End If

End Sub

With Explicit and Strict On I get the following error :

Option Strict On disallows for implicit conversions from Object to Boolean.

I get what I am trying to do, but not doing it. I think that I need to convert the selection to a Boolean and when the user selects ListBox1, ListBox2, ListBox3, etc., X happens.

I am anywhere near what I think I need to have happen



Answer this question

Use of Selected ListItems

  • Suthy67

    you need to change it to this:

     

    if Me.ListBox1.SelectedItem.ToString().Equals(yourValue) then

       'do stuff

    end if

     

    or if you want to see if more than 0 items are selected:

     

    if Me.ListBox1.SelectedItems.Count > 0 then

       'do stuff

    end if

     

    does this help



  • alilai

    One Last question. If you wanted to do this for 100 items in a list would you have to do the above code 100 times or is there an easier way to do it


  • Jocker23

    sure does. The Equals is your call, I thought you were comparing values with the selected item with another item. I should also correct my code, to be SelectedItem not SelectedValue

    so say if your listbox has these values:

    A

    B

    C

    D

    user chooses B and hits a button. The button click event would do this:

     

    if Me.theListBox.SelectedItem.ToString().Equals("B") then

       MessageBox.Show("you chose B")

    end if

    of course be sure the user has selected something, by checking the SelectedIndex first, to see if it is greater than -1, since -1 indicates no selection.

    is this what you are after



  • Zadoras

    correct, but change the ".SelectedIndex <-1" to ".SelectedIndex > -1" :-)

    the first example you were saying about "self referencing", not quite - it would reference itself yes however it would be like asking if the current selection equals itself, in which case would be true all the time, and this is not what you want.

    the second example is more like what you need.

     

     



  • Ian_E

    Cool...thanks man....

    Damn math....and here I thought I would never need it again .. =)


  • Lolka

    Ya...I think this is what I am looking for. I suppose that asking is there is a way to have the listbox1.selectedItem equal the listbox1.selectedItem would work...

    if Me.ListBox1.SelectedItem.ToString().Equals(Listbox1.SelectedItem) then

    do stuf

    end If

    I am thinking not because it would cause an error like a selfreference or something right

    So in the see if user selected an item....it would look like this.

    If Me.ListBox1.SelectedIndex < -1 then

    If Me.ListedBox.SelectedItem.ToString().Equals("The State of Rhode Island") then

    do some stuff

    End If

    End If

    Right


  • b182f117

    THis is most definitely on the right track.

    However, I am not sure about the Equals(yourValue) My value is the Selection that the User made.

    For example,

    If the User choses B in a Listbox that contains A, B, C, D....then X happens.

    Does that make sense


  • Gordon Bell

    basically yes you would have to do it that many times. Really depends what the aim of this section is.

    you could have like an "approved" list of words or something in an array, go through each element of the array, if the item chosen in the listbox equals the current item in the array then cool, this would be easy but again, depends the purpose of the section....



  • Use of Selected ListItems