add item to listbox

  • Thread starter Ignacio Martínez
  • Start date
I

Ignacio Martínez

Hey group!
first of all, happy new year to everyone.

I have a small problem that I haven't worked out yet and it's driving me a
little crazy.
I have two forms with a listbox control on each one.

the child form passes the selecteditem to the parent form.
but I want to pass the whole item (Value Member, Display Member) and I have
no clue on how to do this.
what I could do was to pass just the text as a parameter in an event on the
child form.
but that doesn't suit my needs.

I can't use databindings, because I need to do this manually.
is there a way to add the whole item????

thanx in advance!
ignacio martínez
buenos aires, argentina.
 
T

TB

-----Original Message-----
Hey group!
first of all, happy new year to everyone.

I have a small problem that I haven't worked out yet and it's driving me a
little crazy.
I have two forms with a listbox control on each one.

the child form passes the selecteditem to the parent form.
but I want to pass the whole item (Value Member, Display Member) and I have
no clue on how to do this.
what I could do was to pass just the text as a parameter in an event on the
child form.
but that doesn't suit my needs.

I can't use databindings, because I need to do this manually.
is there a way to add the whole item????

thanx in advance!
ignacio martínez
buenos aires, argentina.

Ignacio,

Usually when I need to get more data for a selected item
in a listbox I will set each item's "Tag" property to an
object that contains what I need -- usually an instance of
whatever object I am working with. Given a selected item,
you just cast it's Tag value to what you expect and you're
off!

-- TB
 
I

Ignacio Martínez

ok, so basically I can't manually set the valuemember of a listbox item
(webcontrols are more flexible in this cases...)

thanx!!!!!!!!

-----Original Message-----
Hey group!
first of all, happy new year to everyone.

I have a small problem that I haven't worked out yet and it's driving me a
little crazy.
I have two forms with a listbox control on each one.

the child form passes the selecteditem to the parent form.
but I want to pass the whole item (Value Member, Display Member) and I have
no clue on how to do this.
what I could do was to pass just the text as a parameter in an event on the
child form.
but that doesn't suit my needs.

I can't use databindings, because I need to do this manually.
is there a way to add the whole item????

thanx in advance!
ignacio martínez
buenos aires, argentina.

Ignacio,

Usually when I need to get more data for a selected item
in a listbox I will set each item's "Tag" property to an
object that contains what I need -- usually an instance of
whatever object I am working with. Given a selected item,
you just cast it's Tag value to what you expect and you're
off!

-- TB
 
G

Guest

-----Original Message-----
ok, so basically I can't manually set the valuemember of a listbox item
(webcontrols are more flexible in this cases...)

thanx!!!!!!!!



Ignacio,

Usually when I need to get more data for a selected item
in a listbox I will set each item's "Tag" property to an
object that contains what I need -- usually an instance of
whatever object I am working with. Given a selected item,
you just cast it's Tag value to what you expect and you're
off!

-- TB

Lo siento, Ignacio,

I'm afraid I didn't read your post carefully enough the
first time around. ListBox controls are different from
the ListView controls that I'm more used to. The latter
accept "smarter" items (where each can have a tag). A
listbox does not.

Ok, so let me see if I am understanding your problem.
When the user selects an item in your child form's listbox
you want to pass along both the display member string
*and* value member string for that selected object up to
the parent form.

The value data is, of course, the "SelectedValue" for the
listbox. There is no corresponding "SelectedDisplay", but
you can use "SelectedItem" to get the object you
originally added to the listbox, cast it to the right type
of thing, and then access the property directly. You can
also get the value member this way.

But you're right -- the ListItem Web object allows the
text and value members to be specified but the normal
Forms Listbox just takes untyped Objects for its items and
applies the same DisplayMember and ValueMember properties
to all.

-- TB
 
I

Ignacio Martínez

