add item to listbox [what am I doing wrong?]

  • Thread starter Ignacio Martínez
  • Start date
I

Ignacio Martínez

Ok, here's the deal. I tried to do this and it didn't work:

[Child Form "Form2"]
Event OK (ByVal item as Object)

'I cast it as Object simply because that's the type of a Listbox Item.

Private Sub Button1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Raiseevent OK(Lisbox1.selecteditem)
End Sub
--------------------------------
[Parent Form "Form1"]
Private Sub Form2_OK(ByVal item as Object) Handles Form2.OK
Listbox.Items.add(item)
End Sub

the fresh new item on Form1's Listbox shows "System.Data.DataRowView"

what's going on?!?!?!?!
thanx for all previous replies!
ignacio

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

100

Hi Ignacio,
the list box shows what ToString method returns. For classes if ToString is
not overriden the default implementation returns the name of the type.
That's why you get the name of the class.
If you want to show particular property of the object use DisplayMember and
ValueMember properties of the ListBox type.
You can also declare wrapper class for the items in the list box. Wrappers
will hold reference to the actual item and will override ToString method in
order for the list box to show proper text

HTH
B\rgds
100


Ignacio Martínez said:
Ok, here's the deal. I tried to do this and it didn't work:

[Child Form "Form2"]
Event OK (ByVal item as Object)

'I cast it as Object simply because that's the type of a Listbox Item.

Private Sub Button1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Raiseevent OK(Lisbox1.selecteditem)
End Sub
--------------------------------
[Parent Form "Form1"]
Private Sub Form2_OK(ByVal item as Object) Handles Form2.OK
Listbox.Items.add(item)
End Sub

the fresh new item on Form1's Listbox shows "System.Data.DataRowView"

what's going on?!?!?!?!
thanx for all previous replies!
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!



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

my original problem came with the need of passing both displaymamber and
valuemember properties.
if it's only the displaymember, that's easy, I can do it several ways, but I
don't know how to send the valuemember property to the parent form listbox.


100 said:
Hi Ignacio,
the list box shows what ToString method returns. For classes if ToString is
not overriden the default implementation returns the name of the type.
That's why you get the name of the class.
If you want to show particular property of the object use DisplayMember and
ValueMember properties of the ListBox type.
You can also declare wrapper class for the items in the list box. Wrappers
will hold reference to the actual item and will override ToString method in
order for the list box to show proper text

HTH
B\rgds
100


Ignacio Martínez said:
Ok, here's the deal. I tried to do this and it didn't work:

[Child Form "Form2"]
Event OK (ByVal item as Object)

'I cast it as Object simply because that's the type of a Listbox Item.

Private Sub Button1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Raiseevent OK(Lisbox1.selecteditem)
End Sub
--------------------------------
[Parent Form "Form1"]
Private Sub Form2_OK(ByVal item as Object) Handles Form2.OK
Listbox.Items.add(item)
End Sub

the fresh new item on Form1's Listbox shows "System.Data.DataRowView"

what's going on?!?!?!?!
thanx for all previous replies!
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
 
H

Herfried K. Wagner [MVP]

* "Ignacio Martínez said:
Ok, here's the deal. I tried to do this and it didn't work:

[Child Form "Form2"]
Event OK (ByVal item as Object)

'I cast it as Object simply because that's the type of a Listbox Item.

Private Sub Button1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Raiseevent OK(Lisbox1.selecteditem)
End Sub
--------------------------------
[Parent Form "Form1"]
Private Sub Form2_OK(ByVal item as Object) Handles Form2.OK
Listbox.Items.add(item)
End Sub

the fresh new item on Form1's Listbox shows "System.Data.DataRowView"

Did you set the 'DisplayMember'?
 
I

Ignacio Martínez

what should I set? there's no datasource. I'm using the Add Function to
insert items one by one.
should I set those properties with the same values as the other listbox?

Herfried K. Wagner said:
* "Ignacio Martínez said:
Ok, here's the deal. I tried to do this and it didn't work:

[Child Form "Form2"]
Event OK (ByVal item as Object)

'I cast it as Object simply because that's the type of a Listbox Item.

Private Sub Button1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Raiseevent OK(Lisbox1.selecteditem)
End Sub
--------------------------------
[Parent Form "Form1"]
Private Sub Form2_OK(ByVal item as Object) Handles Form2.OK
Listbox.Items.add(item)
End Sub

the fresh new item on Form1's Listbox shows "System.Data.DataRowView"

Did you set the 'DisplayMember'?
 
1

100

