none
ListBox to integer RRS feed

  • Question

  • So i need to take the value of a number in a ListBox (from 1000 to 1025) and then set each number to integer, and im trying to do this in a for loop. Is there any way i could use the index or something, then just go Index + 1 to get the next number?
    Friday, May 25, 2018 3:01 PM

All replies

  • Why not simply populate the ListBox with integers

    ListBox1.DataSource = Enumerable.Range(1000, 26).ToList

    Then to get say the current item

    MessageBox.Show(CInt(ListBox1.SelectedItem).ToString)


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, May 25, 2018 3:08 PM
    Moderator
  • Told by your teacher or as it is done when you are ready with studying?

    Success
    Cor

    Friday, May 25, 2018 4:58 PM
  • That doesnt work with my code. i need to pull out the numbers so i can decompose them. I'm trying to do a loop to go through and seperate all 26 numbers, aka all 26 indexes
    Friday, May 25, 2018 6:28 PM
  • The ListBox.Items property is a collection.  All collections generally have an Item(index) property.  So in almost every case, you can say:

    For idx = 0 to {Collection}.Count -1
       Dim item = {Collection}.Item(idx)
       'do something with item
    Loop

    That's just pseudo code, but you should be able to apply it to any collection you need to loop over.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Friday, May 25, 2018 7:19 PM
    Moderator
  • Why Index + 1? Why not just use the Index to get the next number?

    Why don't you display your code that sets the Listbox items? And then the code you are trying to use to get the ListBox Items?

    The ListBox has an Items property. ListBox Items are type object. So you need to loop through the Items property converting the items at each index to integer. Then do something with the item and move on.


    La vida loca

    Friday, May 25, 2018 8:23 PM
  • That doesnt work with my code. i need to pull out the numbers so i can decompose them. I'm trying to do a loop to go through and seperate all 26 numbers, aka all 26 indexes

    Hi Frosty32631,

    If you have added some items in ListBox, then you want to pull out these item using loop and index, you can do this, but I am not quite sure what you want to do, and why you want to do.

        Dim count As Integer = ListBox1.Items.Count
            For i As Integer = 0 To count - 1
                Console.WriteLine(ListBox1.Items(i).ToString())
    
            Next

    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.

    Tuesday, May 29, 2018 2:50 AM
    Moderator
  • I'm for 80% sure it is homework because this is 100% theoretical. 

    However, because of the so many code which is given. The assignment can be solved by looping from 1000 to 1025 and then use the findstring or findstringexact and not by the index from 0 to end index.

    It teaches very well that looping through the listbox using the index in this case cost more code and time than needed. 


    Success
    Cor


    Tuesday, May 29, 2018 10:19 AM
  • Here's my entire code so far

    Basically, i need to pull out each number one at a time from my initial listbox, seperate it into thousands, hundreds, tens, and ones and then go on to the next number. What I'm stuck on is looping through each number.

    Public Class Form1
        Private Sub btnSepRé_Click(sender As Object, e As EventArgs) Handles btnSepRé.Click
            Dim nombre As Integer
            Dim Unités As Integer
            Dim Dizaines As Integer
            Dim Centaines As Integer
            Dim Milliers As Integer
            Dim intA As Integer
            Dim intIndex As Integer = 0
            Dim intNuméro As Integer = 1000
            Dim intCompte As Integer = lstEntrer.Items.Count
    
            Me.lstEntrer.Items.Clear() 'Prévient de doubler la liste de numéros
            'Insère les numéros de 1000 à 1025 dans le colonne de numéros
            For intA = 1 To 26
                lstEntrer.Items.Add(intNuméro)
                intNuméro = intNuméro + 1
            Next
    
            For i As Integer = 0 To intCompte - 1
                nombre = lstEntrer.FindString(intIndex)
                intIndex = intIndex + 1
            Next
        End Sub
        Sub SépareQuatreChiffres(ByVal nombre As Integer, ByRef Milliers As Integer, ByRef Centaines As Integer, ByRef Dizaines As Integer, ByRef Unités As Integer)
            Milliers = nombre \ 1000
            nombre = nombre Mod 1000
            Call SépareTroisChiffres(nombre, Centaines, Dizaines, Unités)
    
            Me.lstSep1.Items.Add(nombre)
        End Sub
    
        Sub SépareTroisChiffres(ByVal nombre As Integer, ByRef Centaines As Integer, ByRef Dizaines As Integer, ByRef Unités As Integer)
            Centaines = nombre \ 100
            nombre = nombre Mod 100
            Call SépareDeuxChiffres(nombre, Dizaines, Unités)
    
            Me.lstSep2.Items.Add(nombre)
        End Sub
    
        Sub SépareDeuxChiffres(ByVal nombre As Integer, ByRef Dizaines As Integer, ByRef Unités As Integer)
            Dizaines = nombre \ 10
            nombre = nombre Mod 10
            Call DémontreUnNombre(nombre, Unités)
    
            Me.lstSep3.Items.Add(nombre)
        End Sub
    
        Sub DémontreUnNombre(ByVal nombre As Integer, ByRef Unités As Integer)
            Me.lstSep4.Items.Add(nombre)
        End Sub
    End Class
    

    Tuesday, May 29, 2018 5:44 PM
  • Hi

    Don't know if this will be helpful or not:

    Form1 with ListBox1 holding list of numbers and

    ListBox2 for resultant split into Units/10's/100's/1000's etc

    ' Form1 with:
    ' ListBox1 - list of numbers
    ' ListBox2 - list of current values
    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
        ListBox2.DataSource = ListBox1.SelectedItem.ToString.Reverse.ToList
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    Tuesday, May 29, 2018 6:36 PM
  • What I'm stuck on is looping through each number.

    To do the loop you would iterate over the Items collection of the listbox.

    For Each I As Integer In lstEntrer.Items
        SépareQuatreChiffres(I)
    Next
    

    Each sub requires only one value to be passed.  It then calls the next sub using only the remainder that needs to be decomposed.

        Sub SépareQuatreChiffres(ByVal nombre As Integer)
            Milliers = nombre \ 1000
            nombre = nombre Mod 1000
            Call SépareTroisChiffres(nombre)
            Me.lstSep1.Items.Add(Milliers)
        End Sub
    You have declared a lot of variables as local to  btnSepRé_Click. Some aren't used, and some need to be either global or local to where they are used.
    Tuesday, May 29, 2018 10:20 PM