Adding items to combobox dynamically

f15e

Member
Joined
May 4, 2006
Messages
21
Programming Experience
Beginner
I am looking to add items as needed to my combobox without having to do it in the code. For example, I open the application and need to add an item. I want to be able to click a button or whatever and type in the name of the new item in the combobox list and then it appears in the list. Now from there, I want to attach an address to that newly created item in order for it to open a file if that item is selected from the combobox. Is this possible?

Also, would I be able to delete items in the combobox while the application is running?
 
Wow! When you find out how to make an application do something without code can you let me know. That would be sooooo cool.

All sarcasm aside, you cannot do ANYTHING in an application without code. When you use the designer in the IDE all it does is generate code for you. Software is nothing but code. If you press a button some code is run and the Click event is raised, giving you a chance to execute some of your own code. That is where you write the code to add an item to your ComboBox.
 
Combobox

Sarcasm not funny. Anyway, I didn't me that a program can run without any code. Of course you need to write code to make it do something. What I meant was, as an application is running, being able to add things to it without having to go into the code itself and write it and compile it and so forth; that I would be able to add items to the combobox list as the application is running. I don't want to have to stop the application and go into the design window to add it there. I want to be able to use this application at work where I don't have VB.NET installed, with the exception of the .NET framework which of course I need in order to run VB applications. I want to add items to the combobox on the fly.
 
Like I said, when you do something in the designer all it does is generate code. When you add an item to a ComboBox it generates a line of code that calls the Add method of the ComboBox's Items collection. You can open the Designer.vb file for your form to see the code the IDE has generated. If you want to add an item to a ComboBox at run time then you canuse the same code, e.g.
VB.NET:
If Not Me.ComboBox1.Items.Contains(Me.ComboBox1.Text) Then
    'The user has typed a new value into the control so add it to the list.
    Me.ComboBox1.Items.Add(Me.ComboBox1.Text)
End If
Having said that, this doesn't change your compiled code at all, so next time you run the app the ComboBox will revert to its original state. There's nothing you can do about that, but if you want to save the items that get added then you can do so and just load them each time the app is run. The appropriate way to do that would be with an application setting of type StringCollection. You should read the MSDN documentation for the ComboBox and its members for more information on that. For more information about persisting data between sessions read about the 'My.Settings' object at MSDN.
 
Back
Top