Hi Ignacio,
Even though you don't use datasource you can still use DisplayMember
property. You don't need to set ValueMember at all. But if you want to you
can set it to whatever you want (for example you can set it to the same
property as DisplayMember). When the selection changes use SelectedIndex or
SelectedItem properties. Don't use SelectedValue it seems like this property
doesn't work without datasource.

Make sure that you use name of *property* (not a member variable) for
DisplayMember. Make also sure that that proerty can be accessed for reading
(it has to be visible - public) If reading the property failed in some
reason (exception thrown, mistyped property name or so) the listbox control
will use item's ToString method to fill out the row.

HTH
B\rgds
100




Ignacio Martínez said:
what should I set? there's no datasource. I'm using the Add Function to
insert items one by one.
should I set those properties with the same values as the other listbox?

"Herfried K. Wagner [MVP]" <[email protected]> escribió en el mensaje
* "Ignacio Martínez said:
Ok, here's the deal. I tried to do this and it didn't work:

[Child Form "Form2"]
Event OK (ByVal item as Object)

'I cast it as Object simply because that's the type of a Listbox Item.

Private Sub Button1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Raiseevent OK(Lisbox1.selecteditem)
End Sub
--------------------------------
[Parent Form "Form1"]
Private Sub Form2_OK(ByVal item as Object) Handles Form2.OK
Listbox.Items.add(item)
End Sub

the fresh new item on Form1's Listbox shows "System.Data.DataRowView"

Did you set the 'DisplayMember'?
 
I

Ignacio Martínez

so, let's make this clear.
I wan to show an Item that's in a database, and has an ID, but for some
reason I can only add it manually into a listbox.
can I insert both Description and ID in the listbox?

a yes / no will do.
so far other posts answered me with many things that had nothing to do with
what I needed because they didn't read my message carefully.
that's why I made that brief example.

thanx for all replies!!!!
Ignacio.

100 said:
Hi Ignacio,
Even though you don't use datasource you can still use DisplayMember
property. You don't need to set ValueMember at all. But if you want to you
can set it to whatever you want (for example you can set it to the same
property as DisplayMember). When the selection changes use SelectedIndex or
SelectedItem properties. Don't use SelectedValue it seems like this property
doesn't work without datasource.

Make sure that you use name of *property* (not a member variable) for
DisplayMember. Make also sure that that proerty can be accessed for reading
(it has to be visible - public) If reading the property failed in some
reason (exception thrown, mistyped property name or so) the listbox control
will use item's ToString method to fill out the row.

HTH
B\rgds
100




Ignacio Martínez said:
what should I set? there's no datasource. I'm using the Add Function to
insert items one by one.
should I set those properties with the same values as the other listbox?

"Herfried K. Wagner [MVP]" <[email protected]> escribió en el mensaje
* "Ignacio Martínez" <[email protected]> scripsit:
Ok, here's the deal. I tried to do this and it didn't work:

[Child Form "Form2"]
Event OK (ByVal item as Object)

'I cast it as Object simply because that's the type of a Listbox Item.

Private Sub Button1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Raiseevent OK(Lisbox1.selecteditem)
End Sub
--------------------------------
[Parent Form "Form1"]
Private Sub Form2_OK(ByVal item as Object) Handles Form2.OK
Listbox.Items.add(item)
End Sub

the fresh new item on Form1's Listbox shows "System.Data.DataRowView"

Did you set the 'DisplayMember'?
 
1

100

Ignacio,
so, let's make this clear.
I wan to show an Item that's in a database, and has an ID, but for some
reason I can only add it manually into a listbox.
can I insert both Description and ID in the listbox?

a yes / no will do.

The easiest answer is NO you cannot add two things directly to the list box.
However you always can declare a class

class ListBoxItem
{
//The following can be made readonly properties
public readonly int ID;
public readonly string Description;

public ListBoxItem(int id, string decr)
{
ID = id;
Description = dscr;
}

public override string ToString()
{
return Description; // Or return ID or both; depends what you want
to show in the list box.
}
}


Now you can add objects of this class to the listbox

listBox.Items.Add(new ListBoxItem(dataID, dataDescription));

and so on.
If it is not what you are asking for I believe you should give some more
detialied example to let us understand your question and problem

HTH
B\rgds
100
 
I

Ignacio Martínez

well, basically I needed to insert directly both valumember and
displaymember values in the listbox.
so "NO you cannot add two things directly to the list box" answers my
question.

I'll do the class thing, I had done it before with a Structure, but I'll do
it that way.
I found it strange, because in ASP.NET with webcontrols you have the
ListItem type, and you can set both value and text properties and add it to
the proper control. Webcontrols seem more flexible regarding types and
methods.

thanx for the help!!!!!!!
ignacio.
 

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