c# - Calculating Listbox Items -
i'm stuck on following:
i have application gui follows:
i created seperate class library 'klant' contains string 'naam' , integer 'consumpties'and linked project.
when user adds name (naam) , number of consumptions (consumpties), these values addes listbox.
here code far:
private void btnklanttoevoegen_click(object sender, routedeventargs e) { list<klant> klanten = new list<klant>(); klanten.add(new klant { naam = txtklantnaam.text, consumpties = int.parse(txtklantconsumpties.text) } ); (int = 0; < klanten.count; i++) { lboverzicht.items.add(klanten[i]); }
i need calculate sum of entered clients , consumptions, i'm stuck on how achieve this.
also, there needs set maximum number of consumptions per client , maximum of total consumptions.
how call this?
i tried creating method calculate total of consumptions, not able accomplish results. code have far follows:
int berekentotaalconsumpties(int totaalconsumpties) { totaalconsumpties = 0; int consumpties = int.parse(txtklantconsumpties.text); (int = 0; <= consumpties; i++) { totaalconsumpties += convert.toint32(lboverzicht.items[i].tostring()); } return totaalconsumpties; }
the result of method needs places in label. follows:
lbloverzicht.content = berekentotaalconsumpties();
klant class follows:
public class klant { public string naam; public int consumpties; public override string tostring() { return string.format("{0} ({1})", this.naam, this.consumpties); } }
how can names (naam) sorted alphabetically? untill have
lboverzicht.items.sortdescriptions.add(new sortdescription("", listsortdirection.ascending));
but doens't seem work?
how can verify users enters letters in field naam , numbers in field consumptions?
for counting, try this:
int totalnaams = 0; int totalconsumpties = 0; foreach (klant item in klanten) { totalconsumpties += item.consumpties; totalnaams += 0; }
as limiting number of consumpties per client, needs done before add listbox. check see if it's less or equal max.
for limiting total comsumpties, need calculate new total after each new addition listview. require different approach counting you're not executing foreach more times need to.
new xaml
<listview x:name="lboverzicht" horizontalalignment="left" height="200" margin="435,372,0,0" verticalalignment="top" width="177"> <listview.view> <gridview> <gridviewcolumn displaymemberbinding="{binding naam}" /> <gridviewcolumn displaymemberbinding="{binding consumpties}" /> </gridview> </listview.view> </listview>
new code-behind
int totalnaams = 0; int totalconsumpties = 0; int consumptiestotalmax = 0; int consumptiesmax = 0; list<klant> klanten = new list<klant>(); public class klant { public string naam { get; set; } public int consumpties { get; set; } public override string tostring() { return string.format("{0} ({1})", this.naam, this.consumpties); } } public void btnklanttoevoegen_click(object sender, routedeventargs e) { int current = int.parse(txtklantconsumpties.text); consumptiestotalmax = int.parse(txttotalmax.text); consumptiesmax = int.parse(txtconsumpmax.text); if (!string.isnullorempty(txtklantconsumpties.text.trim()) && !string.isnullorempty(txtklantnaam.text.trim())) { if (((totalconsumpties + current) <= consumptiestotalmax) && (current <= consumptiesmax)) { klanten.add(new klant { naam = txtklantnaam.text, consumpties = current } ); lboverzicht.itemssource = klanten; totalconsumpties += current; totalnaams += 1; collectionview view = (collectionview)collectionviewsource.getdefaultview(lboverzicht.itemssource); view.sortdescriptions.add(new sortdescription("naam", listsortdirection.ascending)); } } }
Comments
Post a Comment