set checkboxes in listview from list

M

mp

given a listview with checkboxes
user checks some items
list of checked items is saved to xml
user checks or unchecks other items
list of previous choices is readback from xml
(either on button click or next program run)

how *best* to set the items in listview.Item[x].Checked = true
based on items in retrieved list?

heres' what i'm trying (seems to work but probably not right way)
private void lvCriteriaList_ItemCheck(object sender,
System.Windows.Forms.ItemCheckEventArgs e)
{
if (_ignoreCheck) return;
...
}
private void ResetCriteriaChoicesListview(List<Criteria> criteriaChoiceList)
{
Debug.Print("Turn Listview.ItemCheck event off");
_ignoreCheck = true;

ClearCriteria();

Debug.Print("Set checks to stored values");
foreach (Criteria criteria in criteriaChoiceList)
{
for (int i = 0; i < this.lvCriteriaList.Items.Count; i++)
{
if
(this.lvCriteriaList.Items.SubItems[1].Text==criteria.Key )
{
this.lvCriteriaList.Items.Checked = true;
break;
}
}
}
Debug.Print("Turn event back on");
_ignoreCheck = false;
}
private void ClearCriteria()
{
for (int i = 0; i < this.lvCriteriaList.Items.Count; i++)
{
this.lvCriteriaList.Items.Checked = false;
}
}

comments?
thank
mark
 
M

mp

Peter Duniho said:
given a listview with checkboxes
user checks some items
list of checked items is saved to xml
user checks or unchecks other items
list of previous choices is readback from xml
(either on button click or next program run)

how *best* to set the items in listview.Item[x].Checked = true
based on items in retrieved list?

heres' what i'm trying (seems to work but probably not right way)

There's nothing drastically wrong with the code you posted. The only
improvement I might make is to store the Criteria objects in a
Dictionary<string, Criteria> where the key is the Key property of the
Criteria object.

I had thought of that and thought maybe that was redundant storage of the
string key?
also i'm serializing/deserializing the List<Criteria> so i'll have to see if
i can
substitute Dictionary<string,Criteria> as easily



Then just scan all the ListView items once, checking
the dictionary to see if the relevant Criteria object is present for that
item name.

Other than that, seems fine to me. And really, the O(n^2) algorithm isn't
likely to be a real problem for reasonably small numbers of list items.
You'd have to have a list of probably at least hundreds of items before
before it created a delay noticeable to the user, maybe more (since the
operation happens as part of a file i/o operation).

Pete

thinking out loud...can i store the last index so the next for doesn't have
to start again at 0?
(i know the keys are alphabetical)

thanks for the input
mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top