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. Programmatically Select item in ComboBox where ValueMember is known

Programmatically Select item in ComboBox where ValueMember is known

Scheduled Pinned Locked Moved Visual Basic
tutorial
8 Posts 2 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.
  • M Offline
    M Offline
    mincefish
    wrote on last edited by
    #1

    Hello, I've got a form that I create to show certain information about a specfied ClientID. I also load a combo box of all the Clients, so that the user can also pick up (and use) information that is currently associated with other clients. When I load the combo box of all Clients, I also want to set the Combo box to show the specified client, for continuity. I can't seem to work out how to do this! A brief overview of how I load the combo box is as follows:

    Dim strProc As String = "usp_ClientSubDetails_LoadControls"         Dim dsClients As New DataSet         Dim drClients As DataRow         Dim iRowCtr As Integer = 0         Dim iSelectionVal As Integer = 0         Dim iLoop As Integer ' to find the client in the combo box 'return dataset with client list in it         If Not fncConnectSelectDataset(strProc, CommandType.StoredProcedure, dsClients, sysSystemData.SchedulerDb) Then             strError = strProc             Return False         End If 'make sure there's records in the table         If dsClients.Tables(0).Rows.Count = 0 Then             strError = "No Rows Returned"             Return False         End If         For Each drClients In dsClients.Tables(0).Rows 'DataLoader is a class that has an ID property and a DisplayValue property             Me.cboClients.Items.Add(New DataLoader(drClients.Item(0), drClients.Item(1)))         Next         Me.cboClients.DisplayMember = "DisplayValue"         Me.cboClients.ValueMember = "ID"

    This gives me a combo box loaded with a bunch of DataLoader objects. By doing CType(cboClients.SelectedItem,DataLoader).ID I can retrieve the ClientID... I just don't know how t

    D 1 Reply Last reply
    0
    • M mincefish

      Hello, I've got a form that I create to show certain information about a specfied ClientID. I also load a combo box of all the Clients, so that the user can also pick up (and use) information that is currently associated with other clients. When I load the combo box of all Clients, I also want to set the Combo box to show the specified client, for continuity. I can't seem to work out how to do this! A brief overview of how I load the combo box is as follows:

      Dim strProc As String = "usp_ClientSubDetails_LoadControls"         Dim dsClients As New DataSet         Dim drClients As DataRow         Dim iRowCtr As Integer = 0         Dim iSelectionVal As Integer = 0         Dim iLoop As Integer ' to find the client in the combo box 'return dataset with client list in it         If Not fncConnectSelectDataset(strProc, CommandType.StoredProcedure, dsClients, sysSystemData.SchedulerDb) Then             strError = strProc             Return False         End If 'make sure there's records in the table         If dsClients.Tables(0).Rows.Count = 0 Then             strError = "No Rows Returned"             Return False         End If         For Each drClients In dsClients.Tables(0).Rows 'DataLoader is a class that has an ID property and a DisplayValue property             Me.cboClients.Items.Add(New DataLoader(drClients.Item(0), drClients.Item(1)))         Next         Me.cboClients.DisplayMember = "DisplayValue"         Me.cboClients.ValueMember = "ID"

      This gives me a combo box loaded with a bunch of DataLoader objects. By doing CType(cboClients.SelectedItem,DataLoader).ID I can retrieve the ClientID... I just don't know how t

      D Offline
      D Offline
      Dinuj Nath
      wrote on last edited by
      #2

      try cboClients.Items.FindByText("TheText").Selected = True Or cboClients.Items.FindByValue("TheValue").Selected = True oops! I think you asked for windows forms. I have answered for asp.net. Sorry. Last modified: 14-June-2006 6:39:52 AM --

      M 1 Reply Last reply
      0
      • D Dinuj Nath

        try cboClients.Items.FindByText("TheText").Selected = True Or cboClients.Items.FindByValue("TheValue").Selected = True oops! I think you asked for windows forms. I have answered for asp.net. Sorry. Last modified: 14-June-2006 6:39:52 AM --

        M Offline
        M Offline
        mincefish
        wrote on last edited by
        #3

        You're correct, it is Windows Forms. I can do cboClients.FindValue, but I don't have the string!

        D 1 Reply Last reply
        0
        • M mincefish

          You're correct, it is Windows Forms. I can do cboClients.FindValue, but I don't have the string!

          D Offline
          D Offline
          Dinuj Nath
          wrote on last edited by
          #4

          Can't you use Overloads Public Function FindStringExact(String) As Integer

          M 1 Reply Last reply
          0
          • D Dinuj Nath

            Can't you use Overloads Public Function FindStringExact(String) As Integer

            M Offline
            M Offline
            mincefish
            wrote on last edited by
            #5

            I may well be being incredibly dense, but I don't know the string; I only know the ID of the Client. So I wouldn't have a String to pass the function, right? Thanks for your help so far! Tom

            D 1 Reply Last reply
            0
            • M mincefish

              I may well be being incredibly dense, but I don't know the string; I only know the ID of the Client. So I wouldn't have a String to pass the function, right? Thanks for your help so far! Tom

              D Offline
              D Offline
              Dinuj Nath
              wrote on last edited by
              #6

              Maybe you could loop through each of the elements in the combobox and set the Item with the client id selected. Something like this...


              Pseudocode


              for each item in combobox.items if item.value = clientid then item.selected = true end if

              M 1 Reply Last reply
              0
              • D Dinuj Nath

                Maybe you could loop through each of the elements in the combobox and set the Item with the client id selected. Something like this...


                Pseudocode


                for each item in combobox.items if item.value = clientid then item.selected = true end if

                M Offline
                M Offline
                mincefish
                wrote on last edited by
                #7

                Thanks Dinuj... Your pseudocode got me there in the end....

                        Dim dl As DataLoader
                
                        For Each dl In cboClients.Items
                            If dl.ID = ClientID Then
                                cboClients.SelectedItem = dl
                            End If
                        Next
                

                Nice one! Give this man a cigar!! Regards, Tom

                D 1 Reply Last reply
                0
                • M mincefish

                  Thanks Dinuj... Your pseudocode got me there in the end....

                          Dim dl As DataLoader
                  
                          For Each dl In cboClients.Items
                              If dl.ID = ClientID Then
                                  cboClients.SelectedItem = dl
                              End If
                          Next
                  

                  Nice one! Give this man a cigar!! Regards, Tom

                  D Offline
                  D Offline
                  Dinuj Nath
                  wrote on last edited by
                  #8

                  mincefish wrote:

                  Nice one! Give this man a cigar!!

                  :) Glad to be of your help.

                  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