em muốn khi gọi nhiều ô textbox ví dụ : gọi ra 4 ô text box thì ô text box thứ 4 là tổng giá trị thời gian của 3 ô textbox còn lại thông qua nút Enter, và ô thứ 4 hiển thị tổng theo giờ mong mọi người giúp đỡ ạ
using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace GetSumTimeControl
{
public class GetSumTime : TextBox
{
//Check typed character and avoid illegal character.
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);
//Save the old states.
string txt = this.Text;
int selectStart = this.SelectionStart;
int selectLen = this.SelectionLength;
if (Char.IsControl(e.KeyChar) == false)
{
//Cope with the common char, such as A,B,C,!,1,2
txt = txt.Remove(selectStart, selectLen);
txt = txt.Insert(selectStart, e.KeyChar.ToString());
selectStart += 1;
}
else
{
//Cope with control key.
Keys cmdKey = (Keys)e.KeyChar;
if (cmdKey == Keys.Delete)
{
if (selectLen == 0 && selectStart < txt.Length)
txt = txt.Remove(selectStart + 1);
else
txt = txt.Remove(selectStart, selectLen);
}
else if (cmdKey == Keys.Back)
{
if (selectLen == 0 && selectStart > 0)
{
txt = txt.Remove(selectStart - 1);
selectStart -= 1;
}
else
txt = txt.Remove(selectStart, selectLen);
}
}
if (ValidText(txt))
{
//Reset the text and states.
this.Text = txt.ToUpper();
this.SelectionStart = selectStart;
}
e.Handled = true;
}
//Check if the typed character would generate a legal text
private bool ValidText(string txt)
{
bool isLegal = true;
for (int i = 0; i < txt.Length; i++)
{
char c = txt[i];
bool isCharLegal = false;
switch (i)
{
case 0:
{
//The first character must be 0 or 1
if (c == '0' || c == '1')
isCharLegal = true;
break;
}
case 1:
{
//The second character depends on the first character
//If the first is 0, this character can range from 1 to 9,
//else it must be 1 or 2
if ((txt[0] == '0' && (c >= '1' && c <= '9'))
|| ((txt[0] == '1') && (c == '1' || c == '2')))
isCharLegal = true;
break;
}
case 2:
{
//The third character must be ':'
if (c == ':')
isCharLegal = true;
break;
}
case 3:
{
//The fourth character must range from 0 to 5,
//because minites must range from 0 to 59
if (c >= '0' && c <= '5')
isCharLegal = true;
break;
}
case 4:
{
//The fifth character can be any digit.
if (Char.IsDigit(c))
isCharLegal = true;
break;
}
//case 5:
// {
// //The sixth character must be a blank to split the time and AM/PM
// if (Char.IsWhiteSpace(c))
// isCharLegal = true;
// break;
// }
//case 6:
// {
// //The seventh character can be A or P, a lower character is also legal.
// if (c == 'A' || c == 'P'
// || c == 'a' || c == 'p')
// isCharLegal = true;
// break;
// }
//case 7:
// {
// //The eighth character can be M, a lower character is also legal.
// if (c == 'M' || c == 'm')
// isCharLegal = true;
// break;
// }
default:
break;
}
if (isCharLegal == false)
{
//If one character is illegal, the string is illegal.
isLegal = false;
break;
}
}
return isLegal;
}
private void GetSumTime_KeyPress(object sender, KeyPressEventArgs e)
{
if (Text.Length >= 5)
{
e.Handled = true;
}
//if (e.KeyChar == (char)Keys.Enter )
//{
// for (int i = 0; i < 4; ++i)
// {
// string name = "GetSumTime" + i;
// if (Name != name)
// {
// Text = PairingBox(Text[0], Text[1], Text[3], Text[4]).ToString();
// }
// }
//}
}
//Total hours and minutes in one TextBox
public decimal PairingBox(char txt1, char txt2, char txt3, char txt4)
{
decimal numfirst = Convert.ToDecimal(txt1.ToString() + txt2.ToString());
decimal numlast = ((Convert.ToDecimal(txt3.ToString() + txt4.ToString()))/60);
decimal Sumtime = numfirst + numlast;
return Sumtime;
}
private void GetSumTime_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
for (int i = 0; i < 4; ++i)
{
string name = "GetSumTime" + i;
if (Name != name)
{
Text = PairingBox(Text[0], Text[1], Text[3], Text[4]).ToString();
}
}
}
}
}
}