arrays - Stopwatch changing button text in c# -
basically, i've got multiple button in form, , want show stopwatch in button.text when button pressed. (button modified toggle button.) , stop , reset timmer when button toggled off. simple enough seemed because have multiple buttons pressed in order, , don't know threading, seems more difficult presumed.
my origional intent have function runs every second , interates interager if button pressed using code:
public void jogger()//purpose step time[0] every second when button on. { while (true) { (int = 0; < 16; i++) { if (btnstat[i]) time[i]++; } thread.sleep(1000); } }
problem is, don't know threading when call function, stuck doing , this.
either way, once called, call update function updates buttons including button.text displays time[0]; (array built around buttons)
is better way of doing doesn't cause cpu use and/or works?
thanks help! -john ivey
assuming using checkbox property button = appearence, in event handler checkedchanged:
private void checkboxcheckedchanged(object sender, eventargs e) { checkbox checkbox = (checkbox) sender; if (checkbox.checked) { timer timer = new timer {interval = 1000}; timer.tick += jogger; timer.start(); timer.tag = new checkboxcounter {checkbox = checkbox, time = 0}; checkbox.tag = timer; } else { timer timer = checkbox.tag timer; if (timer != null) { timer.tag = null; timer.stop(); timer.dispose(); checkbox.tag = null; } } }
change jogger function:
private void jogger(object a_sender, eventargs a_eventargs) { timer timer = (timer) a_sender; checkboxcounter data = (checkboxcounter)timer.tag; data.time++; data.checkbox.text = data.time.tostring(); }
you need simple class store checkbox , current time:
class checkboxcounter { public checkbox checkbox; public int time; }
then can add number of checkboxes , set event checkedchanged checkboxcheckedchanged.
Comments
Post a Comment