468,769 Members | 2,310 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,769 developers. It's quick & easy.

confused listview.items during itemChecked Event

Hi,

I have a problem with Listview using checkboxes.
If i check items by code BEFORE the form is shown the Listview.Items
are confused during the ItemChecked Event !!!
After showing the form every thing works fine: checking items by code
as well as checking with mouse:
Using the CheckdItems Property is confused too.
I load Settings from XML into different Listboxes, depending on
check-
States differnt data will load from database, so i will doing this
"initialising" before showing the form.
....any and suggestions or ideas
Thanks!
Sample:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
this.listView.CheckBoxes = true;
this.listView.View = System.Windows.Forms.View.List;
this.listView.Columns.Add("Test");
this.listView.Items.Add(new ListViewItem("one"));
this.listView.Items.Add(new ListViewItem("two"));
this.listView.Items.Add(new ListViewItem("three"));
this.listView.ItemChecked += new
System.Windows.Forms.ItemCheckedEventHandler(this. listView_ItemChecked);
}
private void listView_ItemChecked(object sender,
ItemCheckedEventArgs e)
{
System.Console.WriteLine("ItemChecked evt: {0} {1}" ,
e.Item, e.Item.Checked);
List("during event");
}
public void Test(string text, bool on)
{
List(text);
this.listView.Items[1].Checked = on;
}
private void List(string text)
{
System.Console.WriteLine("{3}: {0}, {1}, {2}",
listView.Items[0].Text,
listView.Items[1].Text,
listView.Items[2].Text,
text.PadRight(15)); }
}
//---------------------------------------------------------------------
using the form in another form (e.g. Buttonclick)
Form2 frm = new Form2();
frm.Test("before Show", true);
frm.Show();
frm.Test("after Show", false);
will show:
before Show : one, two, three
ItemChecked evt: ListViewItem: {one} False
during event : one, one, one ! ! !
ItemChecked evt: ListViewItem: {two} False
during event : one, two, one ! ! !
ItemChecked evt: ListViewItem: {two} True
during event : one, two, one ! ! !
ItemChecked evt: ListViewItem: {three} False
during event : one, two, three
after Show : one, two, three
ItemChecked evt: ListViewItem: {two} False
during event : one, two, three
Dec 5 '07 #1
2 6289
Peter,
>If i check items by code BEFORE the form is shown the Listview.Items
are confused during the ItemChecked Event !!!
Unless your event handler writes changes back to the underlying data source
your ListViewItems should not get confused. If you constructed your ListView
before the form is shown, the events, if any, will be fired when the form
becomes visible. If your event handler does change the underlying data, then
you have to prevent unwanted changes caused by events fired automatically,
as opposed to events fired in response to user's input.

I am quite confused by the code you listed. It calls Console from
Windows.Forms?

Michael

"Peter" <pR****@procom-gmbh.comwrote in message
news:a8**********************************@w28g2000 hsf.googlegroups.com...
Hi,

I have a problem with Listview using checkboxes.
If i check items by code BEFORE the form is shown the Listview.Items
are confused during the ItemChecked Event !!!
After showing the form every thing works fine: checking items by code
as well as checking with mouse:
Using the CheckdItems Property is confused too.
I load Settings from XML into different Listboxes, depending on
check-
States differnt data will load from database, so i will doing this
"initialising" before showing the form.
...any and suggestions or ideas
Thanks!
Sample:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
this.listView.CheckBoxes = true;
this.listView.View = System.Windows.Forms.View.List;
this.listView.Columns.Add("Test");
this.listView.Items.Add(new ListViewItem("one"));
this.listView.Items.Add(new ListViewItem("two"));
this.listView.Items.Add(new ListViewItem("three"));
this.listView.ItemChecked += new
System.Windows.Forms.ItemCheckedEventHandler(this. listView_ItemChecked);
}
private void listView_ItemChecked(object sender,
ItemCheckedEventArgs e)
{
System.Console.WriteLine("ItemChecked evt: {0} {1}" ,
e.Item, e.Item.Checked);
List("during event");
}
public void Test(string text, bool on)
{
List(text);
this.listView.Items[1].Checked = on;
}
private void List(string text)
{
System.Console.WriteLine("{3}: {0}, {1}, {2}",
listView.Items[0].Text,
listView.Items[1].Text,
listView.Items[2].Text,
text.PadRight(15)); }
}
//---------------------------------------------------------------------
using the form in another form (e.g. Buttonclick)
Form2 frm = new Form2();
frm.Test("before Show", true);
frm.Show();
frm.Test("after Show", false);
will show:
before Show : one, two, three
ItemChecked evt: ListViewItem: {one} False
during event : one, one, one ! ! !
ItemChecked evt: ListViewItem: {two} False
during event : one, two, one ! ! !
ItemChecked evt: ListViewItem: {two} True
during event : one, two, one ! ! !
ItemChecked evt: ListViewItem: {three} False
during event : one, two, three
after Show : one, two, three
ItemChecked evt: ListViewItem: {two} False
during event : one, two, three


