c# - Iterating through alphabet and get newly created strings back to their original Button Form -
after long search found winning strategy clean code on hangman game, explanation needed understand.
when game opens, wanted letter buttons disabled. created method that,
private void disableletters(); { a.enabled = false; b.enabled = false; . . . z.enabled = false; }
effective not elegant. iterating through alphabet try , condense code seemed logical.
private void disableletters() { char alphebetstart = char.parse("a"); char alphabetend = char.parse("z"); (i = alphabetstart; <= alphabetend; i++); { string allletters = i.tostring(); } }
but challenge appeared starting because couldn't use these newly created strings button forms this,
i.tostring().enabled = false;
for obvious reasons. strings needed understood original form.
the long search revealed solution, don't understand quite it's doing. magic was,
controls[allletters].enabled = false;
the "controls" reveals cryptic, "gets collection of controls contained within control." code works planned, looks it's doing saying, "take new strings created thru z , understand them name of buttons."
if briefly explain code doing, i'd grateful.
your final code in no way related variables. while there variable a
represents control, there entirely separate collection within form has of control
objects within it. each of control object has name
field, , 1 of them has string value of "a"
. can find object in collection name value of "a"
enough. there happens variable called a
references control (from point of view of computer) coincidence. if renamed variable (but didn't change name of control) "a1"
, you'd still need use "a"
control using controls
collection.
Comments
Post a Comment