Complete a simple time reminder with Visual C # .NET
Some people will forget the time when I use a computer, so I want to be a gadget to achieve the function of the alarm.
First of all, to design the user interface, you want to use it, so don't have much thing, two TextBox, two button, and a few to display the text prompt Lable, you can!
After the control is arranged, you can write code. One of the TEXTBOXs (TextBox1 in the code) is the prompt information to be displayed in the future. Another TextBox is set time (TextBox2 in the code). The format of the TEXTBOX for setting the time is "00:00:00", so there are many qualifications, such as only entering numbers, and two colons cannot be modified. I designed the SimpleTextBox class to limit the input of time. SimpleTextBox class code is as follows:
Document: SimpleTextBox.cs
Using system;
Using system.collections;
Using system.componentmodel;
Using system.drawing;
Using system.data;
Using system.windows.forms;
Namespace SimpleTextBox
{
Public Class SimpleTextBox: System.Windows.Forms.TextBox
{
Private string m_format; // Set the display format of the time
Private char m_inpchar; // default characters
Public SimpleTextBox ()
{
Base.Multiline = false;
Base.maxlength = 8;
m_inpchar = '0';
M_Format = "
00:00:00
"
}
Private bool isvalidchar (charinput)
{
Return (Char.IsDigit (Input)); / / Check if it is a number
}
// Removal of the TEXTBOX onKeyPress method
Protected Override Void OnkeyPress (KeypressEventArgs E)
{
INT strat = base.selectionstart;
Int len = base.selectionLength;
Int P;
// Handling the Backspace button -> Replace the deleted place with default characters
IF (E.Keychar == 0x08)
{
String s = base.text;
P = prev (STRT);
IF (p! = STRT)
{
Base.text = s.substring (0, p) m_inpchar.tostring () S.Substring (p 1);
Base.SelectionStart = P;
Base.selectionLength = 1;
}
e.handled = true;
Return;
}
// Initial display
IF (M_Format [STRT]! = m_inpchar)
{
Strt = next (-1);
Len = 1;
}
// Accept the input of the keyboard
IF (IsValidchar (E.KEYCHAR))
{
String t = "";
T = base.text.substring (0, start); T = E.Keychar.toString ();
IF (strt g! = base.maxlength)
{
T = m_Format.Substring (strt 1, len - 1);
T = Base.text.substring (start len);
}
Else
T = m_Format.Substring (strt 1);
Base.text = t;
// Next input character
Strt = next (strt);
Base.SelectionStart = STRT;
// m_caret = STRT;
Base.selectionLength = 1;
}
e.handled = true;
}
// Try the cursor forward
Private Int Prev (int StartPOS)
{
Int strat = startpos;
int RET = STRT;
While (Strt> 0)
{
STRT -;
IF (m_format [strt] == m_inpchar)
Return strt;
}
Return Ret;
}
/ / Return the cursor backward, return the position of the next character
Private int next (int StartPOS)
{
Int strat = startpos;
int RET = STRT;
While (strt { STRT ; IF (m_format [strt] == m_inpchar) Return strt; } Return Ret; } / / Re-load the ONMOUSEUP event Protected Override Void OnMouseUp (MouseEventArgs E) { INT strat = base.selectionstart; Int Orig = STRT; Int len = base.selectionLength; // Reset the start position IF (strt == base.maxlength || m_format [strt]! = m_inpchar) { // RESET START IF (next (strt) == STRT) STRT = Prev (STRT); Else Strt = next (strt); Base.SelectionStart = STRT; } // Reset the selected length IF (Len <1) Base.selectionLength = 1; Else IF (m_format [orig g - 1]! = m_inpchar) { LEN = Next (strt len) - (strt len); Base.SelectionLength = LEN; } Base.onmouseUp (e); } } } You can compile this class into a control, you can continue to use later. Below is the Timeralarm class, this class uses a Timer class for timing and prompts when time. code show as below: Using system; Using system.windows.forms; Using system.threading; Using system.timers; Public Class TimerarmM { Private int clocktime = 0; Private int alarmtime = 0; Private string message = "time is here"; Private system.timers.timer TimerClock = new system.timers.timer (); Public int alarmtime { set { AlarmTime = Value; } } Public int clocktime { set { ClockTime = Value; } } Public String Message { set { Message = value; } } Public int countdown { get { Return AlarmTime - ClockTime; } } Public Timerarm () { //Messagebox.show( "timeAlarm Start. "); TimerClock.elapsed = New ElapseDeventHandler (ONTIMER); TimerClock.Interval = 1000; TimerClock.enabled = true; } Public Void Ontimer (Object Source, ElapSedEventArgs E) { Try { ClockTime ; IF (clocktime == alarmtime) { Messagebox.show (Message, "time", MessageBoxButtons.ok, MessageBoxicon.warning; } } Catch (Exception EX) { Messagebox.show ("Ontimer ():" EX.MESSAGE); } } Public void stoptimer () { TimerClock.enabled = false; } } The FormatconvertConvert class is then used, which provides two static methods, and INPUTTOSECONDS () converts a String type time string into a total of a total. Public Static Int Inputtoseconds (String TimerInput) { String [] TimeArray = New String [3]; INT minutes = 0; INT Hours = 0; INT seconds = 0; INT occupurence = 0; INT length = 0; INT TOTALTIME = 0; Occurence = TimerInput.lastIndexof (":"); Length = TimerInput.Length; // Check for Invalid INPUT IF (Occurence == -1 || Length! = 8) { MessageBox.show ("INVALID TIME FORMAT."); } Else { TimeArray = TimerInput.split (':'); Seconds = Convert.Toint32 (TimeArray [2]); Minutes = Convert.Toint32 (TimeArray [1]); Hours = Convert.Toint32 (TimeArray [0]); TOTALTIME = Seconds; TOTALTIME = Minutes * 60; TOTALTIME = (Hours * 60) * 60; } Return TotalTime; } The SecondStotime method is to return the second to a string of time format returns. Public Static String SecondStotime (int SECONDS) { INT minutes = 0; INT Hours = 0; While (Seconds> = 60) { MINUTES = 1; SECONDS - = 60; } While (minutes> = 60) { Hours = 1; Minutes - = 60; } String strhouse = hours.tostring (); String strminutes = minutes.toString (); String strseconds = seconds.toString (); IF (strhours.length <2) Strhours = "0" strhours; IF (strMinutes.length <2) STRMINUTES = "0" STRMINUTES; IF (Strseconds.Length <2) STRSECONDS = "0" strseconds; Return Strhours : " Strminutes ": " strseconds; } Below is the main form, write two buttons, respectively: Start button: Private void Button1_Click (Object Sender, System.Eventargs E) { Timealarm = new timelarm (); TimeAlarm.AlarmTime = formatconvert.inputtoseconds (this.textbox2.text); TimeAlarm.Message = this.TextBox1.text; IF (Timealarm.countDown> 0) THIS.TIMER1.ENABED = TRUE; IF (TextBox2.text! = " 00:00:00 ") THIS.TEXTBOX2.Readonly = true; } Create an instance of Timeralarm and start timing. Reset button: Private void button2_click (Object Sender, System.Eventargs E) { THIS.TEXTBOX1.CLEAR (); this.timer1.enabled = false; IF (TimeAlarm! = NULL) TimeAlarm.Stoptimer (); THIS.TEXTBOX2.Readonly = false; this.TextBox2.text = " 00:00:00 " } Restore procedure. There is also a Timer that dynamically displays the remaining time. Private void Timer1_Tick (Object Sender, System.EventArgs E) { IF (TimeAlarm.countDown> = 0) textBox2.text = formatconvert.secondstotime (TimeAlarm.countdown); Else { this.timer1.enabled = false; THIS.TEXTBOX2.Readonly = false; } } The program is basically completed. Interested friends can also join the functionality of the system tray. For the first time, write this article, time is also more rush, I hope everyone will give more comments. (Reference: c # maskedidit control ---- Oscar Bowyer, Use a Timer to create a Simple alarm Application ---- Andrew Boisen, from Codiproject.com)