Results 1 to 7 of 7

Thread: ComboBox Duplicate - Problem

  1. #1

    Thread Starter
    Addicted Member Alexandru_mbm's Avatar
    Join Date
    Jul 2007
    Location
    VBForums.com
    Posts
    157

    Cool ComboBox Duplicate - Problem

    Hello again...

    I use for my combobox this code... but i have a problem with this!
    If i hit twice the combobox the data from the file "jd.mbm" is duplicating!

    Code:
    Private Sub cmb_judet_DropDown(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmb_judet.DropDown
    
    Me.cmb_judet.SelectAll()
    
    Dim str As IO.StreamReader = IO.File.OpenText(Application.StartupPath & ".\data\jd.mbm")
    
    While Not str.EndOfStream
    
    Dim line As String = str.ReadLine()
    
    Me.cmb_judet.Items.Add(line)
    
    End While
    
    End Sub

    How can i hit the combobox more than once without duplicating the data ?!

    Thanks in advice!
    I'm still learning VB.NET
    Sorry for my bad english
    Thanks for your help

  2. #2
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: ComboBox Duplicate - Problem

    Well think about it. You are adding those items to the combobox every time it drops down. That means every time you click it, those items are going to be re-added. You'd be better off just adding items in the constructor of the Form or in the Form_Load event. Or even in the InitializeComponent method, even though VS says not to touch it.

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: ComboBox Duplicate - Problem

    I would suggest an alternative solution: The first step of filling a combobox is emptying the combobox, unless you specifically intend to be appending to anything that is already there. Emptying the whole thing takes only one line.
    My usual boring signature: Nothing

  4. #4
    Lively Member tamjap's Avatar
    Join Date
    Jun 2007
    Posts
    99

    Re: ComboBox Duplicate - Problem

    I agree with Fromethius that the logic to load the combobox would be better placed in something like a form load routine. If (for whatever reason) this is impractical, you should use Me.cmb_judet.Items.Clear() at the beginning of your cmb_judet_DropDown routine.


    ~*~*~*~*~*~*~* Wow... I was too slow ~*~*~*~*~*~*~*
    • That's the way things come clear. All of a sudden. And then you realize how obvious they've been all along.

    - Madeleine L'Engle

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: ComboBox Duplicate - Problem

    Quote Originally Posted by tamjap
    ~*~*~*~*~*~*~* Wow... I was too slow ~*~*~*~*~*~*~*
    Yeah, sometimes you have to rush one out. I've been beaten to the punch many times by people who fire off a shorter answer.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Addicted Member Alexandru_mbm's Avatar
    Join Date
    Jul 2007
    Location
    VBForums.com
    Posts
    157

    Re: ComboBox Duplicate - Problem

    Thanks to all for the big help. I'll try it to put into the Form_Load event...

    I've asked this because on the Form_Load statement the combobox takes a value from a database... and... if the user wants to modify this value then the combobox data will be filled from the "jd.mbm" file.

    But i'll try this too... into the Load_Form

    Thanks again!
    I'm still learning VB.NET
    Sorry for my bad english
    Thanks for your help

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

    Re: ComboBox Duplicate - Problem

    If the combo box will ever be changed other than on Form Load, then putting that stuff in the form load event won't solve the problem. Clearing the combo box prior to filling it will solve the problem whether it is in the load event, or anywhere else.
    My usual boring signature: Nothing

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