Use VisualC #. net to complete a time daemon

Source: Internet
Author: User

Some people forget the time when they use their computers, so I want to make a small tool to implement the alarm function.

First of all, we need to design the user interface and introduce the ease of use, so we don't have to worry too much about anything. We need two TextBox, two buttons, and a few labels to display text prompts!
  
After you have configured the control, you can write the code. One TextBox (textBox1 in the Code) is the prompt information to be displayed after the input time is reached. The other TextBox sets the time (textBox2 in the Code ). The TextBox format for setting the time is "00:00:00", so there are many limits. For example, you can only enter numbers, and the two colons cannot be modified. I designed the SimpleTextBox class to restrict the input of time. The SimpleTextBox class code is as follows:
  
File: SimpleTextBox. cs
  
UsingSystem;
  
UsingSystem. Collections;
  
UsingSystem. ComponentModel;
  
UsingSystem. Drawing;
  
UsingSystem. Data;
  
UsingSystem. Windows. Forms;
  
NamespaceSimpleTextBox
  
{
  
PublicclassSimpleTextBox: System. Windows. Forms. TextBox
  
{
  
Privatestringm_format; // set the display format of the time.
  
Privatecharm_inpChar; // default character
  
PublicSimpleTextBox ()

{

Base. Multiline = false;
  
Base. MaxLength = 8;
  
M_inpChar = 0;
  
M_format = "00:00:00 ";
  
}
  
PrivateboolIsValidChar (charinput)
  
{
  
Return (char. IsDigit (input); // check whether it is a number
  
}
  
  File: // heavyOnKeyPress Method for carrying TextBox
  
Protectedoverrisponidonkeypress (KeyPressEventArgse)
  
{
  
Intstrt = base. SelectionStart;
  
Intlen = base. SelectionLength;
  
Intp;
  
  File: //Backspace key-> use the default character to replace the deleted location
  
If (e. KeyChar = 0x08)
  
{
  
Strings = 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;
  
}
  
  File: //Start display
  
If (m_format [strt]! = M_inpChar)
  
{
  
Strt = Next (-1 );
  
Len = 1;
  
}
  
  File: // connectKeyboard Input
  
If (IsValidChar (e. KeyChar ))
  
{
  
Stringt = "";
  
T = base. Text. Substring (0, strt );
  
T + = e. KeyChar. ToString ();
  
If (strt + len! = Base. MaxLength)
  
{
  
T + = m_format.Substring (strt + 1, len-1 );
  
T + = base. Text. Substring (strt + len );
  
}

Else
  
T + = m_format.Substring (strt + 1 );
  
Base. Text = t;
  
  File: //One input character
  
Strt = Next (strt );

Base. SelectionStart = strt;
  
  File: // m_caret = strt;
  
Base. SelectionLength = 1;
  
}
  
E. Handled = true;
  
}
  
  File: // setForward cursor Detection
  
PrivateintPrev (intstartPos)
  
{
  
Intstrt = startPos;
  
Intret = strt;
  
While (strt> 0)
  
{
  
Strt --;
  
If (m_format [strt] = m_inpChar)
  
Returnstrt;
  
}
  
Returnret;
  
}


  File: // setCursor backward detection, returns the position of the next character
  
PrivateintNext (intstartPos)
  
{
  
Intstrt = startPos;
  
Intret = strt;
  
While (strt
{
  
Strt ++;
  
If (m_format [strt] = m_inpChar)
  
Returnstrt;
  
}
  
Returnret;
  
}
  
  File: // heavyOnMouseUp event
  
Protectedoverrisponidonmouseup (MouseEventArgse)
  
{
  
Intstrt = base. SelectionStart;
  
Intorig = strt;
  
Intlen = base. SelectionLength;
  
  File: // heavySet the start position
  
If (strt = base. MaxLength | m_format [strt]! = M_inpChar)
  
{
  
  File: // resetstart
  
If (Next (strt) = strt)
  
Strt = Prev (strt );
  
Else
  
Strt = Next (strt );
  
Base. SelectionStart = strt;

}
  
  File: // heavySet the Selected length
  
If (len <1)

Base. SelectionLength = 1;
  
Elseif (m_format [orig + len-1]! = M_inpChar)
  
{
  
Len + = Next (strt + len)-(strt + len );
  
Base. SelectionLengthlen;
  
}
  
Base. onMouseUp (e );
  
}
  
}
  
}
  
You can compile this class as a control and continue to use it later.
  
The following is the TimerAlarm class, which uses a Timer class for timing and sends a prompt when the time is reached. The Code is as follows:
  
UsingSystem;
  
UsingSystem. Windows. Forms;
  
UsingSystem. Threading;
  
UsingSystem. Timers;

PublicclassTimerAlarm
  
{


PrivateintclockTime = 0;
  
PrivateintalarmTime = 0;
  
Privatestringmessage = "time reached ";
  
PrivateSystem. Timers. TimertimerClock = newSystem. Timers. Timer ();

PublicintAlarmTime

{

Set
  
{
  
AlarmTime = value;
  
}
  
}
  
PublicintClockTime
  
{
  
Set
  
{
  
ClockTime = value;
  
}
  
}
  
PublicstringMessage
  
{
  
Set
  
{
  
Message = value;
  
}
  
}
  
  
  
PublicintCountdown
  
{
  
Get
  
{
  
ReturnalarmTime-clockTime;
  
}
  
}
  
PublicTimerAlarm ()
  
{
  
  File: // MessageBox. Show("TimeAlarmstart .");
  
TimerClock. Elapsed + = newElapsedEventHandler (OnTimer );
  
TimerClock. Interval = 1000;
  
TimerClock. Enabled = true;
  
}
  
PublicvoidOnTimer (Objectsource, ElapsedEventArgse)
  
{
  
Try
  
{
  
ClockTime ++;
  
If (clockTime = alarmTime)
  
{
  
MessageBox. Show (message, "time reached", MessageBoxButtons. OK, MessageBoxIcon. Warning );
  
}
    
}


Catch (Exceptionex)
  
{
  
MessageBox. Show ("OnTimer ():" + ex. Message );
  
}

}
  
PublicvoidStopTimer ()
  
{
  
TimerClock. Enabled = false;
  
}
  
}
  
Then, the FormatConvert class is used. It provides two static methods. inputToSeconds () converts a string-type time string to a total of seconds.
  
PublicstaticintinputToSeconds (stringtimerInput)
  
{
  
String [] timeArray = newstring [3];
  
Intminutes = 0;
  
Inthours = 0;
  
Intseconds = 0;
  
Required ccurence = 0;
  
Intlength = 0;
  
InttotalTime = 0;
  
Occurence = timerInput. LastIndexOf (":");
  
Length = timerInput. Length;
  
  File: // Checkforinvalidinput
  
If (occurence =-1 | length! = 8)
  
{
  
MessageBox. Show ("Inval

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.