Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combo box Itemdata equivalent- wits end 1

Status
Not open for further replies.

Kalor

Programmer
Jan 22, 2002
13
AU
Hi all,

I'm now getting back into vb .net after a few months doing vb6. What I want to do is the usual combo box thing- description with a hidden ID, then select the ID later.
I've read zillions of FAQs and they all say the same thing: "Itemdata is now replaced by an object! Isn't it great?" but never give me a simple example of how to do it (do you really have to write > 50 lines of code just for this??)

The clincher: Back when I did some vb .net I did a similar thing quite easily, but can't get it to work now. I populated the combo box with a dataset by:

Me.cboMeal.DataSource = dsCombo.Tables(0)
Me.cboMeal.DisplayMember = "DescField"
Me.cboMeal.ValueMember = "IDField"


Then retrieved the selected ID with
Me.cboMeal.SelectedValue

How come this worked a few months ago in another job, but won't work now?? What I get now is "Cast from type 'DataRowView' to type 'String' is not valid". If it can't resolve the selectedvalue property, what they @#$#! is it there for?

 
Kalor

Unfortunately the base combobox is very limited.

You are well advised to create your own combo box control, subclassed from the base combobox. The main thing you need to do is also to subclass the Listitem, so you can have an Integer property for your hidden ID, and a Text Displayed Field. Once you have done this though, you can reuse your own combo box. I've done quite a bit of work on this, to allow the combo to display more than 1 column, by overriding the draw event, and to allow incremental searching

Here is what I do in my combobox class. As well as an ID and a Text Display, I also have place holders for up to 3 other fields that can store relevant informtion

Code:
Private Class oListItem

Inherits ListViewItem

Public iValue As Integer
Public sText As String
Public oObject1 As Object
Public oObject2 As Object
Public oObject3 As Object

Sub New(ByVal iValue As Integer, ByVal sText As String, _
    Optional ByVal oObject1 As Object = Nothing, _
    Optional ByVal oObject2 As Object = Nothing, _
    Optional ByVal oObject3 As Object = Nothing)

    MyBase.New()
    Me.iValue = iValue
    Me.sText = sText
    Me.oObject1 = oObject1
    Me.oObject2 = oObject2
    Me.oObject3 = oObject3

End Sub

End Class

Then you need to build a custom fill method in your combo. I did this by raising an event called Filllist, when the combo drop down occurs. Instead of adding the base listitem, add your subclassed oListitem.

Code:
dim oItem as new olistitem(id,"Text Displayed")
combo1.items.add(olistitem)





Sweep
...if it works dont mess with it
 
See thread796-656160

Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Thanks guys. I imagine it'll feel normal after a while :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top