• Welcome to Valhalla Legends Archive.
 

How to add item in the List/Combo Box

Started by ramu_s, August 26, 2004, 07:34 AM

Previous topic - Next topic

ramu_s

 Hi

I am very new to VB, I want to develop a combo box or list box - when ever the user want to add the item to the combo box or list box- they have to type the item in the below text box then pressing the command button- it have to save in the combo/list box.

I am using the following code

Private Sub Command1_Click()
Combo1.AddItem (Text1.Text)
End Sub

This adding the item in the combo box but when I close and reopen all gone!!!!!

But I know through form load we can add item but the normal user want to enter the item in the text box and save in the combo box

Any one can help me please

[email protected]

Regards
Ramu


Meh

When the form is unloaded it must be written to a file, like a text file named combo.txt or something. There should be code around to do it.

ramu_s

Hi Meh

Thanks, is it possible to explain me more? :- pls

Regards
Ramu

Eli_1

#3
Let me break down his explination...  Kinda.   :P


On Form Load()

   Open up the text file, containing the items, for input
   Do
       Store the next line, in the text file, in a buffer
       If the buffer isn't empty, add it to the listbox
   Loop until EOF (end of file)

   Close the text file

End Sub


   . . .

Allow the user to add items to the listbox

   . . .

On Form Unload()
   
   Open the text file for output
   For i = 0 to the amount of items in the listbox - 1
       Print the item(i) to the text file
   Next i
   Close the text file
   
End Sub

ramu_s

Thanks Kinda

Will try based on your help, I don't how far I can reach!!!!

Thanks again
Ramu

SPY-3

Quote from: ramu_s on August 26, 2004, 07:34 AM
Hi

I am very new to VB, I want to develop a combo box or list box - when ever the user want to add the item to the combo box or list box- they have to type the item in the below text box then pressing the command button- it have to save in the combo/list box.

I am using the following code

Private Sub Command1_Click()
Combo1.AddItem (Text1.Text)
End Sub

This adding the item in the combo box but when I close and reopen all gone!!!!!

But I know through form load we can add item but the normal user want to enter the item in the text box and save in the combo box

Any one can help me please

[email protected]

Regards
Ramu


if you want the easiest way then add this to a module

Public Sub LoadList(List As ListBox, FileLocation As String)
Dim sText As String
Dim LoadTheList
LoadTheList = FreeFile
On Error GoTo Make:
Open FileLocation For Input As #LoadTheList
Do Until EOF(1)
Input #LoadTheList, sText
sText = Replace(sText, "%Comma/", ",")
List.AddItem sText
Loop
Close #LoadTheList
Exit Sub
Make:
Dim MakeTheList
MakeTheList = FreeFile
Open FileLocation For Output As #MakeTheList
Close #MakeTheList
LoadList List, FileLocation
End Sub

Public Sub SaveList(List As ListBox, SaveTo As String)
Dim sText As String
Dim SaveTheList
SaveTheList = FreeFile
Open SaveTo For Output As #SaveTheList
For x = 0 To List.ListCount - 1
sText = Replace(List.List(x), ",", "%Comma/")
Print #SaveTheList, sText
Next x
Close #SaveTheList
End Sub

then call on it like this
Private Sub Form_Load()
LoadList combo1, app.path & "\somefile.txt"
End Sub

Private Sub Form_Unload()
savelist combo1, app.path & "\somefile.txt"
End Sub
if you do this then you will be able to have the form save things and load things when it opens/closes
hope this helps.