c# - EventHandler inside loop for several Windows Forms -
my code creating multiple instances of form different controls , events. done while incrementing i
total amount of forms. aim add event button control in form. forms instances of ticker
, subclass of form
have created.
the issue have when click on button @ run-time, event handler kicks in , i
out of bounds loop has finished.
a snippet of code below, event handler integrated loop incrementing i
houses switches. way thisobject
equal current active ticker while still keeping event handler in loop? edit: i've provided full class better clarity. section in question follows through action_set>>email>>trigger>>click cases.
public partial class desktoptickers : form { public desktoptickers(list<string> tickerargs) { initializecomponent(); this.opacity = 0; this.showintaskbar = false; int totaltickers = tickerargs.count(); rectangle screenbounds = getdpisaferesolution(); size iconsizedefault = this.size; string icondirectory = @"\\server\tickers\"; string iconsuffix = "_icon.png"; string configdirectory = @"\\server\tickers\"; string configsuffix = ".txt"; char paramdelimiter = ':'; char controldelimiter = '='; char[] controlseparatorl = {'('}; char controlseparatorr = ')'; char actiondelimiter = '='; char[] actionseparatorl = {'('}; char actionseparatorr = ')'; char propertydelimiter = '-'; int maxwidthdefault = iconsizedefault.width; int maxheightdefault = iconsizedefault.height; ticker[] tickers = new ticker[tickerargs.count()]; list<control> controls = new list<control>(); (int = 0; < tickerargs.count(); i++) { string tickerarg = tickerargs[i]; string tickerconfigpath = configdirectory + tickerarg + configsuffix; string tickerresourcepath = icondirectory + @"\" + tickerarg + @"\"; string tickericonpath = tickerresourcepath + tickerarg + iconsuffix; tickers[i] = new ticker(screenbounds, tickerarg, i+1, tickerargs.count(), iconsizedefault, maxheightdefault, maxwidthdefault, tickericonpath); string[] tickerconfigcontents = file.readalllines(tickerconfigpath); (int j = 0; j < tickerconfigcontents.length; j++) { string thisconfigline = tickerconfigcontents[j]; int configparamend = thisconfigline.indexof(paramdelimiter); if (configparamend < 0) { configparamend = 0; } string tickerconfigparam = thisconfigline.substring(0, configparamend); string tickerconfigvalue = thisconfigline.substring(configparamend + 1); switch (tickerconfigparam.tolower()) { //ticker level parameters case "icon_width": tickers[i].iconwidth = convert.toint32(tickerconfigvalue); break; case "icon_height": tickers[i].iconheight = convert.toint32(tickerconfigvalue); break; case "max_width": tickers[i].maxwidth = convert.toint32(tickerconfigvalue); break; case "max_height": tickers[i].maxheight = convert.toint32(tickerconfigvalue); break; case "control_set": (int k = j + 1; k < tickerconfigcontents.length; k++) { //control level parameters string thiscontrolline = tickerconfigcontents[k]; if(thiscontrolline == "end") { break; } int controlparamend = thiscontrolline.indexof(controldelimiter); string thiscontroltype = thiscontrolline.substring(0, controlparamend); string thiscontroldetails = thiscontrolline.substring(controlparamend+ 1); thiscontroldetails = thiscontroldetails.replace(controlseparatorr.tostring(), ""); string[] controlproperties = thiscontroldetails.split(controlseparatorl, stringsplitoptions.removeemptyentries); switch (thiscontroltype.tolower()) { //control type level parameters case "image": picturebox thisimage = new picturebox(); (int l = 0; l < controlproperties.length; l++) { string thisproperty = controlproperties[l]; int propertyparamend = thisproperty.indexof(propertydelimiter); string propertytype = thisproperty.substring(0, propertyparamend - 1); string propertyvalue = thisproperty.substring(propertyparamend + 2); switch (propertytype.tolower()) { //property level parameters case "file": try { thisimage.backgroundimage = image.fromfile(tickerresourcepath + propertyvalue); } catch { thisimage.backgroundimage = thisimage.errorimage; } break; case "bounds": char[] pointdelimiter = { ',' }; string[] boundsvalues = propertyvalue.split(pointdelimiter); rectangle bounds = new rectangle( new point(convert.toint32(boundsvalues[0]), convert.toint32(boundsvalues[1])), new size(convert.toint32(boundsvalues[2]), convert.toint32(boundsvalues[3]))); thisimage.bounds = bounds; break; case "layout": switch(propertyvalue.tolower()) { case "stretch": thisimage.backgroundimagelayout = imagelayout.stretch; break; case "zoom": thisimage.backgroundimagelayout = imagelayout.zoom; break; case "tile": thisimage.backgroundimagelayout = imagelayout.tile; break; case "center": thisimage.backgroundimagelayout = imagelayout.center; break; default: thisimage.backgroundimagelayout = imagelayout.none; break; } break; case "id": thisimage.name = propertyvalue; break; } tickers[i].controls.add(thisimage); thisimage.show(); } break; case "label": label thislabel = new label(); (int l = 0; l < controlproperties.length; l++) { string thisproperty = controlproperties[l]; int propertyparamend = thisproperty.indexof(propertydelimiter); string propertytype = thisproperty.substring(0, propertyparamend - 1); string propertyvalue = thisproperty.substring(propertyparamend + 2); thislabel.borderstyle = system.windows.forms.borderstyle.fixedsingle; switch (propertytype.tolower()) { //property level parameters case "text": thislabel.text = propertyvalue; break; case "font": char fontdelimiter = ','; int fontsplitindex = propertyvalue.indexof(fontdelimiter); string fontname = propertyvalue.substring(0, fontsplitindex); string fontsize = propertyvalue.substring(fontsplitindex + 1); int fontsizenum = int.parse(fontsize); thislabel.font = new font(propertyvalue, fontsizenum); break; case "bounds": char[] pointdelimiter = {','}; string[] boundsvalues = propertyvalue.split(pointdelimiter); rectangle bounds = new rectangle( new point(convert.toint32(boundsvalues[0]), convert.toint32(boundsvalues[1])), new size(convert.toint32(boundsvalues[2]), convert.toint32(boundsvalues[3]))); thislabel.bounds = bounds; break; case "id": thislabel.name = propertyvalue; break; } thislabel.show(); tickers[i].controls.add(thislabel); } break; case "button": button thisbutton = new button(); (int l = 0; l < controlproperties.length; l++) { string thisproperty = controlproperties[l]; int propertyparamend = thisproperty.indexof(propertydelimiter); string propertytype = thisproperty.substring(0, propertyparamend - 1); string propertyvalue = thisproperty.substring(propertyparamend + 2); switch (propertytype.tolower()) { case "text": thisbutton.text = propertyvalue; break; case "font": char fontdelimiter = ','; int fontsplitindex = propertyvalue.indexof(fontdelimiter); string fontname = propertyvalue.substring(0, fontsplitindex); string fontsize = propertyvalue.substring(fontsplitindex + 1); int fontsizenum = int.parse(fontsize); thisbutton.font = new font(propertyvalue, fontsizenum); break; case "bounds": char[] pointdelimiter = { ',' }; string[] boundsvalues = propertyvalue.split(pointdelimiter); rectangle bounds = new rectangle( new point(convert.toint32(boundsvalues[0]), convert.toint32(boundsvalues[1])), new size(convert.toint32(boundsvalues[2]), convert.toint32(boundsvalues[3]))); thisbutton.bounds = bounds; break; case "id": thisbutton.name = propertyvalue; break; } thisbutton.show(); tickers[i].controls.add(thisbutton); } break; case "textbox": textbox thistextbox = new textbox(); (int l = 0; l < controlproperties.length; l++) { string thisproperty = controlproperties[l]; int propertyparamend = thisproperty.indexof(propertydelimiter); string propertytype = thisproperty.substring(0, propertyparamend - 1); string propertyvalue = thisproperty.substring(propertyparamend + 2); thistextbox.borderstyle = system.windows.forms.borderstyle.fixedsingle; switch (propertytype.tolower()) { //property level parameters case "text": thistextbox.text = propertyvalue; break; case "font": char fontdelimiter = ','; int fontsplitindex = propertyvalue.indexof(fontdelimiter); string fontname = propertyvalue.substring(0, fontsplitindex); string fontsize = propertyvalue.substring(fontsplitindex + 1); int fontsizenum = int.parse(fontsize); thistextbox.font = new font(propertyvalue, fontsizenum); break; case "bounds": char[] pointdelimiter = { ',' }; string[] boundsvalues = propertyvalue.split(pointdelimiter); rectangle bounds = new rectangle( new point(convert.toint32(boundsvalues[0]), convert.toint32(boundsvalues[1])), new size(convert.toint32(boundsvalues[2]), convert.toint32(boundsvalues[3]))); thistextbox.bounds = bounds; break; case "id": thistextbox.name = propertyvalue; break; } thistextbox.show(); tickers[i].controls.add(thistextbox); } break; } } break; case "action_set": (int k = j + 1; k < tickerconfigcontents.length; k++) { //action level parameters string thisactionline = tickerconfigcontents[k]; if (thisactionline == "end") { break; } int actionparamend = thisactionline.indexof(actiondelimiter); string thisactiontype = thisactionline.substring(0, actionparamend); string thisactiondetails = thisactionline.substring(actionparamend + 1); thisactiondetails = thisactiondetails.replace(actionseparatorr.tostring(), ""); string[] actionproperties = thisactiondetails.split(actionseparatorl, stringsplitoptions.removeemptyentries); control thisobject = new control(); switch (thisactiontype.tolower()) { //action type level parameters case "email": //email requires trigger, objectid, action send email, email action params (int l = 0; l < actionproperties.length; l++) { string thisproperty = actionproperties[l]; int propertyparamend = thisproperty.indexof(propertydelimiter); string propertytype = thisproperty.substring(0, propertyparamend - 1); string propertyvalue = thisproperty.substring(propertyparamend + 2); string emaildomain = ""; string emailserver = ""; int emailport = 0; string emailtemplate = ""; string emailrecipient = ""; switch (propertytype.tolower()) { case "domain": emaildomain = propertyvalue; break; case "server": emailserver = propertyvalue; break; case "port": emailport = convert.toint32(propertyvalue); break; case "file": emailtemplate = tickerresourcepath + propertyvalue; break; case "recipient": emailrecipient = propertyvalue; break; case "object": thisobject = tickers[i].controls.find(propertyvalue, false).firstordefault() control; //thisobject = objects[0]; break; case "trigger": tickers[i].setemailproperties(emaildomain, emailserver, emailport, emailtemplate, emailrecipient); switch(propertyvalue.tolower()) { case "click": thisobject.mousedown += new mouseeventhandler((sender, e) => tickers[i].sendemail_event(sender, e)); break; } break; } } break; } } break; } } tickers[i].show(); } } private rectangle getdpisaferesolution() { using (graphics graphics = this.creategraphics()) { return new rectangle(new point(0, 0), new size((screen.primaryscreen.bounds.width * (int)graphics.dpix) / 96 , (screen.primaryscreen.bounds.height * (int)graphics.dpiy) / 96)); } } }
this seems capture issue. need capture value of i
when form created , keep value when create event handler. can done creating new variable ivalue
or tickerind
while creating form , using variable rather i
in event handler code.
i writing answer little bit speculatively. please provide larger code snippet allow see creation code form , handler. believe current code follows. part marked "code based on i" code snippet have provided.
for (var = 0; < formcount; ++i) { var form = new ticker(); form.button.onclicked += () => { //code based on dosomething(i); }; }
it needs follows:
for (var = 0; < formcount; ++i) { var formind = i; var form = new ticker(); form.button.onclicked += () => { //code based on dosomething(formind); }; }
edit: need change following code:
case "click": //replace tickerind, capture current value of var tickerind = i; thisobject.mousedown += new mouseeventhandler((sender, e) => tickers[tickerind].sendemail_event(sender, e)); break;
note: code seems need refactoring. first of all, don't need ticker[] tickers
, can following:
public desktoptickers(list<string> tickerargs) { (var = 0; < tickerargs.count; ++i) { var tickerarg = tickerargs[i]; var ticker = new ticker(...); //your whole initialization code goes here //replace "tickers[i]" "ticker" ticker.show(); } }
afterwards, can further refactoring moving initialization ticker
constructor , further divide initializtion methods.
Comments
Post a Comment