Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Visual Basic 2008 - Combobox Confusion :P  (Read 2662 times)

0 Members and 1 Guest are viewing this topic.

jimbo8098

    Topic Starter


    Beginner

    Visual Basic 2008 - Combobox Confusion :P
    « on: January 29, 2010, 06:33:31 PM »
    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?


    I am currently working for clicknetworks IT Support. Please feel free to visit our site at http://www.clicknetworks.co.uk

    jimbo8098

      Topic Starter


      Beginner

      Re: Visual Basic 2008 - Combobox Confusion :P
      « Reply #1 on: January 29, 2010, 06:38:08 PM »
      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()


      I am currently working for clicknetworks IT Support. Please feel free to visit our site at http://www.clicknetworks.co.uk

      BC_Programmer


        Mastermind
      • Typing is no substitute for thinking.
      • Thanked: 1140
        • Yes
        • Yes
        • BC-Programming.com
      • Certifications: List
      • Computer: Specs
      • Experience: Beginner
      • OS: Windows 11
      Re: Visual Basic 2008 - Combobox Confusion :P
      « Reply #2 on: January 29, 2010, 06:54:59 PM »
      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.



      I was trying to dereference Null Pointers before it was cool.