نتعرض فى هذه الجزئية للأدوات الأساسية فى Mono WinForms
الأدوات (الكنترولز) هى وحدة بناء الواجهة، وتقدم لنا WinForms الكثير من الأدوات المختلفة كالأزرار وصناديق الإختبار والمنزلقات والقوائم.. الخ ،كل مايحتاجه المبرمج فى عمله. سنتعرض لبعض الأدوات المفيدة فى هذه الجزئية
هى اداة لعرض نص ساكن او صور ولاتأخذ focus
label.cs using System; using System.Drawing; using System.Windows.Forms; class MForm : Form { string text = @"Sometimes I feel I've got to Run away I've got to Get away From the pain that you drive into the heart of me The love we share Seems to go nowhere I've lost my lights I toss and turn I can't sleep at night Once I ran to you (I ran) Now I'll run from you This tainted love you've given I give you all a boy could give you Take my tears and that's not nearly all Tainted love Tainted love"; public MForm() { Text = "Tainted Love"; Font font = new Font("Serif", 10); Label lyrics = new Label(); lyrics.Parent = this; lyrics.Text = text; lyrics.Font = font; lyrics.Location = new Point(10, 10); lyrics.Size = new Size (290, 290); CenterToScreen(); } } class MApplication { public static void Main() { Application.Run(new MForm()); } }
فى مثالنا نعرض على الأداة كلمات اغنية
Label lyrics = new Label();انشاء الآداة
string text = @"Sometimes I feel I've got ... الحرف @ يستخدم لعمل نصوص على اسطر متعددة
Font font = new Font("Serif", 10); ... lyrics.Font = font;
نخدد نوع الخط ل Serif والحجم الى 10px
Figure: Label
صندوق الإختبار لديه حالتين اما منشط او لا، هو عبارة عن صندوق من نص ساكن او صورة. اذا كان الصندوق منشط فيتم التعبير عن ذلك بعلامة على الصندوق، ممكن استخدامه لعرض Splashscreen او عكس الحالة الخ الخ
checkbox.cs using System; using System.Drawing; using System.Windows.Forms; class MForm : Form { private CheckBox cb; public MForm() { Text = "CheckBox"; Size = new Size(220, 170); cb = new CheckBox(); cb.Parent = this; cb.Location = new Point(30, 30); cb.Text = "Show Title"; cb.Checked = true; cb.CheckedChanged += new EventHandler(OnChanged); CenterToScreen(); } void OnChanged(object sender, EventArgs e) { if (cb.Checked) { Text = "CheckBox"; } else { Text = ""; } } } class MApplication { public static void Main() { Application.Run(new MForm()); } }
هنا يتوقف عرض عنوان النافذة على حالة ال checkbox
cb = new CheckBox();انشاء الآداة
cb.Text = "Show Title"; cb.Checked = true;
تحديد النص الظاهر مع الآداة بإستخدام الخاصية Text
تحديد حالة الcheckbox بإستخدام الخاصية Checked
cb.CheckedChanged += new EventHandler(OnChanged);عند الضغط على الآداة يتم اطلاق الحدث CheckedChanged
if (cb.Checked) { Text = "CheckBox"; } else { Text = ""; }
هنا نعرض عنوان النافذة على حسب حالة الآداة
Figure: CheckBox
المنزلق، هو اداة تسمح بإختيار قيمة من فترة محددة، فى مثالنا سنعرض اداة للتحكم فى الصوت
trackbar.cs using System; using System.Drawing; using System.Windows.Forms; class MForm : Form { PictureBox pb; TrackBar tb; Bitmap mute, min, med, max; public MForm() { Text = "TrackBar"; Size = new Size(260, 190); tb = new TrackBar(); tb.Parent = this; tb.Size = new Size(160, 30); tb.Location = new Point(20, 40); tb.TickStyle = TickStyle.None; tb.ValueChanged += new EventHandler(OnChanged); LoadImages(); pb = new PictureBox(); pb.Parent = this; pb.Location = new Point(210, 50); pb.Image = mute; CenterToScreen(); } void LoadImages() { mute = new Bitmap("mute.png"); min = new Bitmap("min.png"); med = new Bitmap("med.png"); max = new Bitmap("max.png"); } void OnChanged(object sender, EventArgs e) { int val = tb.Value; if (val == 0) { pb.Image = mute; } else if (val > 0 && val <= 3) { pb.Image = min; } else if (val > 3 && val < 8) { pb.Image = med; } else { pb.Image = max; } } } class MApplication { public static void Main() { Application.Run(new MForm()); } }
فى مثالنا نعرض منزلق ، و صندوق صورة "لعرض الصور"، وعند تحريك المنزلق يتم تعديل الصورة الظاهرة بشكل ملائم
tb = new TrackBar();انشاء المنزلق
tb.TickStyle = TickStyle.None;اخفاء العلامات الخاصة به
pb = new PictureBox(); ... pb.Image = mute;
انشاء الكائن pb من الصف PictureBox لعرض صورة ، وقمنا بتحديد الصورة الظاهرة الى mute بإستخدام الخاصية Image
void LoadImages() { mute = new Bitmap("mute.png"); min = new Bitmap("min.png"); med = new Bitmap("med.png"); max = new Bitmap("max.png"); }
هنا نقوم بتحميل الصور اللتى سنقوم بإستخدامها
int val = tb.Value; if (val == 0) { pb.Image = mute; } else if (val > 0 && val <= 3) { pb.Image = min; } else if (val > 3 && val < 8) { pb.Image = med; } else { pb.Image = max; }
نحدد القيمة المختارة من المنزلق وبناءا عليها نقوم بتحديد الصورة المطلوبة ونحدثها على الكائن pb
Figure: TrackBar
اداة ComboBox تجمع بين زر او حقل ادخال "قابل للتحرير" (فى حال تحديدك بجعل الآداة قابلة للتحرير) وقائمة منسدلة ليختار منها المستخدم اختيارا مناسبا،
combobox.cs using System; using System.Drawing; using System.Windows.Forms; class MForm : Form { private ComboBox cb; private Label label; public MForm() { Text = "ComboBox"; Size = new Size(240, 240); cb = new ComboBox(); cb.Parent = this; cb.Location = new Point(50, 30); cb.Items.AddRange(new object[] {"Ubuntu", "Mandriva", "Red Hat", "Fedora", "Gentoo"}); cb.SelectionChangeCommitted += new EventHandler(OnChanged); label = new Label(); label.Location = new Point(50, 140); label.Parent = this; label.Text = "..."; CenterToScreen(); } void OnChanged(object sender, EventArgs e) { ComboBox combo = (ComboBox) sender; label.Text = combo.Text; } } class MApplication { public static void Main() { Application.Run(new MForm()); } }
المثال يعرض اداة ComboxBox ب 5 عناصر، ويتم عرض العنصر المختار فى نص ساكن
Our code programming example shows a combobox with five items. The selected item is shown in a label control.
cb = new ComboBox();انشاء الآداة
cb.Items.AddRange(new object[] {"Ubuntu", "Mandriva", "Red Hat", "Fedora", "Gentoo"});
ملء العناصر الخاصة بها
cb.SelectionChangeCommitted += new EventHandler(OnChanged);عند تغيير العنصر يتم اطلاق الحدث SelectionChangeCommitted
void OnChanged(object sender, EventArgs e) { ComboBox combo = (ComboBox) sender; label.Text = combo.Text; }
يتم عرض العنصر المختار على النص الساكن "بإستخدام الخاصية Text المسئولة عن الحصول على النص المحتوى"
Figure: ComboBox
الأجندة الشهرية، تسمح للمستخدم بإختيار تاريخ معين عن طريق واجهة
monthcalendar.cs using System; using System.Drawing; using System.Windows.Forms; class MForm : Form { private MonthCalendar calendar; private Label date; public MForm() { Text = "Month Calendar"; Size = new Size(240, 240); calendar = new MonthCalendar(); calendar.Parent = this; calendar.Location = new Point(20, 20); calendar.DateSelected += new DateRangeEventHandler(OnSelected); date = new Label(); date.Location = new Point(40, 170); date.Parent = this; DateTime dt = calendar.SelectionStart; date.Text = dt.Month + "/" + dt.Day + "/" + dt.Year; CenterToScreen(); } void OnSelected(object sender, EventArgs e) { DateTime dt = calendar.SelectionStart; date.Text = dt.Month + "/" + dt.Day + "/" + dt.Year; } } class MApplication { public static void Main() { Application.Run(new MForm()); } }
فى المثال نعرض اجندة ونص ساكن
private MonthCalendar calendar; private Label date;
لدينا اداتين اجندة، ونص ساكن
void OnSelected(object sender, EventArgs e) { DateTime dt = calendar.SelectionStart; date.Text = dt.Month + "/" + dt.Day + "/" + dt.Year; }
عندما نختار تاريخا معينا، يتم استدعاء الطريقة OnSelected ونحصل على بداية الإختيار بإستخدام الخاصية SelectionStart
Figure: MonthCalendar
ال TextBox هو اداة لعرض او ادخال بعض النصوص، قد تكون على سطر واحد او متعددة وتستطيع ايضا استخدامها لادخال كلمات السر
textbox.cs using System; using System.Drawing; using System.Windows.Forms; class MForm : Form { private Label text; public MForm() { Text = "TextBox"; Size = new Size(250, 200); CenterToScreen(); text = new Label(); text.Parent = this; text.Text = "..."; text.Location = new Point(60, 40); text.AutoSize = true; TextBox tbox = new TextBox(); tbox.Parent = this; tbox.Location = new Point(60, 100); tbox.KeyUp += new KeyEventHandler(OnKeyUp); } void OnKeyUp(object sender, KeyEventArgs e) { TextBox tb = (TextBox) sender; this.text.Text = tb.Text; } } class MApplication { public static void Main() { Application.Run(new MForm()); } }
فى المثال لدينا نص ساكن وحقل ادخال نصوص، يتم عرض مانقوم بكتابته فى حقل الإدخال على الlabel فورا
text = new Label(); ... text.AutoSize = true;
انشاء النص الساكن، وتفعيل AutoSize لضمان تمدد الlabel مع حجم النص
TextBox tbox = new TextBox(); ... tbox.KeyUp += new KeyEventHandler(OnKeyUp);
هنا نقوم بربط الحدث KeyUp “عندما نترك الزر" بالطريقة OnKeyUp
void OnKeyUp(object sender, KeyEventArgs e) { TextBox tb = (TextBox) sender; this.text.Text = tb.Text; }
هنا نقوم بتحديث محتوى ال label بما يحويه حقل الإدخال
Figure: TextBox
Home ‡ Contents ‡ Top of Page