1.

Solve : Visual Basic 2008 - Combobox Confusion :P?

Answer»

I'm trying to get a combobox to read a CONF file so that i can permenantle save some stuff.

As a test the conf file contains:

1. Battlefield 1 (a directory of the file in brackets)
2. Battlefield 2 "
3. Battlefield 3 "
4. Battlefield 4 "

Now heres the code

Code: [Select] is_conf_made = Dir(confloc)
If is_conf_made <> "" Then
counter = 0
FileReader = New StreamReader(confloc)
Do
counter = counter + 1
line = FileReader.ReadLine()
If counter >= 1 And counter < 10 Then
txt_game_name = 3
Else
txt_game_name = 4
End If
game = Mid(line, txt_game_name)
game_name(counter) = game
cboxSelectgame.MaxDropDownItems = counter
If line = Nothing Then
EXIT Do
End If
If counter >= 1 Then
cboxSelectgame.Items.Add(game_name(counter))
MsgBox(game_name(counter))
End If
Now when i run this PROGRAM , the program will check for the conf file (and find it) and will then try and read all of the games i put there... When i run the program HOWEVER the following shows:

1
Battlefield 1
Battlefield 2
Battlefield 3
Battlefield 4

And i cant for the life of me get rid of that 1

does anyone know how i could do that?
LOL got it working:

Code: [Select]is_conf_made = Dir(confloc)
If is_conf_made <> "" Then
counter = 0
FileReader = New StreamReader(confloc)
Do
counter = counter + 1
line = FileReader.ReadLine()
If counter >= 1 And counter < 10 Then
txt_game_name = 3
Else
txt_game_name = 4
End If
game = Mid(line, txt_game_name)
game_name(counter) = game
cboxSelectgame.MaxDropDownItems = counter
If line = Nothing Then
Exit Do
End If
If counter >= 1 Then
If cboxSelectgame.Items.Contains("1") Then
cboxSelectgame.Items.RemoveAt(0)
End If
cboxSelectgame.Items.Add(game_name(counter))
MsgBox(game_name(counter))
End If
Me.Show()or you could write it properly to begin with... heh.

Code: [Select] Dim is_conf_made As String, confloc As String
Dim counter As Long, FileReader As StreamReader
confloc = "D:\combocontents.txt"

is_conf_made = IIf(File.Exists(confloc), confloc, "")

If is_conf_made <> "" Then
counter = 0
FileReader = New StreamReader(confloc)
Do Until FileReader.EndOfStream
Dim lineread = FileReader.ReadLine()
Dim game As String = lineread
cboxselectgame.Items.Add(game)
Loop



End If

You didn't have any of the variables you had declared.... well, you probably did, but conveniently forgot to actually include all the code. Also, you made no mention of your requirements whatsoever. Anyway, that segment above, once you strip out the assignment statement and DECLARATIONS at the top, will load the contents of each line in "conflac" into the combobox cboxselectgame.

Also, you should avoid anything from VB6 if there is an equivalent in .NET; in this case, the static File.Exists() would work better then Dir(). remove the maxdropdownitems property assigment. it's useless and doesn't do anything.

Lastly, your original issue is probably caused by you adding an item accidentally in the designer window.





Discussion

No Comment Found