473,419 Members | 4,368 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,419 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 2488
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.