Results 1 to 13 of 13

Thread: [RESOLVED] Combobox

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    208

    Resolved [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

  2. #2
    Addicted Member Daystar's Avatar
    Join Date
    Dec 2006
    Location
    Pahrump, NV
    Posts
    132

    Re: Combobox

    That should work for you. If your using VS 2005
    VB Code:
    1. Public Sub Command1_Click(ByVal Sender as System.Object,ByVal e as System.EventArgs) Handles Button1.Click
    2. if ComboBox1.Items.Contains(ComboBox1.Text) = False then
    3.  ComboBox1.Items.Add(ComboBox1.Text)
    4. End If
    5. End Sub

  3. #3
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036

    Re: Combobox

    VB Code:
    1. If Not Me.ComboBox1.Items.Contains("hello") Then
    2.             Me.ComboBox1.Items.Add("hello")
    3. End If
    "The dark side clouds everything. Impossible to see the future is."

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    208

    Re: Combobox

    Hi

    I have done this:
    VB Code:
    1. If cmbname.Items.Contains(cmbname.Text) = True Then
    2.         Else
    3.             cmbname.Items.Add(cmbname.Text)
    4.  
    5.         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

  5. #5
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    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.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    208

    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

  7. #7
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    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.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. 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:
    1. If Not My.Settings.MySetting.Contains(Me.ComboBox1.Text) Then
    2.     My.Settings.MySetting.Add(Me.ComboBox1.Text)
    3. End If
    Saving the settings is handled automatically by the system when your app shuts down, as is loading them when it starts up.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    208

    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:
    1. 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:
    1. 'form load
    2.  lstexisting.DataSource = My.Settings.products
    3.  
    4.  
    5. 'btn add
    6. 'the adding works but i want to update the listbox and the combobox
    7. If Not My.Settings.products.Contains(txtnew.Text) Then
    8.             My.Settings.products.Add(txtnew.Text)
    9.             lstexisting.DataSource = My.Settings.products
    10.         End If
    11.  
    12.  
    13.         Form1.cmbname.DataSource = My.Settings.products

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. 'Create a binding list.
    2. Private settingsList As New System.ComponentModel.BindingList(Of String)
    3.  
    4. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    5.     'Copy all the items from the application setting to the binding list.
    6.     For Each item As String In My.Settings.Setting
    7.         settingsList.Add(item)
    8.     Next item
    9.  
    10.     'Bind the control(s) to the binding list.
    11.     Me.ComboBox1.DataSource = settingsList
    12. End Sub
    13.  
    14. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    15.     'Add an item to the binding list, which will be reflected in the bound control(s).
    16.     Me.settingsList.Add(Me.settingsList.Count.ToString())
    17. End Sub
    18.  
    19. Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    20.     My.Settings.Setting.Clear()
    21.  
    22.     'Copy all the items from binding list to the application setting.
    23.     For Each item As String In settingsList
    24.         My.Settings.Setting.Add(item)
    25.     Next item
    26. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    208

    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

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    208

    Re: Combobox

    I did it using this:
    VB Code:
    1. Form1.settingsList.Remove(lstexisting.SelectedItem)

    Alot easier than I orginally thought.

    Thanks for Your help

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    208

    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
  •  



Click Here to Expand Forum to Full Width