Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple Combobox question

Status
Not open for further replies.

smithbr

MIS
May 29, 2003
85
0
0
US
I have a combobox that I want the user to be able to update if it does not contai a choice he needs.
I tried to use this code:
cbotype.Items.Add(txtcboadd.Text)
It would add the needed choice but only for tht session, once i closed the program it would disappear. Is there any way to save it in the combobox for good?
 
Smith,

Can you explain your whole situation more? From your description as wanting it saved, it seems as though you are talking about a data-bound combo box to a database.

If this is the case, it leads me to believe you want a scenario as follows:

[bold]
User needs to create an order for Bob Smith. Bob Smith is not in the combo box list. User types in Bob Smith, which would save Bob Smith in the customers table, allowing him to then create an order for Bob Smith.
[/bold]

The problem with that, is that it sort-of breaks normilization constraints in relational databases. Suppose there was a "Robert Smith", and the user did not know the people were one in the same. Then you have the same customer with 2 customer records.

The approach I would use would be to put a button next to the combo box to add a new entity (customer, part no., whatever). Bring up a modal form where a new entity is created, and upon closing the new form, clear the combo box, datasets, and reload the combo box. They can pick the newly entered item from there.

I've always thought about implementing it the way you want to, but it always got back to me that there are lazy people out there--who if they mis-type something, i.e., Bob Smth (w/o the "i"), they won't notice, and Bob Smth will be saved in the database.
 
In this case, you are better off to read all the data from a textfile into the combobox. This is the only real way to save data. This is what I would do:

1. Remove any items you have in the combo box, or remove the code which creates them.

2.Make a sub procedure to add items to a textfile, and combobox:

Private Sub WriteItems(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Buttonadd.Click
Dim strItem As String
strItem = Me.txtcboadd.Text
Me.CboItems.Items.Add(strItem)
Dim objStreamWriter As System.IO.StreamWriter
'check whether file exists
If System.IO.File.Exists("items.txt") Then
'if exists, add data to file
objStreamWriter = System.IO.File.AppendText("items.txt")
objStreamWriter.WriteLine(strItem)
Else
'if doesn't exist, create new file
objStreamWriter = System.IO.File.CreateText("items.txt")
objStreamWriter.WriteLine(strItem)
End If
objStreamWriter.Close()
End Sub


3. Next, a subprocedure to read items into the combobox:

Private Sub ReadItems()
'read items from file
Dim objStreamReader As System.IO.StreamReader
'check whether file exists
If System.IO.File.Exists("items.txt") Then
objStreamReader = System.IO.File.OpenText("items.txt")
Do Until objStreamReader.Peek = -1
CboItems.Items.Add(objStreamReader.ReadLine)
Loop
objStreamReader.Close()
Else
MessageBox.Show("No items available", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub



4. Finally, in the code for the form load event add the following, so that the items are added into the combobox and are sorted:

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.CboItems.Sorted = True
ReadItems()
End Sub


You can change the path of where the textfile is saved. If you leave it as I have, it will create the file in the bin directory of the project folder.

It is quite simple to do. Give this a try. If you are still unsure, then post back and I can help you out. This is a little easier than using a random access file, and should be sufficient for what you want to do.

Good Luck.
 
One more thing I missed out. You need to add the following to the top of the code:

Imports System.IO
 

What DataSource is your ComboBox bound to???


Email: pankajmsm@yahoo.com
 
The combobox is NOT databound...it is simple full of 6 or 7 different options that can be added to a datagrid. I can get new items to add to the combobox but not save from use to use. I will try AP81's method when I get to work. Alhtough I have never used a textfile before. Thanks for all the help so far.
Brent
 

if your combobox is not bound to any datasource then try AP81 sample. It's quite straight forward

Email: pankajmsm@yahoo.com
 
I finished it and got it to work using AP81's method. I was wonder what I needed to do if the items.txt file was located in another directory. Also, How should I split up the code if the textbox and add new item button are on a seperate form. Thank again for all the help.
 
OK,

1)what I needed to do if the items.txt file was located in another directory

If you want to put the the items.txt file in another directory, you can just do something like this:

objStreamWriter = System.IO.File.AppendText("C:\items.txt")
OR
objStreamWriter = System.IO.File.AppendText("C:\Files\items.txt")

Just make sure you make a complete path to the file, and adjust all code to read and write to the same path.

2)
How should I split up the code if the textbox and add new item button are on a seperate form

If the textbox is on another form, I would make a global variable to assign to the string in the textbox. Add a module (Project>Add Module) to your project and declare a public string in it, e.g

Public strItem as string

After doing so, you can assign the value in the textbox to strNewItem and retrieve it from both forms, e.g

strItem = Me.txtcboadd.Text

Depending on where you assign it, here is one way to assign the value before you display the new form:
Dim Form2 as new Form2
strItem = Me.txtcboadd.Text
Form2.ShowDialog

Also, if you change the variable to a global one, you can remove the line Dim strItem As String from the Private Sub WriteItems, as you would have already declared it.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top