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.Fi leSystemOb ject")
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(fsLogin Nm)
End If
End While
objFile.Close()
objFile = Nothing
End Sub
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------
How can I get this done?
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.Fi
objFile = objFSO.OpenTextFile(fsFile
While Not objFile.AtEndOfStream
fsLine = objFile.ReadLine
If InStr(fsLine, fsReq) > 0 Then
fnCnt = fnCnt + 1
cbLogins.Items.Add(fsLogin
End If
End While
objFile.Close()
objFile = Nothing
End Sub
--------------------------
How can I get this done?
ASKER
swapneel:
The combo box gets loaded with:
cbLogins.Items.Add(fsLogin Nm)
I am using someone else's file reading script. What do you suggest with File or Fileinfo object?
The combo box gets loaded with:
cbLogins.Items.Add(fsLogin
I am using someone else's file reading script. What do you suggest with File or Fileinfo object?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That is PERFECT! Thanks for your quick responses!
and why you are using Scripting.FileSystemObject