By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,244 Members | 1,772 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,244 IT Pros & Developers. It's quick & easy.

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

P: n/a
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,
--
Grant Schenck

Nov 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Hi,

You force a refresh calling Control.Invalidate();
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Grant Schenck" <sc******@optonline.net> wrote in message
news:Oe**************@TK2MSFTNGP09.phx.gbl...
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,
--
Grant Schenck

Nov 17 '05 #2

P: n/a
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.
--
Grant Schenck
http://grantschenck.tripod.com

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:Ov**************@TK2MSFTNGP12.phx.gbl...
Hi,

You force a refresh calling Control.Invalidate();
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Grant Schenck" <sc******@optonline.net> wrote in message
news:Oe**************@TK2MSFTNGP09.phx.gbl...
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,
--
Grant Schenck


Nov 17 '05 #3

P: n/a
Hi,
"Grant Schenck" <sc******@optonline.net> wrote in message
news:O3**************@TK2MSFTNGP14.phx.gbl...
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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Nov 17 '05 #4

P: n/a
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
Nov 17 '05 #5

P: n/a
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.

Nov 17 '05 #6

P: n/a
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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Aleksandar" <ad***********@yahoo.co.uk> wrote in message
news:eB**************@TK2MSFTNGP14.phx.gbl...
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

Nov 17 '05 #7

P: n/a
Hello
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:Ou**************@TK2MSFTNGP12.phx.gbl...
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.


cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Aleksandar" <ad***********@yahoo.co.uk> wrote in message
news:eB**************@TK2MSFTNGP14.phx.gbl...
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



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;
}
}
}
Nov 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.