How to get ListBox to show changes made to an item in C#?

G

Grant Schenck

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.

How do I get the ListBox to update?

Thanks,
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

You force a refresh calling Control.Invalidate();


cheers,
 
G

Grant Schenck

If I RemoveAt and then Add the item back then it updates.

However, I just tried .Update() but it didn't change.

Assuming my ListBox is called listCalls, then the method would be
listCalls.Update, correct?

By the way, is there a simpler way to have items in a list box (and combo)
where I can simply associate an integer with each item? Having to create a
separate class to be added rather then the text is a hassle.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


Grant Schenck said:
If I RemoveAt and then Add the item back then it updates.

Of course, each time you add/remove an item an Invalidate() is sent to the
control
However, I just tried .Update() but it didn't change.

update redraw the invalidated part of the control, if you change the
collection of items and do nothign to invalidate the control ( put a control
over it, more it, etc ) it does not need to redraw.
Assuming my ListBox is called listCalls, then the method would be
listCalls.Update, correct?

yes, but look above for the difference between change the items collection
adn the visual of the control, Update refer to the latter.
By the way, is there a simpler way to have items in a list box (and combo)
where I can simply associate an integer with each item? Having to create
a
separate class to be added rather then the text is a hassle.

What you mean?

a listbox gets an object collection, you can add anything. what listbox does
is call ToString() for each element.
Meaning you can add an Int directly and it will be displayed correctly.



cheers,
 
A

Aleksandar

Similar problem here. I put objects into ListBox Items collection.
When i change a property of an object that is used in ToString
method nothing changes in ListBox. I tried bith Update and Invalidate
methods of ListBox, but with no result.

Aleksandar
 
J

Jeffrey Tan[MSFT]

Hi Grant,

Does "Ignacio Machin \( .NET/ C# MVP \)"'s reply make sense to you? Is your
problem resolved? Please feel free to tell me, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
I

Ignacio Machin \( .NET/ C# MVP \)

hi,

well I think that now we got to the point where a piece of code is in order
:)

IMO the Invalidate should solve it, if it did not then post the code where
you modify the property, the ToString() method and if possible the code
related to the listbox.

Just to be sure, what if you set the size in such a way that the changed
item is shown/no shown in the listing?

Now , there is a WILD possibility that the problems you are having are due
to another reason, this could happen if the listbox has a optimization that
each time you add a new item it call the ToString() method and then keep
these strings in another private collection, and later user this collection
to actually paint the elements. AFAIK this is not done, but I don;t know
everyting :)

cheers,
 
A

Aleksandar

Hello


Ignacio Machin ( .NET/ C# MVP ) said:
hi,

well I think that now we got to the point where a piece of code is in
order :)


I pasted a sample at the bottom. It can be compiled and run.

IMO the Invalidate should solve it, if it did not then post the code where
you modify the property, the ToString() method and if possible the code
related to the listbox.

Just to be sure, what if you set the size in such a way that the changed
item is shown/no shown in the listing?

Same.



Now , there is a WILD possibility that the problems you are having are due
to another reason, this could happen if the listbox has a optimization
that each time you add a new item it call the ToString() method and then
keep these strings in another private collection, and later user this
collection to actually paint the elements. AFAIK this is not done, but I
don;t know everyting :)


It looks to me that something like this is the case.


Aleksandar


Code:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace ListBoxUpdate
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListBox list;
private System.Windows.Forms.TextBox tbName;
private System.Windows.Forms.TextBox tbSurname;
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
AddPersons();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
private void InitializeComponent()
{
this.list = new System.Windows.Forms.ListBox();
this.tbName = new System.Windows.Forms.TextBox();
this.tbSurname = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// list
//
this.list.Location = new System.Drawing.Point(40, 24);
this.list.Name = "list";
this.list.Size = new System.Drawing.Size(208, 212);
this.list.TabIndex = 0;
this.list.SelectedIndexChanged += new
System.EventHandler(this.list_SelectedIndexChanged);
//
// tbName
//
this.tbName.Location = new System.Drawing.Point(272, 32);
this.tbName.Name = "tbName";
this.tbName.TabIndex = 1;
this.tbName.Text = "";
this.tbName.TextChanged += new
System.EventHandler(this.tbName_TextChanged);
//
// tbSurname
//
this.tbSurname.Location = new System.Drawing.Point(272, 56);
this.tbSurname.Name = "tbSurname";
this.tbSurname.TabIndex = 2;
this.tbSurname.Text = "";
this.tbSurname.TextChanged += new
System.EventHandler(this.tbSurname_TextChanged);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(456, 314);
this.Controls.Add(this.tbSurname);
this.Controls.Add(this.tbName);
this.Controls.Add(this.list);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void AddPersons()
{
list.Items.Add(new Person("Aca", "Faca 1"));
list.Items.Add(new Person("Ser", "Majstro 1"));
list.Items.Add(new Person("Aca", "Faca 2"));
list.Items.Add(new Person("Ser", "Majstro 2"));
list.Items.Add(new Person("Aca", "Faca 3"));
list.Items.Add(new Person("Ser", "Majstro 3"));
list.Items.Add(new Person("Aca", "Faca 4"));
list.Items.Add(new Person("Ser", "Majstro 4"));
list.Items.Add(new Person("Aca", "Faca 5"));
list.Items.Add(new Person("Ser", "Majstro 5"));
list.Items.Add(new Person("Aca", "Faca 6"));
list.Items.Add(new Person("Ser", "Majstro 6"));
list.Items.Add(new Person("Aca", "Faca 7"));
list.Items.Add(new Person("Ser", "Majstro 7"));
list.Items.Add(new Person("Aca", "Faca 8"));
list.Items.Add(new Person("Ser", "Majstro 8"));
list.Items.Add(new Person("Aca", "Faca 9"));
list.Items.Add(new Person("Ser", "Majstro 9"));

list.SelectedIndex = 0;
}

private void tbName_TextChanged(object sender, System.EventArgs e)
{
Person person = (Person)list.SelectedItem;
person.Name = tbName.Text;
list.Invalidate();
}

private void tbSurname_TextChanged(object sender, System.EventArgs e)
{
Person person = (Person)list.SelectedItem;
person.Surname = tbSurname.Text;
list.Invalidate();
}

private void list_SelectedIndexChanged(object sender, System.EventArgs e)
{
Person person = (Person)list.SelectedItem;
tbName.Text = person.Name;
tbSurname.Text = person.Surname;
}

}

class Person
{
private string name;
private string surname;

public string Name
{
get { return name; }
set { name = value; }
}

public string Surname
{
get { return surname; }
set { surname = value; }
}

public Person(string name, string surname)
{
this.name = name;
this.surname = surname;
}

public override string ToString()
{
return name + " " + surname;
}
}
}
 

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