Results 1 to 4 of 4

Thread: Find Combo Item By ID and Reset SelectedIndex

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Find Combo Item By ID and Reset SelectedIndex

    Hello Folks i want to get ID of the combobox item and then reset the selected index of that combobox to that item. I populate my Combobox with a list of 56 Districts from an array so, the ID is hidden.
    When i call the sub below which calls FindComboItemByID it gives me the error below, so the sub FindComboItemByID has some problem that i seem not to figure out...is there a better way of doing this?
    VB Code:
    1. Sub getDistValueByID()
    2.        Dim strSQL As String = "SELECT DistID FROM tblDistricts WHERE(District='" & Me.cbDistrict.Text & "')"
    3.         Dim strID As String = connMger.LookUp(strSQL)
    4.         FindComboItemByID(Me.cbDistrict, strID)
    5.     End Sub
    VB Code:
    1. Public Sub FindComboItemByID(ByVal cbName As ComboBox, ByVal strID As String)
    2.         ' This sub is used to find an Item in a combobox/listbox and
    3.         ' set the selected index of the combo box/listbox to that item
    4.         'On Error Resume Next
    5.         Dim bFound As Boolean
    6.         Dim itemCount As Integer
    7.         Dim lstItem As New ListItem
    8.         itemCount = 0
    9.         While Not bFound Or itemCount <= cbName.Items.Count - 1
    10.             lstItem = CType(cbName.Items(itemCount), ListItem)
    11.             If lstItem.ID = CInt(strID) Then
    12.                 cbName.SelectedIndex = itemCount
    13.                 bFound = True
    14.             End If
    15.             itemCount += 1
    16.         End While
    17.         If Not bFound Then
    18.             cbName.SelectedIndex = -1
    19.         End If
    20.     End Sub
    Attached Images Attached Images  

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

    Re: Find Combo Item By ID and Reset SelectedIndex

    Use data-binding:
    VB Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         Dim arr(5) As District
    5.  
    6.         For i As Integer = 0 To arr.GetUpperBound(0) Step 1
    7.             arr(i).ID = i
    8.             arr(i).Name = "District " & i
    9.         Next
    10.  
    11.         Me.ComboBox1.DisplayMember = "Name"
    12.         Me.ComboBox1.ValueMember = "ID"
    13.         Me.ComboBox1.DataSource = arr
    14.  
    15.         'Select the District object with an ID of 3.
    16.         Me.ComboBox1.SelectedValue = 3
    17.     End Sub
    18.  
    19. End Class
    20.  
    21. Public Structure District
    22.  
    23.     Private _id As Integer
    24.     Private _name As String
    25.  
    26.     Public Property ID() As Integer
    27.         Get
    28.             Return Me._id
    29.         End Get
    30.         Set(ByVal value As Integer)
    31.             Me._id = value
    32.         End Set
    33.     End Property
    34.  
    35.     Public Property Name() As String
    36.         Get
    37.             Return Me._name
    38.         End Get
    39.         Set(ByVal value As String)
    40.             Me._name = value
    41.         End Set
    42.     End Property
    43.  
    44. End Structure
    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

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: Find Combo Item By ID and Reset SelectedIndex

    Thanks JMC...the problem is that the number of Districts is not static, it increases and decreases with time.At one time it can be 60 and at another time it can be 56 Districts.
    And i want to find the combo item using the District text that is displayed in the combo.(I know this sounds contradictory to the thread title)
    This is how i populate my District Combobox, DistID is hidden and District is shown.
    VB Code:
    1. Sub PopulateDistrictCombo()
    2.         Dim strSQL As String = "SELECT DistID,District FROM tblDistricts ORDER BY District ASC"
    3.         Me.cbDistrict.Items.Clear()
    4.         PopulateColumnCombo(Me.cbDistrict, strSQL, "District", "DistID", "tblDistricts")
    5.     End Sub

    I see this is what your code does.
    but after populating the combobox, i populate the combobox with a District within
    a record i want to update. So, i would like to use the "District Text" to basically bring up the District with the same name
    from the district items contained in the combobox.This will inturn bring up the hidden ID with the district and i would use it
    to populate another combobox in the SelectedIndexChanged of the district combobox

    Hope you get what i mean.
    Last edited by maps; Jul 6th, 2006 at 04:50 AM.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: Find Combo Item By ID and Reset SelectedIndex

    **Bump**

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