Visual Studio Development Bookmark and Share   
 index > Visual Basic Express Edition > Saving in ComboBox
 

Saving in ComboBox

Hello!

How do i do to update Item (Collection)?

Can't get it to work!

This code put the text into ComboBox but it doesen't save it to Item Collection

Combobox1.Item.Add(TextBox1.Text)

TextBox1.ResetText()

TextBox1.Focus()

I.H  Tuesday, August 01, 2006 9:03 PM

well yes it will lose the items because you did not save them to a storage format. you can save them to a file and reload them after. example:

Dim theWriter as new StreamWriter(filename)

for each curItem as string in me.theComboBox.Items

theWriter.WriteLine(curItem)

next

theWriter.Close()

Dim theReader as new StreamReader(filename)

do while theReader.Peek()<> -1

Me.theComboBox.Items.Add(theReader.ReadLine())

loop

theWriter.Close()

ahmedilyas  Wednesday, August 02, 2006 11:45 AM

what happens? any errors? the code seems ok to me:

 

Me.theComboBox.Items.Add(Me.theTextBox.Text)

 

be sure to replace the names of the controls as appropriate

The list will have the item added to it....and you can also go through each item programmatically and see for yourself:

 

for each currentItem as string in this.theComboBox.Items

 MessageBox.Show(currentItem)

next

 

 

 

ahmedilyas  Tuesday, August 01, 2006 9:25 PM

What part doesnt work - you'll need to be more specific

Example Code to determine items collection is being added to correctly

Me.ComboBox1.Items.Add("foo1")
MsgBox(ComboBox1.Items.Count)

Me.ComboBox1.Items.Add("foo2")
MsgBox(ComboBox1.Items.Count)

Me.ComboBox1.Items.Add("foo3")
MsgBox(ComboBox1.Items.Count)

Me.ComboBox1.Items.Add("foo4")
MsgBox(ComboBox1.Items.Count)


Dim stotalstring As String = ""
For Each s As String In Me.ComboBox1.Items
stotalstring = stotalstring & s & vbCrLf
Next

MsgBox(stotalstring)

Seems to work just fine

spotty  Tuesday, August 01, 2006 9:50 PM

the code should work - looks fine to me

Me.theComboBox.Items.Add(me.theTextbox.Text)

of course replace the variable names (control names) as appropriate.

what happens when u add the items?

ahmedilyas  Tuesday, August 01, 2006 10:58 PM

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

ComboBox1.Items.Add(TextBox1.Text)

MsgBox(ComboBox1.Items.Item(ComboBox1.Items.Count - 1) & " is part of the collection!")

TextBox1.ResetText()

TextBox1.Focus()

End Sub

End Class

Tall Dude  Tuesday, August 01, 2006 11:50 PM

Tanks for all answers!

All the codes i have tried works and there is no "errors"!

Saving to combobox works fine wile the program is running, but when i close and reopen it all saved

data disappear.

It doesen't save to "item collection".

Is there some adjustment to be done in property?

I.H  Wednesday, August 02, 2006 11:05 AM

well yes it will lose the items because you did not save them to a storage format. you can save them to a file and reload them after. example:

Dim theWriter as new StreamWriter(filename)

for each curItem as string in me.theComboBox.Items

theWriter.WriteLine(curItem)

next

theWriter.Close()

Dim theReader as new StreamReader(filename)

do while theReader.Peek()<> -1

Me.theComboBox.Items.Add(theReader.ReadLine())

loop

theWriter.Close()

ahmedilyas  Wednesday, August 02, 2006 11:45 AM

Thanks for answering, i hope it's gonna work!

I.H  Wednesday, August 02, 2006 8:16 PM

You can use google to search for other answers

Custom Search

More Threads

• Conversion from string "" to type 'Integer' is not valid.
• Web Page Character Encoding Help
• Basic coding errors- Please help
• Serial Port in VB2008 vs 2005
• Can only delete numbers in access
• Open a file externally
• how do i make my settings change when i close my program?
• Deployment
• Need to change encryption of Northwind.sdf database for VB 2008 Express
• Connecting to an Access 2003 Database from VB 2005 Express