confused listview.items during itemChecked Event

  • Thread starter Thread starter Peter
  • Start date Start date
P

Peter

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
 
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 said:
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
 
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


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




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!

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 -
 
Back
Top