Listbox item question

  • Thread starter Thread starter Bilal Abbasi
  • Start date Start date
B

Bilal Abbasi

I realize that you can add items to a list box as objects so you can have
access to more than just one property like the itemindex in vb6. Question I
have is how do I cause the listbox to show a different text after the object
has already been added.
 
For any given object that may be contained in the listbox it is somtimes
common practice to override the ToString function as the default text which
is displayed will be the objects name. If the object contains a value that
you want displayed then you can return this from the overridden function.

Regards - OHM





Bilal said:
I realize that you can add items to a list box as objects so you can
have access to more than just one property like the itemindex in vb6.
Question I have is how do I cause the listbox to show a different
text after the object has already been added.

Best Regards - OHMBest Regards - OHM (e-mail address removed)
 
:)

you create your own class to add as an item this class can have as mutch
vars as you need
and has to have a overrides to string function to display the text in the
listbox
i'll paste the code below

hope it helps

eric

code
\\
dim li as new clslistitem
li.text = "test"
listbox.items.add(li)
li = new clslistitem("test2")
listbox.items.add(li)
//
if you need to use an item

\\
dim itemke as clslistitem
itemke = listbox.selecteditem
var = itemke.text
//
i havent done altering but it should be similar
try
listbox.selecteditem.text = "test"
listbox.refresh


the class
\\
Public Class clsListItem
Private Tekst As String
Private Tagske As String

Private objke As Object

Public Sub New()
Tekst = ""
Tagske = ""
Omschrijvingske = ""
End Sub

Public Sub New(ByVal te As String)
Tekst = te
Tagske = ""
Omschrijvingske = ""
End Sub


Public Sub New(ByVal te As String, ByVal ta As String)
Tekst = te
Tagske = ta
Omschrijvingske = ""
End Sub
Public Sub New(ByVal te As String, ByVal ta As String, ByVal oms As
String)
Tekst = te
Tagske = ta
Omschrijvingske = oms
End Sub
Public Property Text() As String
Get
Return Tekst
End Get
Set(ByVal Value As String)
Tekst = Value
End Set
End Property

Public Property Tag() As String
Get
Return Tagske
End Get
Set(ByVal Value As String)
Tagske = Value
End Set
End Property
Public ReadOnly Property classe() As Integer
Get
Return 0
End Get
End Property

Public Property Obj() As Object
Get
Return objke
End Get
Set(ByVal Value As Object)
objke = Value
End Set
End Property

Public Overrides Function ToString() As String
Return Tekst
End Function

End Class
//
 
Thanks EricJ

What I do right now is
Dim cl As cListItem = DirectCast(List1.Items(List1.SelectedIndex),
cListItem)

where cListItem is my own class.

When I change the text property of cl, it does not get reflected in the
listbox.

i.e.

cl.text = "***" & cl.text

I have tried list1.beginUpdate and list1.endUpdate

Any ideas?
 
* "Bilal Abbasi said:
I realize that you can add items to a list box as objects so you can have
access to more than just one property like the itemindex in vb6. Question I
have is how do I cause the listbox to show a different text after the object
has already been added.

Add a custom implemented 'ToString' method which returns a string to
your class. This method returns the description text for the item.
 
I have this declaration for the ToString(): in my cListItme class.

Public Overrides Function toString() As String

Return sLookup

End Function
 
* "Bilal Abbasi said:
I have this declaration for the ToString(): in my cListItme class.

Public Overrides Function toString() As String

Return sLookup

End Function

Does it work?
 
Thats really weird, I seem to have posted a reply here which is invisible to
everyone but me !!

I would say regards - ohm, but whats the point as I'm talking to myself.

For any given object that may be contained in the listbox it is
somtimes common practice to override the ToString function as the
default text which is displayed will be the objects name. If the
object contains a value that you want displayed then you can return
this from the overridden function.

Regards - OHM







Best Regards - OHMBest Regards - OHM (e-mail address removed)

