better way to change a string

M

mp

I have a listview with checkboxes showing
as user checks and unchecks boxes i want to track the choices in a string
(this will become part of a connection string of sorts)
I guess i could defer the string construction till user is done selecting
and have them hit a button to commit or something like that

here i am updating a label just to show current selections
a bit redundant since they can see the listview choices itself
but they may have to scroll the listview where the label would show the
whole set
of choices in one place

if i did want to keep the updating label, is there a better way
than what i'm doing here? populating a sorteddictionary
adding items when checked, removing when unchecked
and then updating the label each time to show current selections
it works but maybe not efficient???


private void lvCriteriaList_ItemCheck(object sender,
System.Windows.Forms.ItemCheckEventArgs e)
{
if(sdictCriteriaList_Selected == null)
{sdictCriteriaList_Selected=new SortedDictionary<string,string>
();}

if (e.CurrentValue == CheckState.Checked)
{//this means it was checked before the user clicked it...so now
it is unchecked
if
(sdictCriteriaList_Selected.ContainsKey(this.lvCriteriaList.Items[e.Index].SubItems[1].Text))
{ //remove from dictionary
sdictCriteriaList_Selected.Remove(this.lvCriteriaList.Items[e.Index].SubItems[1].Text);
}
else//add to dictionary
{
sdictCriteriaList_Selected.Add(this.lvCriteriaList.Items[e.Index].SubItems[1].Text,
this.lvCriteriaList.Items[e.Index].Text); }
}
else if ((e.CurrentValue == CheckState.Unchecked))
{
sdictCriteriaList_Selected.Add(this.lvCriteriaList.Items[e.Index].SubItems[1].Text,
this.lvCriteriaList.Items[e.Index].Text);
}

//rebuilding string each time, probably not good way to do it???
sbCriteriaList_Selected = new StringBuilder();
sbCriteriaList_Selected.Append("Selected items" );
sbCriteriaList_Selected.Append(Environment.NewLine);

foreach (KeyValuePair<string, string> kvp in
sdictCriteriaList_Selected)
{
sbCriteriaList_Selected.Append(kvp.Key);
sbCriteriaList_Selected.Append(Environment.NewLine);
}
this.lblSelectedCriteria.Text =
sbCriteriaList_Selected.ToString();
this.lblSelectedCriteria.Refresh();
}

thanks for any ideas/input
marlk
 
M

mp

Peter Duniho said:
That said, I would suggest that you handle the ItemChecked event
instead, so that you can just check the current state of the item, or at
the very least, use the "NewValue" property of the ItemCheck event. That
will allow the dictionary maintenance to match the state of the value
checked, rather than being opposite (and in any case, the "add if not
there, remove if present" logic in the "CheckState.Checked" doesn't seem
correct to me.why not just add/remove depending strictly on the checkbox
state?).

Pete

i'm blind...i didn't see NewValue
when i saw CurrentValue i assumed that was the new(current)value,
but found it was the previous value so had to flip the orientatiion.
that would be much nicer to make it match as you say.
and you're right about the not needing to check .contains
i was just trying to bypass a possible error(which should never occur)

i originally used ItemChecked but it fires every time the items are added
while loading the listview, in addition to when the items are checked by
user
thanks
mark
 
M

mp

Peter Duniho said:
Either can work, and it's not a huge deal to do it in ItemCheck rather
than ItemChecked (especially if you know that items are only ever added in
the unchecked state, and always ever will be).

in this case that is true(new items are not added after initial load), but
good to know for future

But you should not
worry too much about repeating all that logic every time an item is added
to the list. It isn't going to be any kind of performance bottleneck, and
again if the code is simpler that way, that's the better choice.

Pete

when i started playing with the event to see how and when it worked
and why my assumption about .CurrentValue wasn't working, i had a message
box
that's how i found out about it firing as it was loading...
of course i got rid of the msgbox once i figured out CurVal was opposite
what i thought.

so, that's right, it wouldn't matter in reality that the event was firing
before i really had to start watching the event...
I didn't know off the top of
my head how to tell the difference between the event as control was loading
versus the event when user was interacting, so i just went with the event
that
only fired on user interaction. I probably could set some bool var when the
loading starts
and reset it once it's done loading, then check that in the event to see
whether to fire,

but if i didn't do that(bool var), a new item is added, (obviously in an
unchecked state), the event tries
to remove from dictionary, but it's not in dictionary yet, so wouldn't that
throw an error?
i'll try later, maybe remove doesn't error on nonexistant key, maybe it just
ignores it?
and of course i can just add back the error check but as you said, it's
cleaner without it.

thanks for that
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