Dec 5 '07 #2
System.Console.WriteLine is just to print out some messages!

You are right: ..."it should not get confused...."
But - if you look at my example, there is no changing in the
datasource !!
Reading Items during the ItemCheck Event before the listview is shown
(visible) birng out wrong results !

Peter
On 5 Dez., 15:59, "Michael Rubinstein" <mSPAM_REMOVEr@m(R)ubinstein.com>
wrote:
Peter,>If i check items by code BEFORE the form is shown theListview.Items
are confused during the ItemChecked Event !!!

Unless your event handler writes changes back to the underlying data source
your ListViewItems should not get confused. If you constructed yourListView
before the form is shown, the events, if any, will be fired when the form
becomes visible. If your event handler does change the underlying data, then
you have to prevent unwanted changes caused by events fired automatically,
as opposed to events fired in response to user's input.

I am quite confused by the code you listed. It calls Console from
Windows.Forms?

Michael

"Peter" <pRu...@procom-gmbh.comwrote in message

news:a8**********************************@w28g2000 hsf.googlegroups.com...
Hi,
I have a problem withListviewusing checkboxes.
If i check items by code BEFORE the form is shown theListview.Items
are confused during the ItemChecked Event !!!
After showing the form every thing works fine: checking items by code
as well as checking with mouse:
Using the CheckdItems Property is confused too.
I load Settings from XML into different Listboxes, depending on
check-
States differnt data will load from database, so i will doing this
"initialising" before showing the form.
...any and suggestions or ideas
Thanks!
Sample:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
this.listView.CheckBoxes = true;
this.listView.View = System.Windows.Forms.View.List;
this.listView.Columns.Add("Test");
this.listView.Items.Add(new ListViewItem("one"));
this.listView.Items.Add(new ListViewItem("two"));
this.listView.Items.Add(new ListViewItem("three"));
this.listView.ItemChecked += new
System.Windows.Forms.ItemCheckedEventHandler(this. listView_ItemChecked);
}
private void listView_ItemChecked(object sender,
ItemCheckedEventArgs e)
{
System.Console.WriteLine("ItemChecked evt: {0} {1}" ,
e.Item, e.Item.Checked);
List("during event");
}
public void Test(string text, bool on)
{
List(text);
this.listView.Items[1].Checked = on;
}
private void List(string text)
{
System.Console.WriteLine("{3}: {0}, {1}, {2}",
listView.Items[0].Text,
listView.Items[1].Text,
listView.Items[2].Text,
text.PadRight(15)); }
}
//---------------------------------------------------------------------
using the form in another form (e.g. Buttonclick)
Form2 frm = new Form2();
frm.Test("before Show", true);
frm.Show();
frm.Test("after Show", false);
will show:
before Show : one, two, three
ItemChecked evt: ListViewItem: {one} False
during event : one, one, one ! ! !
ItemChecked evt: ListViewItem: {two} False
during event : one, two, one ! ! !
ItemChecked evt: ListViewItem: {two} True
during event : one, two, one ! ! !
ItemChecked evt: ListViewItem: {three} False
during event : one, two, three
after Show : one, two, three
ItemChecked evt: ListViewItem: {two} False
during event : one, two, three- Zitierten Text ausblenden -

- Zitierten Text anzeigen -


Dec 6 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Nick | last post: by
reply views Thread by Dred | last post: by
reply views Thread by Ryan Liu | last post: by
12 posts views Thread by garyusenet | last post: by
3 posts views Thread by RT | last post: by
1 post views Thread by =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.