Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Allowing user to add to/edit combobox

Allowing user to add to/edit combobox

Scheduled Pinned Locked Moved Visual Basic
questiondata-structureshelp
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GuyThiebaut
    wrote on last edited by
    #1

    I populate a combobox as follows:

    ComboBox2.Items.Clear()
    ComboBox2.Items.AddRange(ScanHistoryList)
    ComboBox2.SelectedIndex = 0
    

    ScanHistoryList is a string array. I want to be able to allow the user to add to/edit the combobox . How do I do this? Any help much appreciated - I can't find a property to set:^)

    You always pass failure on the way to success.

    C D 2 Replies Last reply
    0
    • G GuyThiebaut

      I populate a combobox as follows:

      ComboBox2.Items.Clear()
      ComboBox2.Items.AddRange(ScanHistoryList)
      ComboBox2.SelectedIndex = 0
      

      ScanHistoryList is a string array. I want to be able to allow the user to add to/edit the combobox . How do I do this? Any help much appreciated - I can't find a property to set:^)

      You always pass failure on the way to success.

      C Offline
      C Offline
      ciacia
      wrote on last edited by
      #2

      I think you need to achive this prgamatilally by traping the events and if the text is changed while drop down event replace the older text with new one and if older text is a blank then add the new Item inthe list.Just try it:-D

      G 1 Reply Last reply
      0
      • C ciacia

        I think you need to achive this prgamatilally by traping the events and if the text is changed while drop down event replace the older text with new one and if older text is a blank then add the new Item inthe list.Just try it:-D

        G Offline
        G Offline
        GuyThiebaut
        wrote on last edited by
        #3

        Thanks I'll give that a go :)

        You always pass failure on the way to success.

        1 Reply Last reply
        0
        • G GuyThiebaut

          I populate a combobox as follows:

          ComboBox2.Items.Clear()
          ComboBox2.Items.AddRange(ScanHistoryList)
          ComboBox2.SelectedIndex = 0
          

          ScanHistoryList is a string array. I want to be able to allow the user to add to/edit the combobox . How do I do this? Any help much appreciated - I can't find a property to set:^)

          You always pass failure on the way to success.

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          Hmmm... I guess you could put a button next to the ComboBox, called Edit, that gets the current SelectedItem in the ComboBox, and put the text of the item into a small form with a TextBox. That form would have a OK and Cancel button. If OK, then the SelectedItem would be replaced with the new text.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007

          G P 2 Replies Last reply
          0
          • D Dave Kreskowiak

            Hmmm... I guess you could put a button next to the ComboBox, called Edit, that gets the current SelectedItem in the ComboBox, and put the text of the item into a small form with a TextBox. That form would have a OK and Cancel button. If OK, then the SelectedItem would be replaced with the new text.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                 2006, 2007

            G Offline
            G Offline
            GuyThiebaut
            wrote on last edited by
            #5

            Thanks Dave and Ciacia, I've solved it. First thing I did was turn it into a DropDown rather than DropDownList. This allows the users to edit the data:doh: Then I created the following event handlers:

                Private Sub ComboBox2_SelectedTextChange(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.TextChanged
            
                    textChanged = True
            
                End Sub
            
                Private Sub ComboBox2_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.Leave
            
                    If textChanged Then
            
                        ComboBox2.Items.Insert(ComboBox2.Items.Count, ComboBox2.Text)
                        ComboBox2.SelectedIndex = ComboBox2.Items.Count - 1
            
                    End If
            
                End Sub
            

            Note: textChanged is declared as a boolean variable. What happens then is that when the user leaves the combobox if they have changed any text: this is added to the end of the combobox array collection and this is then made the current selection. Thanks for your help guys. It's a shame that something which seems so simple can be so tricky!:-D

            You always pass failure on the way to success.

            C 1 Reply Last reply
            0
            • D Dave Kreskowiak

              Hmmm... I guess you could put a button next to the ComboBox, called Edit, that gets the current SelectedItem in the ComboBox, and put the text of the item into a small form with a TextBox. That form would have a OK and Cancel button. If OK, then the SelectedItem would be replaced with the new text.

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                   2006, 2007

              P Offline
              P Offline
              Paul Conrad
              wrote on last edited by
              #6

              I don't see anything wrong with this approach. Quick and easy :-D

              "Any sort of work in VB6 is bound to provide several WTF moments." - Christian Graus

              1 Reply Last reply
              0
              • G GuyThiebaut

                Thanks Dave and Ciacia, I've solved it. First thing I did was turn it into a DropDown rather than DropDownList. This allows the users to edit the data:doh: Then I created the following event handlers:

                    Private Sub ComboBox2_SelectedTextChange(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.TextChanged
                
                        textChanged = True
                
                    End Sub
                
                    Private Sub ComboBox2_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.Leave
                
                        If textChanged Then
                
                            ComboBox2.Items.Insert(ComboBox2.Items.Count, ComboBox2.Text)
                            ComboBox2.SelectedIndex = ComboBox2.Items.Count - 1
                
                        End If
                
                    End Sub
                

                Note: textChanged is declared as a boolean variable. What happens then is that when the user leaves the combobox if they have changed any text: this is added to the end of the combobox array collection and this is then made the current selection. Thanks for your help guys. It's a shame that something which seems so simple can be so tricky!:-D

                You always pass failure on the way to success.

                C Offline
                C Offline
                ciacia
                wrote on last edited by
                #7

                Gr8... Enjoy

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups