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 Add Two Different Strings to same Index In a Combobox - .NET1.1

How to Add Two Different Strings to same Index In a Combobox - .NET1.1

Scheduled Pinned Locked Moved Visual Basic
tutorialcsharpdatabase
16 Posts 7 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.
  • P Puneet Bhatnagar

    Please let me know the property with which I can accomplish the above mentioned task. For more clearity here is an example: Suppose I have a combobox with name- cboProject and my Two strings are - Puneet, India DesiredTask: I want to add both these strings to 0th Index of cboProject and at the same point of time hide the text followed by "," i.e hide India. Thanks & Regards Puneet

    C Offline
    C Offline
    ChandraRam
    wrote on last edited by
    #4

    Puneet Bhatnagar wrote:

    DesiredTask: I want to add both these strings to 0th Index of cboProject and at the same point of time hide the text followed by "," i.e hide India.

    You would have to create a multi-column combo box for this. A simple search brings me to this[^] article on CP.

    1 Reply Last reply
    0
    • P Puneet Bhatnagar

      Please let me know the property with which I can accomplish the above mentioned task. For more clearity here is an example: Suppose I have a combobox with name- cboProject and my Two strings are - Puneet, India DesiredTask: I want to add both these strings to 0th Index of cboProject and at the same point of time hide the text followed by "," i.e hide India. Thanks & Regards Puneet

      T Offline
      T Offline
      Tom Deketelaere
      wrote on last edited by
      #5

      If you only need two columns and one will always be hidden you could try the following: create a datatable with 2 columns fill the datatable with the data you want assing the datatable to the cboproject.datasource property assing the cboproject.valuemember property to the columnname from the column to be hidden assing the cboproject.displaymember to the columnname from the column to be shown The multi column combobox mentioned by ChandraRam could give you more options tho.

      P 1 Reply Last reply
      0
      • P Puneet Bhatnagar

        Please let me know the property with which I can accomplish the above mentioned task. For more clearity here is an example: Suppose I have a combobox with name- cboProject and my Two strings are - Puneet, India DesiredTask: I want to add both these strings to 0th Index of cboProject and at the same point of time hide the text followed by "," i.e hide India. Thanks & Regards Puneet

        R Offline
        R Offline
        riced
        wrote on last edited by
        #6

        If you use string3 = string1 + string2 you won't be able to hide the second string. You need to put the strings into an object that has two fields and add this object to the ComboBox. You could then use the DisplayMember property to show only the first field. Alternatively, you could override the ToString() function to display only the first field. Something like the following should do it.

        Public Class TheData
        'Public for simplicity but should be properties
        Public firstString As String
        Public secondString As Sting

        Public Overrides Function ToString() As String
        return firstString
        End Function
        End Class

        In your code to populate the combobox you would need something like this.

        Dim d As TheData = new TheData()
        d.firstString = "Puneet"
        d.secondString = "India"
        me.cboProject.Items.Insert(0, d) ' or me.cboProject.Items.Add(d)

        There might be an easier way but I can't think of one (it's a breeze in MS Access because the combobox control has multiple columns that you can show or hide.) David Rice

        J P 2 Replies Last reply
        0
        • P Puneet Bhatnagar

          Please let me know the property with which I can accomplish the above mentioned task. For more clearity here is an example: Suppose I have a combobox with name- cboProject and my Two strings are - Puneet, India DesiredTask: I want to add both these strings to 0th Index of cboProject and at the same point of time hide the text followed by "," i.e hide India. Thanks & Regards Puneet

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #7

          Hi, just like a ListBox, a ComboBox can be made to contain objects, not just strings; when using the OwnerDrawn DrawMode you can make it show one string, while its items actually are objects of your design, holding anything you want, e.g. two strings. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          Fixturized forever. :confused:


          1 Reply Last reply
          0
          • T Tom Deketelaere

            If you only need two columns and one will always be hidden you could try the following: create a datatable with 2 columns fill the datatable with the data you want assing the datatable to the cboproject.datasource property assing the cboproject.valuemember property to the columnname from the column to be hidden assing the cboproject.displaymember to the columnname from the column to be shown The multi column combobox mentioned by ChandraRam could give you more options tho.

            P Offline
            P Offline
            Puneet Bhatnagar
            wrote on last edited by
            #8

            Hi, Thanks for your help and time. However still I am facing some problem. Please find below my code. Try Dim sql As String Dim DB2conn As DB2Connection Dim ds As New DataSet DB2conn = New DB2Connection("server=" & ServerAddress & ";database= " & sDBName & ";Connect Timeout=30;user Id=" & UName & ";password =" & pwd &) Dim DB2Cmd As New DB2Command sql = "Select * from ProjectMaster" DB2conn.Open() Dim DB2DA As New DB2DataAdapter(sql, DB2conn) DB2Cmd = New DB2Command(sql, DB2conn, DB2trans) DB2DA.Fill(ds) ComboBox2.DataSource = ds.Tables(0) ComboBox2.DisplayMember = "ProjectMaster.ReleaseNumber" Me.ComboBox2.ValueMember = "ProjectMaster.ProjectID" Catch ex As Exception MessageBox.Show(ex.Message + " " + ex.Source) Finally DB2conn.Close() End Try The code is breaking at line (ComboBox2.ValueMember = "ProjectMaster.ProjectID"), in bold in code snippet. Please let me know where I am going wrong. Again Thanks for your efforts.

            T 1 Reply Last reply
            0
            • P Puneet Bhatnagar

              Hi, Thanks for your help and time. However still I am facing some problem. Please find below my code. Try Dim sql As String Dim DB2conn As DB2Connection Dim ds As New DataSet DB2conn = New DB2Connection("server=" & ServerAddress & ";database= " & sDBName & ";Connect Timeout=30;user Id=" & UName & ";password =" & pwd &) Dim DB2Cmd As New DB2Command sql = "Select * from ProjectMaster" DB2conn.Open() Dim DB2DA As New DB2DataAdapter(sql, DB2conn) DB2Cmd = New DB2Command(sql, DB2conn, DB2trans) DB2DA.Fill(ds) ComboBox2.DataSource = ds.Tables(0) ComboBox2.DisplayMember = "ProjectMaster.ReleaseNumber" Me.ComboBox2.ValueMember = "ProjectMaster.ProjectID" Catch ex As Exception MessageBox.Show(ex.Message + " " + ex.Source) Finally DB2conn.Close() End Try The code is breaking at line (ComboBox2.ValueMember = "ProjectMaster.ProjectID"), in bold in code snippet. Please let me know where I am going wrong. Again Thanks for your efforts.

              T Offline
              T Offline
              Tom Deketelaere
              wrote on last edited by
              #9

              First a Select * isn't adviced (but is not the cause of the error) You don't need to use the tablename before the columname I'm not shure but I think that is what causing the problem (the combobox can't find that column) so try changing the line to : me.combobox2.displaymember = "ReleaseNumber" me.combobox2.valuemember = "ProjectID" If that doesn't help make shure the datatable has these 2 columns (debug and visualy confirm it) with the correct names

              P 1 Reply Last reply
              0
              • T Tom Deketelaere

                First a Select * isn't adviced (but is not the cause of the error) You don't need to use the tablename before the columname I'm not shure but I think that is what causing the problem (the combobox can't find that column) so try changing the line to : me.combobox2.displaymember = "ReleaseNumber" me.combobox2.valuemember = "ProjectID" If that doesn't help make shure the datatable has these 2 columns (debug and visualy confirm it) with the correct names

                P Offline
                P Offline
                Puneet Bhatnagar
                wrote on last edited by
                #10

                Hi, I tried with all your suggestions however it is still not working. Please let me know what shall I do? Thanks & regards Puneet

                T 1 Reply Last reply
                0
                • P Puneet Bhatnagar

                  Hi, I tried with all your suggestions however it is still not working. Please let me know what shall I do? Thanks & regards Puneet

                  T Offline
                  T Offline
                  Tom Deketelaere
                  wrote on last edited by
                  #11

                  what is the error message you are getting?

                  P 1 Reply Last reply
                  0
                  • R riced

                    If you use string3 = string1 + string2 you won't be able to hide the second string. You need to put the strings into an object that has two fields and add this object to the ComboBox. You could then use the DisplayMember property to show only the first field. Alternatively, you could override the ToString() function to display only the first field. Something like the following should do it.

                    Public Class TheData
                    'Public for simplicity but should be properties
                    Public firstString As String
                    Public secondString As Sting

                    Public Overrides Function ToString() As String
                    return firstString
                    End Function
                    End Class

                    In your code to populate the combobox you would need something like this.

                    Dim d As TheData = new TheData()
                    d.firstString = "Puneet"
                    d.secondString = "India"
                    me.cboProject.Items.Insert(0, d) ' or me.cboProject.Items.Add(d)

                    There might be an easier way but I can't think of one (it's a breeze in MS Access because the combobox control has multiple columns that you can show or hide.) David Rice

                    J Offline
                    J Offline
                    Jon_Boy
                    wrote on last edited by
                    #12

                    This is the right approach. With combos, listboxes, ,etc etc etc...you can add an object. It doesn't matter what the object is. You can add properties out the whazzoo for whatever you want and just use a display member like riced suggested above. Create a custom KeyValuePair class to suit your needs.

                    Any suggestions, ideas, or 'constructive criticism' are always welcome. "There's no such thing as a stupid question, only stupid people." - Mr. Garrison

                    1 Reply Last reply
                    0
                    • T Tom Deketelaere

                      what is the error message you are getting?

                      P Offline
                      P Offline
                      Puneet Bhatnagar
                      wrote on last edited by
                      #13

                      "Could Not Bind To The New Display Member, Parameter Name: New Display Member"

                      1 Reply Last reply
                      0
                      • R riced

                        If you use string3 = string1 + string2 you won't be able to hide the second string. You need to put the strings into an object that has two fields and add this object to the ComboBox. You could then use the DisplayMember property to show only the first field. Alternatively, you could override the ToString() function to display only the first field. Something like the following should do it.

                        Public Class TheData
                        'Public for simplicity but should be properties
                        Public firstString As String
                        Public secondString As Sting

                        Public Overrides Function ToString() As String
                        return firstString
                        End Function
                        End Class

                        In your code to populate the combobox you would need something like this.

                        Dim d As TheData = new TheData()
                        d.firstString = "Puneet"
                        d.secondString = "India"
                        me.cboProject.Items.Insert(0, d) ' or me.cboProject.Items.Add(d)

                        There might be an easier way but I can't think of one (it's a breeze in MS Access because the combobox control has multiple columns that you can show or hide.) David Rice

                        P Offline
                        P Offline
                        Puneet Bhatnagar
                        wrote on last edited by
                        #14

                        Thanks for your efforts and time. I tried with your code and it is working fine, Combo box is displaying only the FirstString. However to retrieve records from combobox I have writen this code Private Sub CboProject_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CboProject.SelectedIndexChanged GetProjectID(Me.CboProject.SelectedIndex) End Sub Public Sub GetProjectID(ByVal iIndex As Int32) Dim d1 As TheData = New TheData d1 = Me.CboProject.Items.Item(iIndex) RichTextBox1.Text = d1.SecondString.ToString() RichTextBox1.Text = RichTextBox1.Text & " " & d1.FirstString.ToString() End Sub Problem Description : Irrespective of the iIndex value FirstString and SecondString always are returnig the last record.

                        R 2 Replies Last reply
                        0
                        • P Puneet Bhatnagar

                          Thanks for your efforts and time. I tried with your code and it is working fine, Combo box is displaying only the FirstString. However to retrieve records from combobox I have writen this code Private Sub CboProject_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CboProject.SelectedIndexChanged GetProjectID(Me.CboProject.SelectedIndex) End Sub Public Sub GetProjectID(ByVal iIndex As Int32) Dim d1 As TheData = New TheData d1 = Me.CboProject.Items.Item(iIndex) RichTextBox1.Text = d1.SecondString.ToString() RichTextBox1.Text = RichTextBox1.Text & " " & d1.FirstString.ToString() End Sub Problem Description : Irrespective of the iIndex value FirstString and SecondString always are returnig the last record.

                          R Offline
                          R Offline
                          riced
                          wrote on last edited by
                          #15

                          Not sure why that is happening. I used the following to test it. The only difference is that I'm getting a new object each time. You might also look at my versions of the other to subs - they don't need the index and I make an explicit cast to type TheData.

                          Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                          Dim d As TheData = New TheData()
                          Me.cboProject.BeginUpdate()
                          d.firstString = "Duke"
                          d.secondString = "USA"
                          Me.cboProject.Items.Insert(0, d) ' or me.cboProject.Items.Add(d)

                          d = New TheData()
                          d.firstString = "Alice"
                          d.secondString = "England"
                          Me.cboProject.Items.Insert(0, d) ' or me.cboProject.Items.Add(d)

                          d = New TheData()
                          d.firstString = "Bob"
                          d.secondString = "Australia"
                          Me.cboProject.Items.Insert(0, d) ' or me.cboProject.Items.Add(d)

                          d = New TheData()
                          d.firstString = "Duncan"
                          d.secondString = "Scotland"
                          Me.cboProject.Items.Insert(0, d) ' or me.cboProject.Items.Add(d)

                          d = New TheData()
                          d.firstString = "Puneet"
                          d.secondString = "India"
                          Me.cboProject.Items.Insert(0, d) ' or me.cboProject.Items.Add(d)

                          Me.cboProject.EndUpdate()
                          End Sub

                          Private Sub CboProject_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboProject.SelectedIndexChanged
                          GetProjectID()
                          End Sub

                          Public Sub GetProjectID()
                          Dim d1 As TheData = New TheData()
                          d1 = CType(Me.cboProject.SelectedItem, TheData)
                          RichTextBox1.Text = d1.secondString
                          RichTextBox1.Text = RichTextBox1.Text & " " & d1.firstString
                          End Sub

                          Hope this helps.

                          1 Reply Last reply
                          0
                          • P Puneet Bhatnagar

                            Thanks for your efforts and time. I tried with your code and it is working fine, Combo box is displaying only the FirstString. However to retrieve records from combobox I have writen this code Private Sub CboProject_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CboProject.SelectedIndexChanged GetProjectID(Me.CboProject.SelectedIndex) End Sub Public Sub GetProjectID(ByVal iIndex As Int32) Dim d1 As TheData = New TheData d1 = Me.CboProject.Items.Item(iIndex) RichTextBox1.Text = d1.SecondString.ToString() RichTextBox1.Text = RichTextBox1.Text & " " & d1.FirstString.ToString() End Sub Problem Description : Irrespective of the iIndex value FirstString and SecondString always are returnig the last record.

                            R Offline
                            R Offline
                            riced
                            wrote on last edited by
                            #16

                            I put my thinking head on and came up with the following reason for this seemingly odd behaviour. Comboboxes (and Listboxes) maintain two collections. One is the Items collection, the other a collection of strings that will be displayed by the list box part of the control. When you insert (or add) an object two things happen. First a reference to the object is placed in the Items collection. Secondly, the object's ToString() method is called and the result placed in the hidden strings collection. This is what is displayed in the list. With the code I used the d = New TheData() creates five objects on the heap and references to these objects are placed in Items collection. When the d = New TheData() lines are commented out, only one object is created on the heap (when d is Dimmed). So all of the references in Items collection point to the same object. This object will have the values last inserted into the list. The selection methods of the combobox operate on the Items collection so they will return what appears to be the last item inserted for all positions. Because of the two collections, the displayed list can be quite different to what is actually in the Items collection. In the SelectedItemsChanged handler you could update the fields in the item, e.g. by setting secondString to "Impossible", but this would not change the displayed list. As far as I'm aware you cannot get direct access to the strings collection. The Combobox control maintains this collection when you add, insert or remove items. Note that the Listbox control behaves in exactly the same way. Regards David Rice

                            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