samsmithnz Posted September 18, 2003 Posted September 18, 2003 What happened to the combo ItemData in VB.NET?? I used to use it to store item keys to reference with (instead of the text). Quote Thanks Sam http://www.samsmith.co.nz
Mehyar Posted September 18, 2003 Posted September 18, 2003 Well, i do not know VB, I started directly .NET but from what i can understand from your question you can use Combo.ValueMember For ex: You have a table of two fields Id and description, you want description to appear in the combo, while id will be used to reference (as key). Then you would do Combo.datasource = the table Combo.DisplayMember = "Description" Combo.ValueMember = "Id" You can then work with Combo.SelectedValue to get the Id of the selected text in the combo. Hope this helps, Quote Dream as if you'll live forever, live as if you'll die today
samsmithnz Posted September 18, 2003 Author Posted September 18, 2003 But I need a store an item for each value. Heres a typical example (in VB 6): adoRS.open("SELECT CustomerKey, CustomerName FROM CUSTOMER") Do While Not adoRS.eof cboCustomers.addItem adoRS!CustomerName cboCustomers.ItemData(cobCustomers.newindex) = adoRS!CustomerKey Loop So this gets all the customers and puts their names in the combo box, and also adds there Primary Key in too. Then if I want to update later I can do something like: adoConn.Exec "UPDATE [customer stuff] WHERE CustomerKey = " & cboCustomers.ItemData(cboCustomers.index) So i need to store the customer key for each record... Quote Thanks Sam http://www.samsmith.co.nz
ultraman Posted September 18, 2003 Posted September 18, 2003 What I personnaly did to solve the problem was to create my own class cCboItem that containt 2 properties : DataToShow and HiddenData. Instead of adding the text, I create an object, set its properties and add the OBJECT to the combo. Dim objItem = New cCboItem() objItem.DataToShow = CStr(DefaultAllValue).ToString objItem.HiddenData = "0" cboToFill.Items.Add(objItem) When I want to get the selected value, I do this : Dim intMyVal as Integer Dim objItem As New cCboItem() objItem = cboOrganisation.SelectedItem intMyVal = objItem.HiddenData Quote Now go on, boy, and pay attention. Because if you do, someday, you may achieve something that we Simpsons have dreamed about for generations: You may outsmart someone! --Homer Simpson
*Experts* Volte Posted September 18, 2003 *Experts* Posted September 18, 2003 Note that all classes which are added to a combo must Overload the ToString function so that it adds the correct data. Otherwise, will simple add a whole bunch of lines that say "MyProject.CboItem" or something similar. Quote
ultraman Posted September 18, 2003 Posted September 18, 2003 You don't need to Overload the ToString method, since you just add a ComboBox.ObjectCollection in the Items collection. As Microsoft said on there site : "The System.Windows.Forms.ComboBox.ObjectCollection class encapsulates the items in the ComboBox. The object collection of a combo box can be used to manage many types of objects, including strings, images, and custom business objects." http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWindowsFormsComboBoxObjectCollectionClassTopic.asp Quote Now go on, boy, and pay attention. Because if you do, someday, you may achieve something that we Simpsons have dreamed about for generations: You may outsmart someone! --Homer Simpson
ultraman Posted September 18, 2003 Posted September 18, 2003 But just for the record, I overloaded it in my class cCboItem Public Class cCboItem Implements IDisposable Public DataToShow As String Public HiddenData As String Public Overrides Function ToString() As String Return DataToShow End Function Public Sub Dispose() Implements IDisposable.Dispose 'Free whatever you have to End Sub End Class Quote Now go on, boy, and pay attention. Because if you do, someday, you may achieve something that we Simpsons have dreamed about for generations: You may outsmart someone! --Homer Simpson
samsmithnz Posted September 18, 2003 Author Posted September 18, 2003 (edited) Eh? This is what I do now. But as you mentioned, all that I get in the dropbox is [projectname].clsalbum. What do I do again? Do While Not adoRS.EOF objAlbum = New clsAlbum() objAlbum.AlbumName = adoRS.Fields("TheName").Value objAlbum.AlbumKey = adoRS.Fields("AlbumKey").Value With cboAlbums intNewIndex = .Items.Add(objAlbum) .DisplayMember = objAlbum.AlbumName .ValueMember = objAlbum.AlbumKey End With adoRS.MoveNext() Loop Edited September 18, 2003 by samsmithnz Quote Thanks Sam http://www.samsmith.co.nz
ultraman Posted September 19, 2003 Posted September 19, 2003 Did you override the ToString method in the clsAlbum class as VolteFace suggested ? That's the first thing you should check Public Overrides Function ToString() As String Return AlbumName End Function Quote Now go on, boy, and pay attention. Because if you do, someday, you may achieve something that we Simpsons have dreamed about for generations: You may outsmart someone! --Homer Simpson
samsmithnz Posted September 19, 2003 Author Posted September 19, 2003 No I haven't yet, and I don't understand why I need too? I've only just figured out what the overrides function seems to do. Maybe I should post my clsAlbum code too: I'm sorry, but I'm really struggling to understand why a relatively easy operation in VB6 is so much harder in .NET. Public Class clsAlbum Private intAlbumKey As Integer Private strAlbumName As String Public Property AlbumKey() As Integer Get AlbumKey = intAlbumKey End Get Set(ByVal Value As Integer) intAlbumKey = Value End Set End Property Public Property AlbumName() As String Get AlbumName = intAlbumKey End Get Set(ByVal Value As String) intAlbumKey = Value End Set End Property End Class Thanks for your help so far guys, its been fantastic! Sam Quote Thanks Sam http://www.samsmith.co.nz
samsmithnz Posted September 20, 2003 Author Posted September 20, 2003 OK, so I got home from work and added that ToString function to my class, but I still don't understand WHY it works...? Quote Thanks Sam http://www.samsmith.co.nz
Mikecrosoft Posted January 21, 2004 Posted January 21, 2004 I used this to fill the combo from DataBase: cboTest.DisplayMember = FieldTextName cboTest.ValueMember = FieldIDName cboTest.DataSource = myDataTable Thats all !! :D Quote Mikecrosoft.Net
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.