On Nov 2, 10:19 am, Jack Jackson <jacknos...@peb bleridge.comwro te:
On Fri, 2 Nov 2007 09:39:52 -0400, "Academia"
<academiaNOS... @a-znet.comwrote:
(If you've seen this in the drawing NG, sorry. I inadvertently sent it
there.)
I have a listbox populated with Objects.
The Class has a String field that ToString returns.
I assume that is what the ListBox uses for its display. Correct?
If I change the value of the object's string field the ListBox display does
not change. Would you expect it to change?
The Debugger shows that the item does in fact have the new value even though
the display has not changed.
I tried putting
ListBox1.Suspen dLayout() and ListBox1.Resume Layout() around the change code
to see it by chance that would trigger a change in display but it didn't. I
didn't really expect it would.
So I added code to remove and then add the item:
Dim j As Object = ListBox1.Items( ListBox1.Select edIndex)
Dim i As Integer = ListBox1.Select edIndex
ListBox1.Items. RemoveAt(i)
ListBox1.Items. Insert(i, j)
ListBox1.Select edIndex = i
That works. Now as soon as the Object is changed the new value displays.
But I can't say I like it.
Isn't there a better way?
Thanks for any help
PS The ListBox appears to generate a list of strings so that it does not
have to access the objects when a Paint is required? Is that your
understanding?
I think that if the Object implements INotifyProperty Changed that will
fix it. If you Google for that you can find some examples of how to
use it.
I have no idea about the Paint questions.
Hmm, just tried a few things to no avail, and then tried the
INotifyProperty Changed interface you mentioned, but no dice. I
unfortunately don't know of a way to force a complete refresh without
reloading the effected item(s).
Here's the code I used if anyone wants to build on my efforts (just
paste into a new form's codebehind)
/////////////////////
Imports System.Componen tModel
Public Class Form1
Private listBox As ListBox
Private button As Button
Private listBoxItems As List(Of ListBoxItem)
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
'// Load the ListBox
listBox = New ListBox()
Controls.Add(li stBox)
'// Add an event to track if the values change
AddHandler listBox.Selecte dIndexChanged, AddressOf
listBox_Selecte dIndexChanged
'// Add some custom items to our listBoxItems
listBoxItems = New List(Of ListBoxItem)
For i As Integer = 0 To 9
listBoxItems.Ad d(New ListBoxItem(Str ing.Format("Ite m {0}",
i.ToString()), i))
Next
'// Add them to our ListBox
listBox.Items.A ddRange(listBox Items.ToArray() )
'// Load the Button
button = New Button()
button.Text = "Click Me"
button.Location = New Point(150, 10)
Controls.Add(bu tton)
'// Map the Button's click event
AddHandler button.Click, AddressOf button_Click
End Sub
Private Sub listBox_Selecte dIndexChanged(B yVal sender As Object,
ByVal e As EventArgs)
Dim item As ListBoxItem = DirectCast(list Box.SelectedIte m,
ListBoxItem)
MessageBox.Show (String.Format( "{0}, {1}", item.Text,
item.Value))
End Sub
Private Sub button_Click(By Val sender As Object, ByVal e As
EventArgs)
'// Modify the listBoxItems
For Each item As ListBoxItem In listBoxItems
item.Value += 1
item.Text = String.Format(" Item {0}",
item.Value.ToSt ring())
Next
listBox.Update( )
End Sub
End Class
Public Class ListBoxItem
Implements INotifyProperty Changed
Public Sub New(ByVal text As String, ByVal value As Integer)
Me.Text = text
Me.Value = value
End Sub
Public Overrides Function ToString() As String
Return Text
End Function
Public Property Value() As Integer
Get
Return _Value
End Get
Set(ByVal value As Integer)
_Value = value
RaiseEvent PropertyChanged (Me, New
PropertyChanged EventArgs("Valu e"))
End Set
End Property
Private _Value As Integer = 0
Public Property Text() As String
Get
Return _Text
End Get
Set(ByVal value As String)
_Text = value
RaiseEvent PropertyChanged (Me, New
PropertyChanged EventArgs("Text "))
End Set
End Property
Private _Text As String = String.Empty
Public Event PropertyChanged (ByVal sender As Object, ByVal e As
System.Componen tModel.Property ChangedEventArg s) Implements
System.Componen tModel.INotifyP ropertyChanged. PropertyChanged
End Class
/////////////////////
Thanks,
Seth Rowe