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!
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.
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.
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 :):):) ~*~*~*~*~*~*~*
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. :wave:
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!
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.