473,240 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,240 software developers and data experts.

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

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
7 2473
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Byron | last post by:
I'm looking for a way to deny a move from the current listbox item. For instance, if the user is editing the record associated with the current listbox item I want to deny a move within the...
1
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...
1
by: jez123456 | last post by:
Hi, I have a windows form with a listbox control. My code all works correctly when deleting an item from the listbox except the last item. I get the following message when trying to delete the...
8
by: tshad | last post by:
I have a string that I read from my database: 1|8|5620|541 These are all values in my ListBox. I want to select each of these items (4 of them - but could be many more). At the moment I am...
9
by: Larry Serflaten | last post by:
I'm drawing my own inherited listbox, and I don't want the user to select any items, but I do want to allow them to scroll the listbox. When they click on an item, there is a flicker of the...
28
by: cjobes | last post by:
Hi all, I need to populate a listbox from a table column. The column has multiple entries of usernames and I need to pull a unique set of usernames. The table is part of an untyped Dataset that...
18
by: Zytan | last post by:
I want the same function to be run whether you press Enter or double click the listbox. It seems really verbose to write both handlers to both events everytime, even if they both call the same...
5
by: Academia | last post by:
(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...
15
by: Doogie | last post by:
I have a .net app that a user currently enters a number in a text box, hits a button and a data call is executed. She wants the ability to enter in multiple numbers (up to 100). So to make...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.