c# - Duplicate clicks on xx_Tap() happening in separate class -
the scenario:
want tap on textblock , run method add item cart.
no, don't prefer buttons textblocks, thank :)
the code (shoppingcart.cs)
using system; using system.collections.generic; using system.linq; using system.text; using system.diagnostics; using system.threading.tasks; namespace m_pos { public class shoppingcart { int cartnum; int duplicate=0; int num_of_items; int counter=1; list<item> items = new list<item>(); //constructor public shoppingcart() { this.cartnum = counter; counter += 1; this.num_of_items = 0; this.items = new list<item>(); init_items(); } //return item list of tapped/purchased items public list<item> getitems(){ return this.items; } //returns number of items tapped/purchased public int get_num_of_items() { return this.num_of_items; } // method adds tapped-on item items list public void additem(string itemx,string qty) { (int = 0; < item.pick_item.count; i++) { if (itemx.equals(item.pick_item[i].getname())) { item itm = new item(item.pick_item[i].getname(), item.pick_item[i].getprice()); itm.addqty(convert.toint16(qty)); this.items.add(itm); debug.writeline("added cart!!"); } } this.num_of_items += convert.toint16(qty); } //used test additem() works. everytime class run, rolex item //be first added cart. always. funny thing is, doesnt // duplicated. private void init_items() { item itm12 = new item("rolex", 4000); //additem(itm12); this.items.add(itm12); } } //////////////////////////////////////////////////////////////////////////////////////// public class item { string itemname; int itemamount; int itemqty = 0; public static list<item> pick_item = new list<item>(); public static list<string> menu_food = new list<string> { "single beef burger", "double beef burger", "triple beef burger", "single chicken burger", "double chicken burger", "single veggie burger", "1/2 fries", "full fries", "beef steak", "mushroom", "steamed rice", "rolex"}; public static list<string> menu_price = new list<string>{ "8000", "17000", "25000", "12000", "26500", "7500", "4000", "6000", "20000", "25000", "17500", "4000"}; public item(string name, int amount) { this.itemamount = amount; this.itemname = name; this.itemqty = 1; } public static void init_menu() { (int = 0; < get_menu().count; i++) { item itm = new item(menu_food[i], convert.toint32(menu_price[i])); pick_item.add(itm); } } public void addqty(int qty) { this.itemqty = qty; } public string getname() { return this.itemname; } public int getprice() { return this.itemamount; } public static int getpxbyname(string itemname) { int ans=0; (int y = 0; y < pick_item.count; y++) { if (pick_item[y].itemname.tostring().equals(itemname)) { ans = pick_item[y].itemamount; } } return ans; } public static list<string> get_menu() { return menu_food; } public static list<string> get_price() { return menu_price; } } }
where duplicates happening?
i'm getting additem(string itemname,int itemqty) being run twice on every tap. else perfect, though.
what have done before posting?
- tested tap
event , made sure being fired once per tap? check.
- tested additem()
method make sure works being with? check. add single item cart everytime app started. item never gets duplicated.
console debug.writeline() shows
added cart!! added cart!! called method system.windows.controls.textblock
the first 2 added cart
s method being called twice.
the next called method system.windows.controls.textblock
debug inserted after calling method food.xaml.cs
food.xaml.cs [part of it]
public foods() { initializecomponent(); item.init_menu(); populatemenu(); } public void populatemenu() { list<string> display = item.get_menu(); (int = 0; < display.count; i++) { string tname = "tb" + i; textblock tb = new textblock(); tb.tap += new eventhandler<system.windows.input.gestureeventargs>(tb_click); tb.style = (style)resources["textblocker"]; tb.fontsize = 36; tb.text = display[i]; tb.name = tname; sp_lunch.children.add(tb); } } private void tb_click(object sender, routedeventargs e) { tapped += 1; selectqty(sender); } private void selectqty(object sender) { popup popup = new popup(); popup.height = 300; popup.width = 400; popup.verticaloffset = 100; popupcontrol control = new popupcontrol(); popup.child = control; popup.isopen = true; string qty=""; control.btnok.click += (s, args) => { popup.isopen = false; //pick input popup's textbox. qty = control.tbx.text; if (qty == null||qty=="") { qty = "0"; } //send clicked item cart addition textblock clicked = ((textblock)sender); string temp = clicked.text; cart.cart_new.additem(temp, qty); debug.writeline("called method "+sender.tostring()); tb_pamount_lunch.text = convert.tostring(cart.cart_new.get_num_of_items()); //tb_pamount_lunch.text = tapped.tostring(); messagebox.show(temp); //update dinner stackpanel display selected items sp_dinner.children.clear(); list<item> display = cart.cart_new.getitems(); (int = 0; < display.count; i++) { textblock tb1 = new textblock(); tb1.fontsize = 36; tb1.text = display[i].getname().tostring(); sp_dinner.children.add(tb1); } }; control.btncancel.click += (s, args) => { //close popup when cancel clicked popup.isopen = false; }; }
any more info??
if there's other class you'd want take at, i'll gladly copy/paste here or upload whole project.zip :)
i think when tab thrid time, 3 items added.
(s, args) => { popup.isopen = false; //pick input popup's textbox. qty = control.tbx.text; if (qty == null||qty=="") { qty = "0"; } //send clicked item cart addition textblock clicked = ((textblock)sender); string temp = clicked.text; cart.cart_new.additem(temp, qty); debug.writeline("called method "+sender.tostring()); tb_pamount_lunch.text = convert.tostring(cart.cart_new.get_num_of_items()); //tb_pamount_lunch.text = tapped.tostring(); messagebox.show(temp); //update dinner stackpanel display selected items sp_dinner.children.clear(); list<item> display = cart.cart_new.getitems(); (int = 0; < display.count; i++) { textblock tb1 = new textblock(); tb1.fontsize = 36; tb1.text = display[i].getname().tostring(); sp_dinner.children.add(tb1); } }; control.btncancel.click += (s, args) => { //close popup when cancel clicked popup.isopen = false; };
this because control still exists , every time call selectqty action added eventhandler list. , executed more ones. should register event ones. in constructor.
personal: should register events button within subcontrol. instead create new event on control , raise there. give advantage change buttons or other controls (maybee want short-cut keys in future) on controls without altering mainform.
example:
public class form1 : form { public form1() { initializecomponent(); control.btnokclicked += control_buttonok; control.btncancelclicked += control_buttoncancel; } private void control_buttonok(object sender, eventargs e) { // implement code } private void control_buttoncancel(object sender, eventargs e) { // implement code } } public class usercontrol1: usercontrol { public usercontrol1() { initializecomponent(); btnok.click += (sender, e) => { if(btnokclicked != null) btnokclicked(this, eventargs.empty); }; btncancel.click += (sender, e) => { if(btncancelclicked!= null) btncancelclicked(this, eventargs.empty); }; } public event eventhandler btnokclicked; public event eventhandler btncancelclicked; }
this way separate functionallity/dependency of layout of control.
as writing this, think might on here: form.showdialog method http://msdn.microsoft.com/en-us/library/c7ykbedk.aspx handle popup.isopen , block mainform until it's closed. dialogresult can read if user pressed ok or cancel.
Comments
Post a Comment