Link to home
Start Free TrialLog in
Avatar of kkbenj
kkbenj

asked on

.NET add to combo box if value not already there

I am currently filling a combo box from a text file.  But what I really want is just the distinct values, make the insert only if it isn't already in the list.

Here's the code to fill the box on load of my Win form:
-------------------------------------------------------------------------------------------------------------------
Private Sub AxiomLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim fnCnt As Long        
        Dim fsReq As String
        Dim fsFile As String
        Dim fsLine As String
        Dim fsLoginNm As String
        Dim strTextToFind As String
        Dim intForReading As Long
        Dim objFSO As Object
        Dim objFile As Object

        intForReading = 1
        fnCnt = 0
        fsReq = "Inst Time"
 
        fsFile = "C:\test.txt"
        objFSO = CreateObject("Scripting.FileSystemObject")
        objFile = objFSO.OpenTextFile(fsFile, intForReading, False)

        While Not objFile.AtEndOfStream
            fsLine = objFile.ReadLine
            If InStr(fsLine, fsReq) > 0 Then
                fnCnt = fnCnt + 1
                cbLogins.Items.Add(fsLoginNm)
            End If
        End While

        objFile.Close()
        objFile = Nothing

    End Sub
-------------------------------------------------------------------------------------------------------------------
How can I get this done?
Avatar of swapneel_d14
swapneel_d14

insert where in Combobox?

and why you are using Scripting.FileSystemObject to open a file when you have File or Fileinfo object
Avatar of kkbenj

ASKER

swapneel:

The combo box gets loaded with:
 cbLogins.Items.Add(fsLoginNm)

I am using someone else's file reading script.  What do you suggest with File or Fileinfo object?
ASKER CERTIFIED SOLUTION
Avatar of swapneel_d14
swapneel_d14

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kkbenj

ASKER

That is PERFECT!  Thanks for your quick responses!