c# - Checking if the correct label was clicked? -
to start of im not @ programming , new it. said, trying make game, pattern of labels show (by speciffic labels changing colors), , user has click speciffic pattern after has been shown. have made pattern show, , put list. problem have how going check if correct label licked, acording random pattern has been made. sorry if code seems clumsy, here (sorry there no commemts yet also):
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading; using system.threading.tasks; using system.windows.forms; namespace spil { public partial class form1 : form { random rnd = new random(); label[] labelarray; int turn = 1; int lives = 3; list<label> orderlist = new list<label>(); public form1() { initializecomponent(); labelarray = new label []{ label1, label2, label3, label4, label5, label6, label7, label8, label9 }; } private void displayorder() { (int = 0; < labelarray.length; i++) { labelarray[i].backcolor = color.blue; } (int = -2; < turn; i++) { int chosennumber = rnd.next(0, 9); labelarray[chosennumber].backcolor = color.green; thread.sleep(1000); labelarray[chosennumber].backcolor = color.blue; orderlist.add(labelarray[chosennumber]); } } private void click0(object sender, eventargs e) { } private void click1(object sender, eventargs e) { } private void click2(object sender, eventargs e) { } private void click3(object sender, eventargs e) { } private void click4(object sender, eventargs e) { } private void click5(object sender, eventargs e) { } private void click6(object sender, eventargs e) { } private void click7(object sender, eventargs e) { } private void click8(object sender, eventargs e) { } private void click9(object sender, eventargs e) { } private void form1_shown(object sender, eventargs e) { system.timers.timer t = new system.timers.timer(100); t.elapsed += t_elapsed; t.start(); } void t_elapsed(object sender, system.timers.elapsedeventargs e) { ((system.timers.timer)sender).stop(); displayorder(); } } }
you can have labels registered same click event , use sender parameter identify clicked label.
for (int = 0; < labelarray.length; i++) { labelarray[i].backcolor = color.blue; labelarray[i].click += label_click; } void label_click(object sender, eventargs e) { string name = ((label)sender).name; }
Comments
Post a Comment