thanx for the 2nd response
I had tried casting the [Object] Object from the child form to the parent
form, but I only got "System.Listbox....something", instead of the text of
the item I passed.
I think it has something to do with not giving valuemember and displaymember
values (what values could I give? it's not bound)

it's pretty damn hard!
I wish I only had to show the text for the item, but I really need the ID to
continue using that item for other purposes on the parent form.
anyway, thanx
I posted this message on other MS groups but I got no reply so far.
nice weekend!

-----Original Message-----
ok, so basically I can't manually set the valuemember of a listbox item
(webcontrols are more flexible in this cases...)

thanx!!!!!!!!



Ignacio,

Usually when I need to get more data for a selected item
in a listbox I will set each item's "Tag" property to an
object that contains what I need -- usually an instance of
whatever object I am working with. Given a selected item,
you just cast it's Tag value to what you expect and you're
off!

-- TB

Lo siento, Ignacio,

I'm afraid I didn't read your post carefully enough the
first time around. ListBox controls are different from
the ListView controls that I'm more used to. The latter
accept "smarter" items (where each can have a tag). A
listbox does not.

Ok, so let me see if I am understanding your problem.
When the user selects an item in your child form's listbox
you want to pass along both the display member string
*and* value member string for that selected object up to
the parent form.

The value data is, of course, the "SelectedValue" for the
listbox. There is no corresponding "SelectedDisplay", but
you can use "SelectedItem" to get the object you
originally added to the listbox, cast it to the right type
of thing, and then access the property directly. You can
also get the value member this way.

But you're right -- the ListItem Web object allows the
text and value members to be specified but the normal
Forms Listbox just takes untyped Objects for its items and
applies the same DisplayMember and ValueMember properties
to all.

-- TB
 
T

TB

-----Original Message-----
thanx for the 2nd response
I had tried casting the [Object] Object from the child form to the parent
form, but I only got "System.Listbox....something", instead of the text of
the item I passed.
I think it has something to do with not giving valuemember and displaymember
values (what values could I give? it's not bound)

it's pretty damn hard!
I wish I only had to show the text for the item, but I really need the ID to
continue using that item for other purposes on the parent form.
anyway, thanx
I posted this message on other MS groups but I got no reply so far.
nice weekend!

I think you're right, that the DisplayMember and
ValueMember properties are going to be useless for you
since you have not provided a DataSource.

However, the SelectedItem property should return the exact
object that was supplied to the ListBox in the first
place. I.e. if you have your own class:

public class foo
{
public int ID;
public string Text;
}

And populate a ListBox via ListBox.Items.Add(aFoo) then
any SelectedItem object should be castable back into a foo
and the fields, methods, properties, etc. accessible
normally.

-- TB
 
D

Doug Forster

Hi Ignacio,
I think it has something to do with not giving valuemember and displaymember
values (what values could I give? it's not bound)

You can bind a ListBox (via DataSource) to *any* object that implements the
IList interface. This includes Array or ArrayList. You just need to fill one
of these with suitable objects with a property for the string to display and
another for the data you need and set the DisplayMember and ValueMember
properties of the ListBox.

Cheers

Doug Forster

Ignacio Martínez said:
thanx for the 2nd response
I had tried casting the [Object] Object from the child form to the parent
form, but I only got "System.Listbox....something", instead of the text of
the item I passed.
I think it has something to do with not giving valuemember and displaymember
values (what values could I give? it's not bound)

it's pretty damn hard!
I wish I only had to show the text for the item, but I really need the ID to
continue using that item for other purposes on the parent form.
anyway, thanx
I posted this message on other MS groups but I got no reply so far.
nice weekend!

-----Original Message-----
ok, so basically I can't manually set the valuemember of a listbox item
(webcontrols are more flexible in this cases...)

thanx!!!!!!!!



Ignacio,

Usually when I need to get more data for a selected item
in a listbox I will set each item's "Tag" property to an
object that contains what I need -- usually an instance of
whatever object I am working with. Given a selected item,
you just cast it's Tag value to what you expect and you're
off!

-- TB

Lo siento, Ignacio,

I'm afraid I didn't read your post carefully enough the
first time around. ListBox controls are different from
the ListView controls that I'm more used to. The latter
accept "smarter" items (where each can have a tag). A
listbox does not.

Ok, so let me see if I am understanding your problem.
When the user selects an item in your child form's listbox
you want to pass along both the display member string
*and* value member string for that selected object up to
the parent form.

The value data is, of course, the "SelectedValue" for the
listbox. There is no corresponding "SelectedDisplay", but
you can use "SelectedItem" to get the object you
originally added to the listbox, cast it to the right type
of thing, and then access the property directly. You can
also get the value member this way.

But you're right -- the ListItem Web object allows the
text and value members to be specified but the normal
Forms Listbox just takes untyped Objects for its items and
applies the same DisplayMember and ValueMember properties
to all.

-- TB
 
T

Tim Jarvis

TB said:
-----Original Message-----
thanx for the 2nd response
I had tried casting the [Object] Object from the child form to the parent
form, but I only got "System.Listbox....something", instead of the text of
the item I passed.
I think it has something to do with not giving valuemember and displaymember
values (what values could I give? it's not bound)

it's pretty damn hard!
I wish I only had to show the text for the item, but I really need the ID to
continue using that item for other purposes on the parent form.
anyway, thanx
I posted this message on other MS groups but I got no reply so far.
nice weekend!

I think you're right, that the DisplayMember and
ValueMember properties are going to be useless for you
since you have not provided a DataSource.

However, the SelectedItem property should return the exact
object that was supplied to the ListBox in the first
place. I.e. if you have your own class:

public class foo
{
public int ID;
public string Text;
}

And populate a ListBox via ListBox.Items.Add(aFoo) then
any SelectedItem object should be castable back into a foo
and the fields, methods, properties, etc. accessible
normally.

-- TB

And override the toString() method, to display the text that you want
in the list box.
i.e.
public override string ToString()
{
return Text; // or whatever you want to display.
}

Cheers Tim.
 

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

Top