Best Regards - OHMBest Regards - OHM (e-mail address removed)
 
Hi OHM,

I dont know why you don't get an answer from Bilal, but I see messages from

OHM 17:13
Eric 17:14
Herfried 19:13

For you that would be 16:13, 16:14, 18:13 I think

Just for your information.
 
Hi OHM,

My class that I am using to add items to the list box, actually does have
the toString() function overridden. Since I have option_strict set to on, I
am not able to directly change the property from the list box like,

list1.items(list1.selectedIndex).text = "new text"

What I have to do is get the object and change it's value. But it does not
get reflected in the list box.
i.e.
dim cl as cListItem = DirectCast(list1.item(list1.selectedIndex),cListItem)
'where cListitem is my class.
cl.text = "new text"
does not cause the listbox item to change it's text, but the object itself
does change. Weird.
 
Hi Bilal,

When OHM does not answer anymore, you can maybe try this

\\\\
Dim cl As clsListItem = DirectCast(List1.SelectedItem, clsListItem)
cl.text="new text"
List1.Items.RemoveAt(List1.SelectedIndex)
List1.Items.Add(cl)
///

I hope this helps?

Cor
 
Hi Bilal,
Did try this and the removeat emthod throws an ArgumentOutOfRangeException.

What event did you use to do this, I did it with the mouse up, tested it and
now even with this complete code, and it did work?

Cor
 
i played around w it a bit and the text isn't changing (strange) even if you
refresh :/

i did find a workaround that works in a combobox (dont have a listbox to
mess w atm)

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim li As New clsListItem
li = cboSFilm.SelectedItem
Dim i As Integer = cboSFilm.SelectedIndex
cboSFilm.Items.RemoveAt(cboSFilm.SelectedIndex)
li.Text = li.Text & " test"
cboSFilm.Items.Insert(i, li)
End Sub

hope it helps

eric
 
hey, tel you what,
Please post you class and we can try this OK


PS: Sorry for delay in answering, I've been building my kids cristmas
present ( 1.3GHZ XP machine ), he's thirteen and a half and he's already
learnt HTML by tag reference manuals etc and now he's learning PHP, and
wants to learn VB.NET as well ( I get sooooo many questions when he gets
home from school every day ).

When I was his age, I was still climbing trees and collecting birds eggs.


CYA L8TR - OHM



Bilal said:
Did try this and the removeat emthod throws an
ArgumentOutOfRangeException.

Best Regards - OHMBest Regards - OHM (e-mail address removed)
 
Perhaps it's the version of Visual Studio that we are using. I ran into a
glitch earlier and posted a few messages here before regarding selectedIndex
throwing invalidCastException. I am using Visual Studio Dot Net 2003. What
are you using?
 
that would be a 2003 ea on win xp pro

but like i said i didn't test on a listbox only on a combo

gimme a few min
 
sorry this works (option explicit off) if its on you will probably have to
cast the selecteditem as a clslistitem
hope it helps
Try
Dim i As Integer
Dim li As New clsListItem
li = ListBox1.SelectedItem
i = ListBox1.SelectedIndex
ListBox1.Items.RemoveAt(i)
li.Text = li.Text & " test"
ListBox1.Items.Insert(i, li)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
 
Just a Few thoughts . . .

** These really dont need to be in the Try Block **
Dim i As Integer
Dim li As New clsListItem

** What if no item is selected, you could have put this in an IF statement,
and prevented throwing an exception
li = ListBox1.SelectedItem
i = ListBox1.SelectedIndex

** Then this could have been written like so without a try catch **
ListBox1.Items.RemoveAt( ListBox1.SelectedIndex )

li.Text = li.Text & " test"
ListBox1.Items.Insert(i, li)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

Regards - OHM
 
Hi OHM,

When you have in your listbox
listbox1.sort = true
It does not matter if you use an insert or an add.

Just a thought.

Cor
 
Back
Top