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;
}
}
}