Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4171102
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T00:24:33+00:00 2026-05-21T00:24:33+00:00

I have a list box on a form and it works fine for what

  • 0

I have a list box on a form and it works fine for what I want to do.

I am wanting to edit items on the form, this means populating the listbox and then selecting the relevant items.

My listbox contains a list of item sizes, i want to select the sizes which belong to the item being edited.

PLease can someone give me some pointers.

I tried me.lstItemSizes.SetSelected(i,true) but this only works for a single item.

Any help wil be much appreciated.

My Code:

    Private Sub SelectItemSizes(ByVal itemID As Integer)

    Dim itemSizes As IList(Of ItemSize) = _sizeLogic.GetItemSizes(itemID)

    Me.lstItemSizes.SelectionMode = SelectionMode.MultiExtended

    If (itemSizes.Count > 0) Then

        For i As Integer = 0 To Me.lstItemSizes.Items.Count - 1

            For x As Integer = 0 To itemSizes.Count - 1

                If (CType(Me.lstItemSizes.Items(i), PosSize).SizeID = itemSizes(x).SizeID) Then
                    Me.lstItemSizes.SetSelected(i, True)
                Else
                    Me.lstItemSizes.SetSelected(i, False)
                End If

            Next

        Next

    End If

End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T00:24:33+00:00Added an answer on May 21, 2026 at 12:24 am

    Did you set the selectionmode to multi?

    You need to specify that in order to allow multiple selections.

    Then you can do:

    Dim i as Integer=0
    
    For i=0 To Me.listBox.SelectedItems.Count -1
      'display the listbox value
    next i
    

    Here is a screen shot:

    enter image description here

    After you set the property on the listbox then call setselected based on the values you want selected.

    me.lstItemSizes.SetSelected(3,true)
    me.lstItemSizes.SetSelected(4,true)
    me.lstItemSizes.SetSelected(9,true)
    

    Here you can add 20 numbers and only select the even.

        Dim i As Integer
    
                'load the list with 20 numbers
                For i = 0 To 20
                    Me.ListBox1.Items.Add(i)
                Next
    
                'now use setselected
                'assume only even are selected
                For i = 0 To 20
                    If i Mod 2 = 0 Then
                        Me.ListBox1.SetSelected(i, True)
                    End If
                Next
    

    3rd edit

    Look at the way you are looping, lets assume I create a list of integers, my vb.net is rusty I mainly develop in C#. But assume you did this:

         Dim l As New List(Of Integer)
    
                l.Add(2)
                l.Add(6)
                l.Add(20)
    

    You only have three items in your list, so first loop based on the items on your list, then within the items in your listbox, you have it vice versa. Look at this:

     Dim i As Integer
            Dim l As New List(Of Integer)
    
            l.Add(2)
            l.Add(6)
            l.Add(20)
    
            'load the list with 20 numbers
            For i = 0 To 20
                Me.ListBox1.Items.Add(i)
            Next
    
            Dim lCount As Integer = 0
    
            For lCount = 0 To l.Count - 1
                For i = 0 To 20
                    If i = l.Item(lCount) Then
                        Me.ListBox1.SetSelected(i, True)
                        Exit For
                    End If
                Next
            Next
    

    In the code my l is a list of just 3 items: 2, 6, and 20.
    I add these items to l which is just a list object.
    So now I have to loop using these 3 numbers and compare with my listbox. You have it the opposite you are looping on your listbox and then taking into account the list object.

    Notice in my for loop that once the item in my list is found I no longer need to loop so I exit for. This ensures I dont overdue the amount of looping required. Once the item is found get out and go back to the count of your list object count.

    After running my code here is the result

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list box like this, <asp:ListBox ID=ListBox1 runat=server Height=175px Width=213px> <asp:ListItem Value=all>All</asp:ListItem>
I have a html form which have a select list box from which you
I have a list box control that contains enough items to list them with
i have a list box and i want to add a folder/directory to that
I have a list of Payees in a drop down box on my form.
I have two controls on my form: a listbox with a list of workers
I have ListBox called lstProductGroups. On a simple Windows Form, a method called GetGroups
I have a ListBox on a WPF form. I would like to display a
I have a ListBox (LB) with a DataTable (DT) DataSource in the Form Class
So I have a form that has a listbox that shows like a ledger.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.