Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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,

Dream as if you'll live forever, live as if you'll die today
Posted

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...

Posted

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

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*
Posted
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.
Posted

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

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

Posted

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

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

Posted (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 by samsmithnz
Posted

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

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

Posted

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

  • 4 months later...
Posted

I used this to fill the combo from DataBase:

 

cboTest.DisplayMember = FieldTextName
cboTest.ValueMember = FieldIDName
cboTest.DataSource = myDataTable

 

Thats all !! :D

Mikecrosoft.Net

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...