none
List Boxes in If/Else Statements RRS feed

  • Question

  • We're learning about list boxes in my coding class, and we are to make a project that follows this, in short: "If this item from a list box is selected, and a number from a combo box is selected, then multiply these integers together (the numbers vary depending on which items and numbers were selected). Then when I press a button, take this, and display it in a label."

    my question, is how to code a phrase that assigns an integer to a selected item. For example, if one was selected, it would have a value of 4, another may be 7, and so on.

    Sunday, November 19, 2017 10:21 PM

All replies

  • We're learning about list boxes in my coding class, and we are to make a project that follows this, in short: "If this item from a list box is selected, and a number from a combo box is selected, then multiply these integers together (the numbers vary depending on which items and numbers were selected). Then when I press a button, take this, and display it in a label."

    my question, is how to code a phrase that assigns an integer to a selected item. For example, if one was selected, it would have a value of 4, another may be 7, and so on.

    I agree that's ambiguous.

    Do you suppose they mean the .SelectedIndex? Do keep in mind that's zero-based.


    "A problem well stated is a problem half solved.” - Charles F. Kettering


    • Edited by Frank L. Smith Sunday, November 19, 2017 10:29 PM ... added link to MSDN documentation
    Sunday, November 19, 2017 10:27 PM
  • Hi

    I am guessing here. You question is unclear.

    If you want to assign an integer to an 'item', and if that item is a string, then perhaps use a Dictionary(Of String, Integer). [NOTE: the Strings need to be unique]

    The Dictionary Key would be the DataSource for the ListBox and the Dictionary Value will be the ListBox ValueMember for use in calculations

    NOTE: NO error checking done!

    Something like this:

    ' Form1 with empty ListBox1, empty
    ' ComboBox1, empty Label1 and
    ' Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
        Dim Dict As New Dictionary(Of String, Integer)
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            With Dict
                .Add("One", 1)
                .Add("Two", 3)
                .Add("Three", 9)
                .Add("Four", 17)
            End With
            With ListBox1
                .DataSource = New BindingSource(Dict, Nothing)
                .DisplayMember = "Key"
                .ValueMember = "Value"
            End With
            With ComboBox1
                .Items.AddRange({12, 11, 9, 7, 5, 3})
            End With
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim a As Integer = CInt(ListBox1.SelectedValue)
    
            Dim b As Integer = CInt(ComboBox1.SelectedItem)
    
            Label1.Text = (a * b).ToString
        End Sub
    End Class


    Regards Les, Livingston, Scotland

    Sunday, November 19, 2017 10:51 PM
  • my question, is how to code a phrase that assigns an integer to a selected item. For example, if one was selected, it would have a value of 4, another may be 7, and so on.

    An item in a list box might be an Integer - it depends on what got added when the list box items collection was created.    So if you used some statements like

            ListBox1.Items.Add(1)
            ListBox1.Items.Add(3)
            ListBox1.Items.Add(5)

    then the listbox items are integers.  So the expression ListBox1.SelectedItem will return an integer with the value of the item that is currently selected, and there is no need to assign an Integer to it - it already is an Integer.

    If it's something other than Integers being added to the list box, such as Strings, then you need to convert the selected item to an Integer.
    https://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.selecteditem(v=vs.110).aspx

    Sunday, November 19, 2017 11:13 PM
  • Hi,

    As others mentioned, I'm afraid it's ambiguous. Which do you mean by "selected item" and "value", SelectedIndex or Text in ListBox and Combobox?

    I made a sample to make it clear.
        

    Blue color shows selected Text in ListBox and ComboBox,
    Red color shows SelectedIndex.
      [note] Index begins from zero, instead of 1.

    Which do you want?

    Regards,

    Ashidacchi


    • Edited by Ashidacchi Monday, November 20, 2017 12:35 AM
    Monday, November 20, 2017 12:30 AM
  •  in short: "If this item from a list box is selected, and a number from a combo box is selected, then multiply these integers together (the numbers vary depending on which items and numbers were selected). Then when I press a button, take this, and display it in a label."

    my question, is how to code a phrase that assigns an integer to a selected item. For example, if one was selected, it would have a value of 4, another may be 7, and so on.

    Hi awkawrdartist,

    Can you share what data the listbox and combobox were bound to, and then what do you want to achieve. Can you give us one example here?

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 22, 2017 3:00 AM
    Moderator