PC Review


Reply
Thread Tools Rate Thread

better way to change a string

 
 
mp
Guest
Posts: n/a
 
      19th Jan 2011
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


 
Reply With Quote
 
 
 
 
mp
Guest
Posts: n/a
 
      19th Jan 2011

"Peter Duniho" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On 1/18/11 4:33 PM, mp wrote:
>> [...]

> 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


 
Reply With Quote
 
mp
Guest
Posts: n/a
 
      19th Jan 2011

"Peter Duniho" <(E-Mail Removed)> wrote in message
news:W-(E-Mail Removed)...
> On 1/18/11 8:15 PM, mp wrote:
>> [...]


> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Change 3 letter text string to a number string Pete Microsoft Excel Misc 3 31st Dec 2007 07:47 PM
Change value in a string =?Utf-8?B?YWx2aW4gS3VpcGVy?= Microsoft Excel Programming 5 13th Feb 2006 09:29 AM
Change value of the default string =?Utf-8?B?QnJpYW4=?= Microsoft Windows 2000 Registry 1 10th Jan 2004 03:53 PM
Change value of the default string =?Utf-8?B?QnJpYW4=?= Microsoft Windows 2000 Registry Archive 2 10th Jan 2004 03:53 PM
change the value of string manoj Microsoft C# .NET 2 13th Sep 2003 05:27 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:19 AM.