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. How to check if user doesn't select anything in combobox.

How to check if user doesn't select anything in combobox.

Scheduled Pinned Locked Moved Visual Basic
databasetutorialannouncement
11 Posts 6 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.
  • H Offline
    H Offline
    hmanhha
    wrote on last edited by
    #1

    The combobox I populated from DB. WHen I add, update DB, this combobox is one field but if user doesn't select anything. The insert/update sql will not correct. So how to detect this situation then warning the user.

    P Richard Andrew x64R A 3 Replies Last reply
    0
    • H hmanhha

      The combobox I populated from DB. WHen I add, update DB, this combobox is one field but if user doesn't select anything. The insert/update sql will not correct. So how to detect this situation then warning the user.

      P Offline
      P Offline
      phil o
      wrote on last edited by
      #2

      If nothing is selected, the SelectedIndex property of the ComboBox instance will equal -1.

      If (combo.SelectedIndex = -1)
      '' Nothing selected
      End If

      "I'm neither for nor against, on the contrary." John Middle

      H 1 Reply Last reply
      0
      • P phil o

        If nothing is selected, the SelectedIndex property of the ComboBox instance will equal -1.

        If (combo.SelectedIndex = -1)
        '' Nothing selected
        End If

        "I'm neither for nor against, on the contrary." John Middle

        H Offline
        H Offline
        hmanhha
        wrote on last edited by
        #3

        I have tried. If no selected (user does not do anything with the combobox) selected.index=0 the same when user select the first row. I use Visual Studio 2013 and populate combobox from database using datatable like this

        Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)
        Dim myDataSet As DataSet = New DataSet()
        myDA.Fill(myDataSet, "MyTable")
        Cbx_PhoneType.DataSource = myDataSet.Tables("MyTable").DefaultView
        Cbx_PhoneType.ValueMember = "ID"
        Cbx_PhoneType.DisplayMember = "Model Name"
        cmd.Dispose()
        con.Close()

        R 1 Reply Last reply
        0
        • H hmanhha

          I have tried. If no selected (user does not do anything with the combobox) selected.index=0 the same when user select the first row. I use Visual Studio 2013 and populate combobox from database using datatable like this

          Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)
          Dim myDataSet As DataSet = New DataSet()
          myDA.Fill(myDataSet, "MyTable")
          Cbx_PhoneType.DataSource = myDataSet.Tables("MyTable").DefaultView
          Cbx_PhoneType.ValueMember = "ID"
          Cbx_PhoneType.DisplayMember = "Model Name"
          cmd.Dispose()
          con.Close()

          R Offline
          R Offline
          Ralf Meier
          wrote on last edited by
          #4

          You could do the Following : After populating the Combobox with new data you set an additional boolean Variable (ComboBoxHelper) to false. If the Combobox is Clicked or SelectedIndexChanged you set this Variable to True. Now you know, that the Combobox was used by the Operator ...

          H 1 Reply Last reply
          0
          • R Ralf Meier

            You could do the Following : After populating the Combobox with new data you set an additional boolean Variable (ComboBoxHelper) to false. If the Combobox is Clicked or SelectedIndexChanged you set this Variable to True. Now you know, that the Combobox was used by the Operator ...

            H Offline
            H Offline
            hmanhha
            wrote on last edited by
            #5

            Do you have skype. I want to contact with you for detail solution.

            R 1 Reply Last reply
            0
            • H hmanhha

              Do you have skype. I want to contact with you for detail solution.

              R Offline
              R Offline
              Ralf Meier
              wrote on last edited by
              #6

              No ... sorry ... but you could reply here and also we could discuss anything ...

              H 1 Reply Last reply
              0
              • R Ralf Meier

                No ... sorry ... but you could reply here and also we could discuss anything ...

                H Offline
                H Offline
                hmanhha
                wrote on last edited by
                #7

                I have tried your suggestion but I fail. Do you have any OTT application such as Viber, Whatsapp, line...... We can contact easier. Thank for your help.

                D R 2 Replies Last reply
                0
                • H hmanhha

                  I have tried your suggestion but I fail. Do you have any OTT application such as Viber, Whatsapp, line...... We can contact easier. Thank for your help.

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

                  It either happens here on Code Project or it doesn't happen at all.

                  Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                  Dave Kreskowiak

                  1 Reply Last reply
                  0
                  • H hmanhha

                    I have tried your suggestion but I fail. Do you have any OTT application such as Viber, Whatsapp, line...... We can contact easier. Thank for your help.

                    R Offline
                    R Offline
                    Ralf Meier
                    wrote on last edited by
                    #9

                    I think you read what Dave has written. My answer is the same - sorry ... Back to your Requirement : You have some ComboBoxes, ListBoxes and/or TextBoxes on your Form. All of them are assigned with some values. Your problem now (as I understood) is that the User has made some inputs but sometimes he forgot (or not realize) to make a selection on the Combobox. By default your Combobox has not SelectedItem= -1 - it has SelectedItem=0 (and this could be a valid selection). So ... what could you do now ? One possibility is to force the user to make a selection or to touch the Combobox. The Suggestion I gave you is to touch the Combobox. But perhaps that doesn't matches to your Requirement (if you coded I in the right way it will work). So we could change something : the content of your Combobox don't have a valid data on Item=0. This could be done if you first insert a "-" as Item and then the correct data. Perhaps you think about that ...

                    1 Reply Last reply
                    0
                    • H hmanhha

                      The combobox I populated from DB. WHen I add, update DB, this combobox is one field but if user doesn't select anything. The insert/update sql will not correct. So how to detect this situation then warning the user.

                      Richard Andrew x64R Offline
                      Richard Andrew x64R Offline
                      Richard Andrew x64
                      wrote on last edited by
                      #10

                      Right after you populate the combo box, set the SelectedIndex property to -1. Then it will be -1 if the user hasn't changed it.

                      The difficult we do right away... ...the impossible takes slightly longer.

                      1 Reply Last reply
                      0
                      • H hmanhha

                        The combobox I populated from DB. WHen I add, update DB, this combobox is one field but if user doesn't select anything. The insert/update sql will not correct. So how to detect this situation then warning the user.

                        A Offline
                        A Offline
                        Alan Burkhart
                        wrote on last edited by
                        #11

                        If by chance you're using Windows Forms, the combobox has an event that fires only when the user makes a selection. It's independent of the SelectedIndexchanged event. Handle the event and get the SelectedIndex property from there. ComboBox.SelectionChangeCommitted[^]

                        Sometimes the true reward for completing a task is not the money, but instead the satisfaction of a job well done. But it's usually the money.

                        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