الأخوة الكرام السلام عليكم ورحمة الله
قد قمت بإضافة الدرس التاسع إلى الموقع
لمعرفة المزيد الرجاء زيارة رابط الدرس 9
http://bashir.taibat.com/cl9.php
كما قمت بإرفاق ملف التنفيذ مع هذه الرسالة الرجاء التأكد من صحة البرنامج
وإذا كانت هنالك أي ملاحظات فأنا على أتم الاستعداد بقدر معرفتي
ولكم مني خالص الشكر والتقدير
// كتابة الحزم الإفتراضية
using System;//لاستخدام الأوامر الخاصة بالنظام
using System.Timers;//وهذه الحزمة خاصة بالوقت والمنبه وهذه أول مرة نستخدمها في
هذه الدروس
using System.Drawing;//خاص بالطباعة على الشاشة
using System.Windows.Forms;//خاص بعناصر الويندوز كالقوائم ومربع التحرير
/********************
إطار Constants
خاص بكتابة الثوابت
وهذا إطار أنشأناه لتسهيل كتابة الثوابت والأوامر
********************/
static class Constants
{
public const String ApplicationName = "حساب الوقت"; // اسم البرنامج
public const int TimeUp = 3600 *10; // تعيين الزمن الأقصى الذي يمكن حسابه
والقيمة المستخدمة بالثانية أي ساعة في 10
public const String TextBoxFontName = "MS Gothic"; // نوع حرف مربع التحرير
public const int TextBoxFontSize = 31; // حجم حرف مربع التحرير
public const int TextBoxMaxLength = 10; // عدد ما يمكن إدخاله في مربع التحرير
public const int TextBoxWidth = 300; // عرض مربع التحرير
public const int TextBoxHeight = 50; // إرتفاع مربع التحرير
public const String TextBoxHourLength = "ساعة"; // ما يكتب بعد عدد الساعات
public const String TextBoxMinuteLength = "دقيقة"; // ما يكتب بعد عدد الدقائق
public const String TextBoxSecondLength = "ثانية"; // ما يكتب بعد عدد الثواني
public const String ButtonFontName = "MS UI Gothic"; // نوع حرف الأزرار
public const int ButtonFontSize = 30; // حجم حرف الأزرار
public const int ButtonWidth = 150; // عرض الأزرار
public const int ButtonHeight = TextBoxHeight; // إرتفاع الأزرار
public const String StartButtonLength = "تشغيل"; // اسم زر التشغيل
public const String StopButtonLength = "إيقاف"; // اسم زر الإيقاف
}
/********************
إطار test
ويبدأ تنفيذ البرنامج من هذه النقطة
********************/
class test
{
public static void Main()
{
Application.Run(new Form1());
}
}
/********************
إطار Form1
********************/
class Form1 : Form
{
private bool bCountFlag = false; // يتم تفعيله فقط عند الضغط زر التشغيل
private int nCounter = 0; // يتم زيادة الأرقام بعد كل ثانية
private System.Timers.Timer timer;
private TextBox textbox;
private Button button;
/* تطبيق الأوامر الأولية */
public Form1()
{
SetTimer();// حزمة لتحديد الوقت
FormDesign();//حزمة لإنشاء العناصر المختلفة
}
/* حزمة SetTimer*/
/* إعداد الموقت */
public void SetTimer()
{
timer = new System.Timers.Timer();
timer.Elapsed += new ElapsedEventHandler(MyTimer);
timer.Interval = 1000; // ألف جزء من الثانية
timer.AutoReset = true;
}
/* حزمة FormDesign */
/* إعداد النافذة */
public void FormDesign()
{
StartPosition = FormStartPosition.CenterScreen;
MaximizeBox = false; // تعطيل زر تكبير الشاشة
FormBorderStyle = FormBorderStyle.Fixed3D; // تعطيل خاصية تغيير حجم الشاشة
Text = Constants.ApplicationName;
BackColor = SystemColors.Window;
ClientSize = new Size(Constants.TextBoxWidth + Constants.ButtonWidth,
Constants.ButtonHeight);
// إنشاء مربع التحرير
textbox = new TextBox();
textbox.Font = new Font(Constants.TextBoxFontName, Constants.TextBoxFontSize);
textbox.Width = Constants.TextBoxWidth;
textbox.Height = Constants.TextBoxHeight;
textbox.Text = Convert.ToString(nCounter) + Constants.TextBoxSecondLength;
textbox.Multiline = false; // تعطيل خاصية تعدد السطور وجعله سطر واحد
textbox.TextAlign = HorizontalAlignment.Right; // جعله على اليامين
textbox.MaxLength = Constants.TextBoxMaxLength;
textbox.ReadOnly = true; // جعل مربع التحرير قابل للقراءة فقط ولا يمكن الكتابة
عليه
textbox.Parent = this;
textbox.Location = new Point(0, 0);
textbox.TabIndex = 1;
textbox.TabStop = true;
// إنشاء زر
button = new Button();
button.Font = new Font(Constants.ButtonFontName, Constants.ButtonFontSize);
button.TextAlign = ContentAlignment.MiddleCenter; // جعله في مركز الوسط
button.Width = Constants.ButtonWidth;
button.Height = Constants.ButtonHeight;
button.Text = Constants.StartButtonLength;
button.BackColor = SystemColors.Control;
button.Parent = this;
button.Location = new Point(Constants.TextBoxWidth, 0);
button.Click += new EventHandler(ButtonClick);
button.TabIndex = 0;
button.TabStop = true;
}
/* حزمة ButtonClick */
/* عند الضغط على الزر */
public void ButtonClick(object sender, EventArgs e)
{
if (bCountFlag) // يتم تعطيل المنبه أو الموقت عند التشغيل
{
timer.Stop();
button.Text = Constants.StartButtonLength;
textbox.Focus();
}
else // تشغيل الموقت
{
nCounter = 0;
textbox.Text = Convert.ToString(nCounter) + Constants.TextBoxSecondLength;
timer.Start();
button.Text = Constants.StopButtonLength;
}
bCountFlag = !bCountFlag;
}
/* حزمة MyTimer */
/* يتم طلبها كل ثانية */
public void MyTimer(object sender, ElapsedEventArgs e)
{
nCounter++;
int nSecond = nCounter;
if (nSecond >= Constants.TimeUp)
{
button.PerformClick(); // الضغط على الزر
}
textbox.Text = "";
if (nSecond >= 3600) // عند تجاوز عدد الثواني 3600 أي ساعة كاملة
{
int nHour = nSecond / 3600;
nSecond %= 3600;
textbox.Text = Convert.ToString(nHour) + Constants.TextBoxHourLength;
}
if (nSecond >= 60) // عند تجاوز عدد الثواني 60 انية أي دقيقة
{
int nMinute = nSecond / 60;
nSecond %= 60;
textbox.Text += Convert.ToString(nMinute) + Constants.TextBoxMinuteLength;
}
textbox.Text += Convert.ToString(nSecond) + Constants.TextBoxSecondLength;
System.Media.SystemSounds.Beep.Play(); // إصدار صوت. وهو الصوت الإفتراضي عند
الرسائل والتحذيرات
}
/* حزمة OnFormClosing */
/* ويتم طلبها تلقائيا عند إغلاق البرنامج */
protected override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
DialogResult dr;
if (bCountFlag) // إذا كان أثناء تشغيل الموقت
{
dr = MessageBox.Show("هل ترغب في إيقاف الموقت والخروج من البرنامج؟",
Constants.ApplicationName, MessageBoxButtons.YesNo, MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2);
if (dr == DialogResult.Yes) // الخروج من البرنامج إثناء التشغيل
{
timer.Stop();
e.Cancel = false;
}
else
{
e.Cancel = true;
}
}
else // وإذا كان الموقت معطلا
{
e.Cancel = false;
}
}
}