c# - How to choose when to loop through listbox? -
i have listbox of items want go through 1 one , each item should printed in messagebox. want default, when reaches end of listbox, stop. if check checkbox "loop through list [x]" want continue , start on when reaches bottom. how can that?
this code right loop through it.
int = 0; = listbox1.selectedindex; = + 1; if (i > listbox1.items.count - 1) = 0; listbox1.selectedindex = i; messagebox.show("item: " + listbox1.selecteditem.tostring()); this never ends though, continues loop. how can make check if it's on bottom stops? should code if checked box "loop through list [x]"...
this in timer1 interval 5000ms
edit: also, how can make print "you have reached end of list!" when comes bottom of listbox?
you said in "timer1", means code looks like:
private void timerelapsed(...) { int = 0; = listbox1.selectedindex; = + 1; if (i > listbox1.items.count - 1) = 0; listbox1.selectedindex = i; messagebox.show("item: " + listbox1.selecteditem.tostring()); } when reach end, set = 0, allows loop continue without throwing. stop loop, need stop timer invoking function well.
the correct code be:
private void timerelapsed(...) { int = 0; = listbox1.selectedindex; i++; if (i > listbox1.items.count - 1) { messagebox.show("end of list reached!"); if (loopafterend) = 0; else timer1.stop(); } listbox1.selectedindex = i; messagebox.show("item: " + listbox1.selecteditem.tostring()); } it written:
private void timerelapsed(...) { int = 0; = listbox1.selectedindex; i++; if (i > listbox1.items.count - 1) { messagebox.show("end of list reached!"); = 0; if (!loopafterend) timer1.stop(); } listbox1.selectedindex = i; messagebox.show("item: " + listbox1.selecteditem.tostring()); } which has advantage of letting restart loop later starting timer again!
Comments
Post a Comment