Timer Visual C++ 2008

0

Witam :)
chciałem zrobić zegar który bd odliczał czas do tyłu :)
I napisałem coś takiego :P

 
private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) {
			 label6->Text = min+":"+sec;

			 if(sec == 1){label6->Text = min+":0"+sec;}
			 if(sec == 2){label6->Text = min+":0"+sec;}
			 if(sec == 3){label6->Text = min+":0"+sec;}
			 if(sec == 4){label6->Text = min+":0"+sec;}
			 if(sec == 5){label6->Text = min+":0"+sec;}
			 if(sec == 6){label6->Text = min+":0"+sec;}
			 if(sec == 7){label6->Text = min+":0"+sec;}
			 if(sec == 8){label6->Text = min+":0"+sec;}
			 if(sec == 9){label6->Text = min+":0"+sec;}	 
			 if(sec == 60){label6->Text = min+":00";}
																
			 if(min == 1){label6->Text = "0"+min+":"+sec;}
			 if(min == 2){label6->Text = "0"+min+":"+sec;}
			 if(min == 3){label6->Text = "0"+min+":"+sec;}
			 if(min == 4){label6->Text = "0"+min+":"+sec;}
			 if(min == 5){label6->Text = "0"+min+":"+sec;}
			 if(min == 6){label6->Text = "0"+min+":"+sec;}
			 if(min == 7){label6->Text = "0"+min+":"+sec;}
			 if(min == 8){label6->Text = "0"+min+":"+sec;}
			 if(min == 9){label6->Text = "0"+min+":"+sec;}

			 sec--;
			 if(sec == 0)
			 {
				 sec = 60;
				 min--;
			 }
			 if(min == 0, sec == 0)
			 {
				 Close();

I mam problem, jest ok dopóki czas nie zejdzie poniżej 10min np. zamiast wyświetlać 09:09 to wyświetla 09:9
i jeszcze jak czas dojdzie do 00:00 to program się nie wyłącza tylko czas jest na minusie :P

0

O nierównościach słyszałeś? Może zamiast porównywać sekundę z każdą cyfrą wystarczy sprawdzić, czy jest mniejsza od 10?

 if(min == 0, sec == 0)

Tu nie powinien być przecinek, tylko operator koniunkcji logicznej (&&).

Czas źle się wyświetla, bo po przeanalizowaniu sekund analizujesz minuty i nadpisujesz zawartość formatki. Najlepiej wpisuj po fragmencie.

0

label6->Text = min+":"+sec;
po co to jest, skoro potem zastępujesz Text inną treścią?

0

if(sec == 60){label6->Text = min+":00";}
sztuczny zabieg aby uniknąć wyświetlenia bezsensownych danych, ale i tak nie do końca bo ci się wyświetli:
10:02 10:01 09:00 09:59 09:58
zamiast zrobić po bożemu:

if((--sec)<0)
   {
    sec=59;
    if((--min)<0) Close();
   }

I postawić to przed wyświetleniem:

if(sec == 2){label6->Text = min+":0"+sec;}
if(sec == 3){label6->Text = min+":0"+sec;}
Szereg if'ów bez else, owszem tu jest mało istotne ale stosuj else tam gdzie to ma sens:

...
else if(sec == 2){label6->Text = min+":0"+sec;}
else if(sec == 3){label6->Text = min+":0"+sec;}
else ...

Tyle zachodu aby wyświetlić w formacie 00:00 !?
czemu nie zrobić tak:

char buf[6];
sprintf(buf,"%2d:%2d",min,sec);
label6->Text = buf;

lub tak:

ostringstream s;
s<<setw(2)<<min<<':'<<setw(2)<<sec;
label6->Text = s.str();

lub przynajmniej użyć String.Format:
http://msdn.microsoft.com/en-us/library/aa331875%28v=vs.71%29.aspx

Horror!

0

Zainteresuj się strukturą TimeSpan

1 użytkowników online, w tym zalogowanych: 0, gości: 1