473,748 Members | 4,030 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Listbox does not display the changes made to an object

(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?


Nov 2 '07 #1
5 2853
On Nov 2, 9:39 am, "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?
Will a call to ListBox.Refresh () or ListBox.Invalid ate() work?

Thanks,

Seth Rowe

Nov 2 '07 #2
I should have mentioned that I had also tried ListBox.Refresh .

Thanks

"rowe_newsgroup s" <ro********@yah oo.comwrote in message
news:11******** *************@v 3g2000hsg.googl egroups.com...
On Nov 2, 9:39 am, "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.Suspe ndLayout() 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.Selec tedIndex = 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
understandin g?

Will a call to ListBox.Refresh () or ListBox.Invalid ate() work?

Thanks,

Seth Rowe

Nov 2 '07 #3
On Fri, 2 Nov 2007 09:39:52 -0400, "Academia"
<ac************ @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.Suspe ndLayout() 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.Selec tedIndex = 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
understandin g?
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.
Nov 2 '07 #4
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

Nov 2 '07 #5
I was able to take your example and a slightly different approach and get the
behavior you were looking for. Instead of using a list, I used a
BindlingList and instead of listBox.Items.A ddRange(listBox Items.ToArray() )
I had
listBox.DataSou rce = listBoxItems
The difference (I think) is that the BindingList looks for the
PropertyChanged event and raises a 'ListChanged' event, that the ListBox
looks for.
One thing that happened that I don't understand was that when the button was
clicked, and the items were changed, the SelectedIndexCh anged event was
fired! And on the second change (second item in the list) the event was
fired, but the selecteditem was 'Nothing'. I had to comment out the code in
the routine to get it to work.

--
Terry
"rowe_newsgroup s" wrote:
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.Suspe ndLayout() 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.Selec tedIndex = 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
>understandin g?
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

Nov 2 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4221
by: Fieldmedic | last post by:
I'm attempting to get an arraylist to display in a Listbox (listbox2). The arraylist is created from selections in a different listbox (listbox1). The 1st listbox has a selection mode of MultiExtended. Once the selection has been made in the 1st ListBox, the user clicks on a button to generate the arraylist using the values selected. Then click a second button to display the arraylist contents to ListBox2. There are no errors, but all I get...
1
3577
by: Annette Massie | last post by:
I would like to have a form that lists current addresses being used. On this form I would also like to have a command button that would allow the user to add an address if they do not see it listed. Is it possible to have the form show the list box (which is a combinination of the address and city/state/zip), along with the detail fields address and city/state/zip? I guess I was thinking of have the listbox display and then when the...
3
9426
by: gdbjohnson-AT-yahoo-dot-ca-nospamplz | last post by:
I have a ListBox built of a simple custom object for the ListItems used to be able to hold a Data Value, and a Display Value, with accessors for each. I have overridden the ToString method to display the Display value. (Two examples that I have seen, including an MS example, suggest that the DisplayMember and ValueMember properties of the ListBox can be used in this way, I could not get it to work using the ListItems.Add method -...
1
2771
by: Dan Bass | last post by:
I'm looking to develop a listbox with in-place editing where as each item is selected, it grows to fit in all the text boxes. When the item is deselected, it shrinks back to its original size. The editing bit is not a problem, but I can't get the selected item to resize, as the listbox does for the "Add/Remove programs". The initial thought I had was to have a handler for the MeasureItem event, then have the condition of whether the...
7
2509
by: Grant Schenck | last post by:
Hello, I have a ListBox control on a form. I add members of a class to the Items collection. They show up and I can select them. The text shown is from my classes ToString override. Now, later on I need to update this item. I find it in the Items collection and update a member used by the ToString member function. However, the text shown for the item in the ListBox does not change.
8
2884
by: Oddball | last post by:
Ok - I have a ListBox control and I'm ready to write my own DrawItem event handler. What I want to draw as the item is another control. I have created a user control that I would like to list in this listbox but I can't for the life of me figure out how to draw the control inside ListBox... I get as far as: private void lbImageList_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
2
1241
by: Lloyd Sheen | last post by:
I am making changes to listbox contents in JavaScript and those changes are not persisted when a PostBack happens. Anything I add during Server events are saved and restored. I only have access to the information for JavaScript. I have an embedded object that cannot be accessed at server execution time so I need to save information in JavaScript. If I add the information to hidden text field it is retained. Why not the listbox. I...
1
8421
by: Edward | last post by:
I am having a terrible time getting anything useful out of a listbox on my web form. I am populating it with the results from Postcode lookup software, and it is showing the results fine. What I want to do is to allow the user to click on the row that corresponds to the correct address, and have the code behind populate the form's Address1, Address2 etc. controls with the relevant data items. I put the code for this into the...
5
4415
by: Dave | last post by:
Hi All, I have a windows form that contains 2 listboxes and 2 buttons. The listbox on the right is populated by a database routine (This is the easy part). The listbox on the left is populated by 1 or more selected items from the listbox on the right after clicking an Add button. Clicking a "Remove" button will remove the item from the left listbox and restore it back to the right box.
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8831
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9548
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9249
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4607
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4876
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3315
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.