WPF C# Checkbox controlling a timer -
i'm stuck here. have checkbox. when checked, start timer. when unchecked stop timer. can use here. have tried checked, unchecked, , click events. nothing stopping timer. keeps running...
xaml: (has 3 events show)
<checkbox x:name="cbautorefresh" grid.row="1" cliptobounds="true" horizontalalignment="left" content="enable auto refresh" margin="10,0,0,0" width="150" click="cbautorefresh_click" checked="cbautorefresh_checked" unchecked="cbautorefresh_unchecked" />
c#: (all 3 attempts)
private void cbautorefresh_click(object sender, routedeventargs e) { var atimer = new timer(); if (cbautorefresh.ischecked == true) { //start timer: atimer.elapsed += ontimedevent; atimer.interval = 60000; atimer.enabled = true; } else { atimer.enabled = false; } } private void cbautorefresh_checked(object sender, routedeventargs e) { //start timer: var atimer = new timer(); atimer.elapsed += ontimedevent; atimer.interval = 60000; atimer.enabled = true; } private void cbautorefresh_unchecked(object sender, routedeventargs e) { var atimer = new timer {enabled = false}; }
i tried this, mention @ stack overflow
<checkbox checked="checkboxchanged" unchecked="checkboxchanged"/> private void checkboxchanged(object sender, routedeventargs e) { messagebox.show("eureka, changed!"); }
don't create new timer
on every event. declare 1 timer in window
object (window field/property), initialize in constructor , work in event handlers. @ moment running multiple timers
<checkbox x:name="cbautorefresh" checked="checkboxchanged" unchecked="checkboxchanged"/>
public class mywindow() { private timer _t; public mywindow() { initializecomponent(); _t = new timer(); _t.elapsed += ontimedevent; _t.interval = 60000; } private void checkboxchanged(object sender, routedeventargs e) { _t.enabled = cbautorefresh.ischecked; } }
Comments
Post a Comment