|
-
Jan 23rd, 2007, 12:41 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Combobox
Hi all,
I have a combo box and when a button is pressed I want it to check the .text value and see if it is the the items if it is not I want to add it, can someone help me with this.
Thanks
Sam
-
Jan 23rd, 2007, 12:52 PM
#2
Addicted Member
Re: Combobox
That should work for you. If your using VS 2005
VB Code:
Public Sub Command1_Click(ByVal Sender as System.Object,ByVal e as System.EventArgs) Handles Button1.Click
if ComboBox1.Items.Contains(ComboBox1.Text) = False then
ComboBox1.Items.Add(ComboBox1.Text)
End If
End Sub
-
Jan 23rd, 2007, 12:57 PM
#3
Re: Combobox
VB Code:
If Not Me.ComboBox1.Items.Contains("hello") Then
Me.ComboBox1.Items.Add("hello")
End If
"The dark side clouds everything. Impossible to see the future is."
-
Jan 25th, 2007, 10:16 AM
#4
Thread Starter
Addicted Member
Re: Combobox
Hi
I have done this:
VB Code:
If cmbname.Items.Contains(cmbname.Text) = True Then
Else
cmbname.Items.Add(cmbname.Text)
End If
But this does not save it for when the form it is opened next, how do I do this?
Thanks for your help so far
Sam
-
Jan 25th, 2007, 12:11 PM
#5
Re: Combobox
I think this depends if you are creating a new instance of the Form again. If it's just revealing the form after it has been hidden it should still be there using the .Show and .Hide methods.
If you are closing a form and creating a new instance then save the selection into a global variable perhaps and on the form load event set the text of the combobox to the variable (or index value if you prefer).
Your code isn't correct by the way. Just paste either of the examples posted above into your code.
Last edited by stimbo; Jan 25th, 2007 at 12:17 PM.
-
Jan 25th, 2007, 06:08 PM
#6
Thread Starter
Addicted Member
Re: Combobox
I want it like when you add the items in the properties of the combobox, everytime you start the app they are there. Can this not be done like this using coding, do you have to use the variable to save them?
Sam
-
Jan 25th, 2007, 06:51 PM
#7
Re: Combobox
If you go to:
Project -> Properties
There's an option down the left hand side called 'Settings'.
This allows you to save settings, properties and preferences. I've never used it but will be the best option.
You should go read about it on MSDN or google it to find out more. Or just play about with it.
-
Jan 25th, 2007, 07:06 PM
#8
Re: Combobox
Stimbo is quite correct. Go to the Settings tab and create a User setting of type StringCollection. You'll notice that the Value field is empty. Click it and then click the browse button. Now add a dummy string and press OK. You'll now note that the Value field contains some XML code. Click the browse button again, remove the dummy string and press OK. This leaves the XML code behind so you now have an empty StringCollection.
Now you have to bind that collection to your ComboBox. Assuming that you have named the setting MySetting, you would handle the form's Load event and use this code:
VB Code:
Me.ComboBox1.DataSource = My.Settings.MySetting
Now when you want to add an item you add it to the collection, not the ComboBox itself:
VB Code:
If Not My.Settings.MySetting.Contains(Me.ComboBox1.Text) Then
My.Settings.MySetting.Add(Me.ComboBox1.Text)
End If
Saving the settings is handled automatically by the system when your app shuts down, as is loading them when it starts up.
-
Jan 27th, 2007, 01:44 PM
#9
Thread Starter
Addicted Member
Re: Combobox
Right Ok I have got most of this to work, except it does not put it into the combo until I have closed the app then started it again.
This is my code for the combo combobox form load:
VB Code:
cmbname.DataSource = My.Settings.products
Then a button opens another form where the items that are in the combo box are listed and there is a textbox for the new item to be entered:
VB Code:
'form load
lstexisting.DataSource = My.Settings.products
'btn add
'the adding works but i want to update the listbox and the combobox
If Not My.Settings.products.Contains(txtnew.Text) Then
My.Settings.products.Add(txtnew.Text)
lstexisting.DataSource = My.Settings.products
End If
Form1.cmbname.DataSource = My.Settings.products
-
Jan 27th, 2007, 06:12 PM
#10
Re: Combobox
My mistake. The StringCollection doesn't raise an event when it changes to notify the control to update. You should still store your values in a StringCollection setting but use a BindingList for the data binding instead.
VB Code:
'Create a binding list.
Private settingsList As New System.ComponentModel.BindingList(Of String)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Copy all the items from the application setting to the binding list.
For Each item As String In My.Settings.Setting
settingsList.Add(item)
Next item
'Bind the control(s) to the binding list.
Me.ComboBox1.DataSource = settingsList
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Add an item to the binding list, which will be reflected in the bound control(s).
Me.settingsList.Add(Me.settingsList.Count.ToString())
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
My.Settings.Setting.Clear()
'Copy all the items from binding list to the application setting.
For Each item As String In settingsList
My.Settings.Setting.Add(item)
Next item
End Sub
-
Jan 29th, 2007, 11:20 AM
#11
Thread Starter
Addicted Member
Re: Combobox
Thanks Alot for your help so far, I have go this working. How would you go about deleting an item?
I was thinking as I have them in a listbox, I could use the remove selected item, and then clear the binding list and loop through the listbox adding the list back into the binding list without the one I deleted. Would this work? Or can you think of a better way.
Sam
-
Jan 29th, 2007, 11:38 AM
#12
Thread Starter
Addicted Member
Re: Combobox
I did it using this:
VB Code:
Form1.settingsList.Remove(lstexisting.SelectedItem)
Alot easier than I orginally thought.
Thanks for Your help
-
Feb 8th, 2007, 12:01 PM
#13
Thread Starter
Addicted Member
Re: [RESOLVED] Combobox
Hi
Can someone help me with this, when I add new things eg
A1 50W pa (price per unit if 10 ordered)
A1 50W pa (price per unit if 50 ordered)
Each of these are separate items.
The second one does not come out the same as what I typed in eg
A11 50W pa (price per unit if 10 ordered)
or
A1 51W pa (price per unit if 10 ordered)
or
A1 50W pa (price per unit if 51 ordered)
Does anyone know why this is happening?
Sam
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|