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