from a text file to a list box

qwertyjustin
09-09-2000, 12:17 AM
Hello!
i've been trying to have a listbox (lstnames) read from a file HOF.txt, how is this done?



thanx!

usetheforce2
09-09-2000, 12:49 AM
qwert look at the attachment. it demonstrates a simple way to add items to a listbox from a file.
Happy, learning!

qwertyjustin
09-09-2000, 11:25 PM
great, but how do i make the numbers ie. 1234567890
turn into useful words?

usetheforce2
09-09-2000, 11:47 PM
i'm not sure what you mean! Can you explain any further, change 1234567890 into useful words?

quote of the moment:
Suspicion amongst thoughts, are like bats amongst birds, they ever fly by twilight.
-Francis Bacon-

anhmytran
09-10-2000, 01:43 AM
You may have a different version for the same project.
Open and see how the attachment works.
The following is the source code for the form:

Option Explicit
Dim strItem As String

Private Sub cmdExit_Click()
End
End Sub

Private Sub Form_Load()

Open App.Path & "list.ini" For Input As #1
Do While Not EOF(1)
Line Input #1, strItem
List1.AddItem strItem
Loop
End Sub

Private Sub Label1_Click()
Static cnt
If cnt = List1.ListCount - 1 Then
cnt = 0
Else:
cnt = cnt + 1
End If
Label1.Caption = List1.List(cnt)

End Sub

AnhMy_Tran

qwertyjustin
09-10-2000, 06:18 AM
okok, in the list box i might want it to say "university"
and in the label, say something like "Proccess involing matriculation whereby......."


??
how would i alter the code to perform in this manner?

anhmytran
09-10-2000, 10:45 AM
' This project uses an API call specified for getting an INI section
' The Split function stores this section into a temporary array
' The first portion of each element in the array is loaded in the List box
' Meanwile the other portion is loaded in an element of the shared array
' that is the form variable invisible throughout the form
' When user selects an item in the list box, triggering an event
' which inturn, displays the corresponding caption of the label

' The following is the entire ini file for the project
' Notice that the section [BBBB] is the working section
' It may have blank lines, but the API call does not take any of them
' It is my purpose to show the advantages of using this API call

'[AAAA]
'By Pass This Section

'[BBBB]
'security|Brighten the future of you and your loved ones
'university|Proccess involing matriculation whereby
'hospitalily|Practice your generousity with courtesy

'curiousity|Keep safe all your list items

'[CCCC]
'Ignore this section
'End of the third section

Option Explicit
' This array stores strings for label's captions
Dim strArr() As String
' This API function reads a section of an ini file
Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias _
"GetPrivateProfileSectionA" (ByVal lpAppName As String, _
ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

Public Function GetIniSection(sIniFile As String, sSection As String) As String
' This function stores the specified section in the specific ini file in a string
Dim sDefVal As String
Dim sRetVal As String
Dim sTemp As String
Dim lRet As Long
sRetVal = Space(32767)
lRet = GetPrivateProfileSection(sSection, sRetVal, Len(sRetVal), sIniFile)
sTemp = IIf(lRet > 0, Left(sRetVal, lRet), "")
GetIniSection = Replace(sTemp, Chr(0), vbNewLine)
End Function

Private Sub Form_Load()
' Get the [BBBB] section, ignoring the other sections
Dim strWorking As Variant
strWorking = Split(GetIniSection(App.Path & "list.ini", "BBBB"), vbNewLine)

' Load List box and array of captions
Dim intIndex As Integer, intPos As Integer
ReDim strArr(UBound(strWorking) - 1)
For intIndex = 0 To UBound(strWorking) - 1
intPos = InStr(1, strWorking(intIndex), "|")
' Add an Item to the List box
List1.AddItem Left(strWorking(intIndex), intPos - 1)
' Load an element to the array of captions
strArr(intIndex) = Right(strWorking(intIndex), Len(strWorking(intIndex)) - intPos)
Next intIndex
End Sub


Private Sub List1_Click()
' Display the element of the array corresponding to item in the list box
Label1.Caption = strArr(List1.ListIndex)
End Sub


Private Sub cmdExit_Click()
End
End Sub

AnhMy_Tran

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum