ItemData equivalent in C#

P

Paul Brownjohn

I have only just started writing C# after years of programming in C and VB
3, 4, 5 & 6.

I'm struggling a bit to find a construct that is equivalent to using
itemdata in VB to store a numeric (usually DB ID) field alongside each
displayed value.

The help files are particularly obtuse on this subject. They say that
ItemData does not exist as such...but not, "so you should now do the same
thing like this:......"; which is what I reckon most people want to see!

Am I right in saying that if you want to do this you create some kind of
object (a struct or class) that contains both the string display value and
the numeric (int) value and do a .ADD on that which enables you to access
the int via the selected row in the combo.

Why the hell MS couldn't provide a LOT more examples instead of the turgid
text in the help files I do not understand.

Can any one explain this in a way that a normal human being can grasp
quickly and easily..it can't be that complex!!

Better yet has anyone out there got some sample code of this concept in
action

Cheers

Paul BJ
 
C

codymanix

Iam not sure what you're talking about but I guess you want to assign data
to Controls/Items where the Tag-property could be relavant for you.
 
P

Paul Brownjohn

The ItemData property in VB is a long integer (32 bit) value that is
associated with each visible item in the dropdown list and is often used to
store the ID field from the database table from which the visible data has
been retrieved.

The ItemData property acts, as its name implies, at the item or row level in
a combo or list box. The tag property, in VB 6 at least, acts at the control
level which would not be of any use in this case.

Cheers

Paul BJ
 
G

Guest

Yes.. the ComboBox control basically displays the return value of the .ToString() member of each object.
For instance, if you store an 'int' in the ComboBox, it will get the display value by calling int.ToString().

So you can try the following:

// good encapsulation omitted for brevity
struct ComboItem
{ public ComboItem(string display, object data) { Display=display; Data=data; }
public override string ToString() { return Display; }
string Display;
object Data;
}

Add ComboItem objects to the ComboBox. Depending on the semantics you want, you may want ComboItem to be a class instead of a struct, but that's the basic idea.
 
C

codymanix

The ItemData property in VB is a long integer (32 bit) value that is
associated with each visible item in the dropdown list and is often used to
store the ID field from the database table from which the visible data has
been retrieved.

The ItemData property acts, as its name implies, at the item or row level in
a combo or list box. The tag property, in VB 6 at least, acts at the control
level which would not be of any use in this case.

You can Use a ListView. Each Listviewitem has a Tag-property. Comboboxes or
ListBoxes don't have a tag for its items.
But you can add each object you want as an item to a combobox or listbox.
Override the ToString()-method of this object to get it properly displayed:

class Test
{
public string Name;
public int ID;
public override string ToString(){return Name;}
}

ComboBox cob = new ComboBox();

cob.Items.Add(new Test());

Test t = (Test)cob.Items[0];
 
P

Paul Brownjohn

This is not an obscure concept that hardly anyone uses...it is something
that absolutely EVERY VB programmer uses at some stage or other, usually
very, very, very often...in every app at least 5 - 10 times often much more,
so what you are saying, and this is what I understood from the documentation
as well, is that in order to acheive tha same thing using .NET technology, I
have to create a class and use it to override the ToString method...etc,
etc, etc a total of around 9 lines of code to do what I could I could do in
2 lines in VB......and this is progress....amazing!

I despair of this industry sometimes!

Cheers

Paul BJ



codymanix said:
The ItemData property in VB is a long integer (32 bit) value that is
associated with each visible item in the dropdown list and is often used to
store the ID field from the database table from which the visible data has
been retrieved.

The ItemData property acts, as its name implies, at the item or row
level
in
a combo or list box. The tag property, in VB 6 at least, acts at the control
level which would not be of any use in this case.

You can Use a ListView. Each Listviewitem has a Tag-property. Comboboxes or
ListBoxes don't have a tag for its items.
But you can add each object you want as an item to a combobox or listbox.
Override the ToString()-method of this object to get it properly displayed:

class Test
{
public string Name;
public int ID;
public override string ToString(){return Name;}
}

ComboBox cob = new ComboBox();

cob.Items.Add(new Test());

Test t = (Test)cob.Items[0];

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
 
F

Frank Oquendo

Paul said:
I despair of this industry sometimes!

You're missing a very big picture, Paul. ItemData is not necessary at all in
..NET since you can add any type of object and get back the *entire* object
when you check for selected items.

If all you're doing is indexing into a table, consider binding the ComboBox
directly to a data source such as a DataTable or DataView. Three little
properties and your ComboBox will populate itself.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
P

Paul Brownjohn

Frank...you are right I am missing a very big picture....any programmer who
has worked with preceeding technologies is going to be looking for the same
sorts of concepts as he/she has worked with in the language(s) that he/she
is used to. No surprise there!

The problem is that the people who have written the help files are incapable
of communicating the changes that have occured in those concepts to the
average reader of their help files (I suspect because they already know too
much about the new system to be able to relate to the ignorance of those who
have only just started using it). The hilarious part of it is that there is
a backwards compatability library available for Visual Basic that
specifically contains the ItemData concept presumably because someone at MS
thought this was going to cause trouble....but along with the backward
compatability blurb is the advice "do not use this for new projects".....but
what is NOT there is: - "because with new projects you should be using this
construct : long explanation and examples".

There is the problem!

I can't help thinking that having to create a class or structure etc, etc,
etc is a very complex way to do a very easy job. OO who needs it?

Thanks

Paul BJ
 
F

Frank Oquendo

Paul said:
I can't help thinking that having to create a class or structure etc,
etc, etc is a very complex way to do a very easy job. OO who needs it?

Let's back this up a bit. How exactly are you obtaining the string and ID
numbers you want to populate your ComboBox with?

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
P

Paul Brownjohn

from a query to a DB....yes I could bind...



Frank Oquendo said:
Let's back this up a bit. How exactly are you obtaining the string and ID
numbers you want to populate your ComboBox with?

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
F

Frank Oquendo

Paul said:
from a query to a DB....yes I could bind...

There you have it. No ItemData necessary. Set the DataSource property to a
compatible data source. Next, set the DisplayMember property to the name of
the field containing the text to show in the list. Finally, the ValueMember
property should be to set to your key field.

I know you think this is more complex but since you'd have to perform the
query either way, the only thing left to compare is how much work it takes
to populate the ComboBox with your query and how easy it is to get the
selected key value.

In .NET, three properties take care of the populating where as VB6 would
have required to iterate your result set and manually enter the text and
ItemData. As for retrieval, myComboBox.SelectedValue is all it takes to get
the key value.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

C# Equivalent of C++ statement 1
Combobox ItemData ? 2
ItemData 4
C++ Reference equivalent in C# 4
ItemData 4
C++ equivalent? 4
ItemData equivalent .NET 5
Combo Box : ItemData 1

Top