Results 1 to 19 of 19

Thread: [RESOLVED] Combo Box Index

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2022
    Posts
    66

    Resolved [RESOLVED] Combo Box Index

    Hello. Is there an index added to a combo box list which I could use to determine which items were added first? I'm trying to set a limit on how many items are held, however, if a new item is added then I need to remove the oldest one to ensure the combo box retains X number of items. I guess I also need to know if the index automatically re-numbers itself if one item is removed and if not, then to consider removing the oldest (possibly using remove(0) if 0 is the first record) and then re-number the remaining items and continue removing the oldest item until the limit is reached.

    I've tried the below code and although the limit and count are correct, it doesn't remove any records.

    Any help would be appreciated. Thanks

    Code:
    Dim Limit as Integer = 4
    For j = Limit To CB_ClientMatter.Items.Count
                        If CB_ClientMatter.Items.Count > Limit Then
                            Me.CB_ClientMatter.Items.Remove(0)
                        End If
                    Next

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Sep 2022
    Posts
    66

    Re: Combo Box Index

    I've just realised I forgot to say that I also need to capture a unique ID number with each combo box item. I was doing this with an insert(ID, Description), but wasn't sure if the ID was setting the index. So ideally I need to determine which items were added first and to have a hidden ID, which can be called when the item is selected, if that makes sense? Thanks

  3. #3
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: Combo Box Index

    put a Combobox and a button on a form and try that :

    Code:
    Public Class Form1
    
        Dim i As Integer = 0
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim limit As Integer = 4
    
    
            If ComboBox1.Items.Count < 4 Then
                ComboBox1.Items.Add(i + 1)
            Else
                ComboBox1.Items.RemoveAt(0)
                ComboBox1.Items.Add(i + 1)
            End If
            i += 1
    
        End Sub
    End Class
    by the way, yes any list is renumbered when you remove an item in it so if you remove the second item, the third become the second, etc
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Sep 2022
    Posts
    66

    Re: Combo Box Index

    Great, thanks for the quick reply. I'll give this a go and will let you know how I get on. Thanks again.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Combo Box Index

    If you want a display Field and a hidden attribute Field, use a class…

    Code:
    Public Class customItem
        Public Property Display As String
        Public Property Hidden As String ‘ or whatever data type you choose
    
        Public Overrides Function ToString As String
            Return Me.Display
        End Function
    End Class
    If you use,
    Code:
    ListBox1.Items.Add(New customItem With {.Display=“whatever”, .Hidden=“your hidden value”})
    , index 0 will always be the oldest, and ListBox1.Items.Count - 1 will always be the newest.

    To remove the oldest, use…
    Code:
    ListBox1.Items.RemoveAt(0)

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Combo Box Index

    In ListBox1.SelectedIndexChanged, use…

    Code:
    Dim item as customItem = DirectCast(ListBox1.SelectedItem, customItem)
    MsgBox(item.Hidden)

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Combo Box Index

    You might also look into a Stack or Queue. The Queue would work better. Whenever you add one, you can dequeue one (and discard it).
    My usual boring signature: Nothing

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Sep 2022
    Posts
    66

    Re: Combo Box Index

    Great, thanks both Paul and Shaggy Hiker for the advice and code. I'll look into both suggestions.
    Last edited by db74; Apr 3rd, 2023 at 02:04 AM.

  9. #9
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Combo Box Index

    Quote Originally Posted by Shaggy Hiker View Post
    You might also look into a Stack or Queue. The Queue would work better. Whenever you add one, you can dequeue one (and discard it).
    Using a Queue

    Some test data

    Code:
        Private CB_ClientMatterItems As New Queue(Of String)
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            For x As Integer = 111 To 118
                CB_ClientMatterItems.Enqueue(x.ToString)
            Next
            CB_ClientMatter.DataSource = CB_ClientMatterItems.ToList
        End Sub
    and then limit code though I like Shaggy's idea of checking when you add,

    Code:
            Const limit As Integer = 4
    
            While CB_ClientMatterItems.Count > limit
                CB_ClientMatterItems.Dequeue()
            End While
            CB_ClientMatter.DataSource = CB_ClientMatterItems.ToList
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Sep 2022
    Posts
    66

    Re: Combo Box Index

    Hi @.Paul. I've implemented your suggestion and it works well (thanks). I have also linked the my.setting to capture the user data to the type of the custom item and I can capture the data in the user.config file fine when using the selected item (DirectCast(Listbox1.SelectedItem, List)), which pulls out both fields. However, I'm struggling to update the my.setting field from the combobox full list by using a for each statement direct from Listbox1. Any advice would be appreciated, as hopefully this is one of the last things I need to do before completing my project. Thanks

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Sep 2022
    Posts
    66

    Re: Combo Box Index

    Quote Originally Posted by dbasnett View Post
    Using a Queue

    Some test data

    Code:
        Private CB_ClientMatterItems As New Queue(Of String)
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            For x As Integer = 111 To 118
                CB_ClientMatterItems.Enqueue(x.ToString)
            Next
            CB_ClientMatter.DataSource = CB_ClientMatterItems.ToList
        End Sub
    and then limit code though I like Shaggy's idea of checking when you add,

    Code:
            Const limit As Integer = 4
    
            While CB_ClientMatterItems.Count > limit
                CB_ClientMatterItems.Dequeue()
            End While
            CB_ClientMatter.DataSource = CB_ClientMatterItems.ToList
    Great, thanks, I will have a play with the code.

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Sep 2022
    Posts
    66

    Re: Combo Box Index

    Not sure what I did previously, but the following seems to work, or it might if I can clear the my.settings list before it loads, but there doesn't seem to be a clear event when using a custom class.

    Code:
                    For Each item In CB_ClientMatter.Items
                        My.Settings.ClientMatterList = item
                    Next
    Could anyone advise how to clear the my.settings field with a custom class attached, as per Paul's recommendation above? Thanks

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Combo Box Index

    Code:
    Public Class customItem
        Public Property Display As String
        Public Property Hidden As String ‘ or whatever data type you choose
    
        Public Sub New()
            ' no arguments here
        End Sub
    
        Public Sub New(item As String)
            Dim fields() as string = item.Split(":"c)
            Me.Display = fields(0)
            Me.Hidden = fields(1)
        End Sub
    
        Public Function ciAsString As String
            Return Me.Display & ":" & Me.Hidden
        End Function
    
        Public Overrides Function ToString As String
            Return Me.Display
        End Function
    End Class
    Code:
    ' to save
    My.Settings.ClientMatterList = New System.Collections.Specialized.StringCollection
    For Each item In CB_ClientMatter.Items
        My.Settings.ClientMatterList.Add(DirectCast(item, customItem).ciAsString)
    Next
    Code:
    ' to load
    For Each s As String in My.Settings.ClientMatterList
        CB_ClientMatter.Items.Add(New customItem(s))
    Next

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Sep 2022
    Posts
    66

    Re: Combo Box Index

    Great, thanks Paul. I initially thought of using a specialized string collection, but thought I needed to use the custom class to match the same format. I appreciate your time with providing the code. Thanks again.

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Sep 2022
    Posts
    66

    Re: Combo Box Index

    Hi Paul. Thanks again for the code it's working really well and it has also helped me understand splitting strings. The load from my settings to the combo box work well, with the exception of the first load when the application is opened. I've added the code below on the form load, which is a child and opened from the main application, so there can be multiple instances of the child opened. Once I populate the combo box for the first time after opening the application then any additional instances of the form work fine and the my.settings populates the combo box. I've checked the user.config file and note that the data is held against the setting correctly before the application is opened. I changed the split from : to a | as I was using a colon in the description part of the string, so thought it might impact the split.

    If I remove the if statement, to check if data exists in my.settings or not then it fails with "Object reference not set to an instance of an object" and "App.My.MySettings.get_ClientMatterList returned Nothing.", but the data does exist in the config file.

    Any ideas on what I might be doing wrong would be appreciated. Thanks

    Code:
                If My.Settings.ClientMatterList IsNot Nothing Then
                    For Each s As String In My.Settings.ClientMatterList
                        CB_ClientMatter.Items.Add(New ClientMatterList(s))
                    Next
    Last edited by db74; Apr 10th, 2023 at 02:30 PM.

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Sep 2022
    Posts
    66

    Re: Combo Box Index

    I don't seem to be able to get any further on this. I have tried a basic specialized string collection and it still doesn't bring in the data on the first load of the form, despite the string being saved in the config file. All my other my.settings work as expected, but not this one. The code obviously works, as opening any addition forms will load the data correctly from my.settings to the combobox. If I set a default string in the property settings of the project then this also works as expected, so the issue seems to be with getting the string from the config file into the setting. I can't think what else to try? Thanks

  17. #17
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Combo Box Index

    Quote Originally Posted by db74 View Post
    Hi Paul. Thanks again for the code it's working really well and it has also helped me understand splitting strings. The load from my settings to the combo box work well, with the exception of the first load when the application is opened. I've added the code below on the form load, which is a child and opened from the main application, so there can be multiple instances of the child opened. Once I populate the combo box for the first time after opening the application then any additional instances of the form work fine and the my.settings populates the combo box. I've checked the user.config file and note that the data is held against the setting correctly before the application is opened. I changed the split from : to a | as I was using a colon in the description part of the string, so thought it might impact the split.

    If I remove the if statement, to check if data exists in my.settings or not then it fails with "Object reference not set to an instance of an object" and "App.My.MySettings.get_ClientMatterList returned Nothing.", but the data does exist in the config file.

    Any ideas on what I might be doing wrong would be appreciated. Thanks

    Code:
                If My.Settings.ClientMatterList IsNot Nothing Then
                    For Each s As String In My.Settings.ClientMatterList
                        CB_ClientMatter.Items.Add(New ClientMatterList(s))
                    Next
    I’ve never seen that happening. One thing I did notice is your custom class name conflicts with your StringCollection name…

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Sep 2022
    Posts
    66

    Re: Combo Box Index

    Ah, thanks Paul, I'll amend one of the names and try again just in case it is causing a conflict. Thanks

  19. #19

    Thread Starter
    Lively Member
    Join Date
    Sep 2022
    Posts
    66

    Re: Combo Box Index

    Thanks again Paul, looks like you were right. I have renamed the custom class and the my.setting and it's now working. Thanks again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width