How to Add Two Different Strings to same Index In a Combobox - .NET1.1
-
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
-
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
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.
-
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
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 StingPublic Overrides Function ToString() As String
return firstString
End Function
End ClassIn 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
-
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
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:
-
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.
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.
-
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.
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
-
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
Hi, I tried with all your suggestions however it is still not working. Please let me know what shall I do? Thanks & regards Puneet
-
Hi, I tried with all your suggestions however it is still not working. Please let me know what shall I do? Thanks & regards Puneet
what is the error message you are getting?
-
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 StingPublic Overrides Function ToString() As String
return firstString
End Function
End ClassIn 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
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
-
what is the error message you are getting?
"Could Not Bind To The New Display Member, Parameter Name: New Display Member"
-
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 StingPublic Overrides Function ToString() As String
return firstString
End Function
End ClassIn 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
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.
-
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.
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 SubPrivate Sub CboProject_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboProject.SelectedIndexChanged
GetProjectID()
End SubPublic 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 SubHope this helps.
-
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.
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 thed = 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