بسم الله الرحمن الرحيم
الموضوع على ما أظن متقدم قليلاً
ولكن لم أستطيع تبسط الموضوع لربما أشكو من ضعف في عملية الشرح أتمنى من الله أن
يعلمنا ماه خير ونفع للمسلمين.
نفترض أننا نريد بناء برنامج مع
طريقة ما لمساعدة المستخدم لمعرفة لمحة عن الأمر قبل أن يقوم بتنفيذه
مثل النوافذ الصفراء التي تضهر لنا
عند الوقوف على أحد أومر البرنامج أنت في بادء المر سوف تقول لماذا هذا التعقيد
نضع خاصية ال ToolTip بما نريد ولكن أفترض أن
تعمل تتعامل مع عدد هائل من الأوامر ففي مثالنا هذا دمجة تقنيتان
1 –Attribute
2- أحد التصاميم المشهورة وهو The Command Pattern From Design Patterns [Behavioral Patterns] وهذا التصميم مفيد جداً
لقيام بعملية التراجع (Undo)
أولأ نقوم ببناء الصنف (Class)
الذي نريد ونضيف عليه الخصائص التي نريدها
في مثالنا هذا قمت ببناء صنف (Class) وأطلقة عليه أسم HelpTestAttribute وأضفت عليه منهج (Method) واحد وهو Description لا حض أننا قمنا بوراثة من الصنف الرئيسي الذي يأتي مع مكتبة
الدوت نت
ملاحظة: يمكننا
إضافة مانشاء من مناهج (Methods) إلى صنفنا ( Our Class) .
using System;
namespace
AttributeSample
{
class HelpTestAttribute : Attribute
{
protected
string m_Description;
public
HelpTestAttribute(string description)
{
m_Description = description;
}
public string Description
{
get
{
return
m_Description;
}
set
{
m_Description = value;
}
}
}
}
أما تبقى من أصناف فهي مسؤولة عن تمثيل Command Pattern
تعريف Command
Pattern : يتركك تعطي برنامجط القدرة لتنفيذ الأوامر بدون
معرفة أيّ شئ حول العمل الفعلي الذي سيؤدّي ويسمح لك لتغيير ذلك العمل بدون تأثير
على برنامجك في أية حال.
ولكن هذا أبسط تعريف لهذا الموضوع
ولكن إذا سنحت لي الفرصة إنشاء الله سوف أقوم بشرح لأغلب Design Patterns في هذا المنتدى
يتألف هذا التصميم من واجهه (Interface)
using System;
namespace
AttributeSample
{
interface ICommand
{
void
Excute();
}
}
ولكن في مثالنا هذا قومنا بإنشاء واجهه (Interface) إخرى لنستطيع أن نضع الأوامر أينما شئنا
using System;
namespace
AttributeSample
{
interface ICommandHolder
{
ICommand
Command
{
get;
set;
}
}
}
وبعدذلك أنشئنا أومرنا السهلة :
1-
using System;
using
System.Windows.Forms;
namespace
AttributeSample
{
class Action1Command : ICommand
{
[HelpTest("ضرب العدد 5 بالعدد 2")]
public void Excute()
{
MessageBox.Show((2
* 5).ToString());
}
}
}
2-
using System;
using
System.Windows.Forms;
namespace
AttributeSample
{
class Action2Command : ICommand
{
[HelpTest("ضرب العدد 5 بالعدد 100")]
public void Excute()
{
MessageBox.Show((100
* 5).ToString());
}
}
}
لاحظ كيف قومنا بوراثة الواجه السابقة ICommand في صنفينا
ننشأ تحكم جديد موروث من الصنف الأساس Button والوجهه التي قمنا بإنشائها IcommandHolser لنستطيع أن نحفظ به أومرنا لتسهيل تصميم
البرنامج:
using System;
using System.Windows.Forms;
namespace
AttributeSample
{
[System.Drawing.ToolboxBitmap(typeof(System.Windows.Forms.Button))]
[System.ComponentModel.DesignerCategoryAttribute("code")]
class CommandButton : Button,
ICommandHolder
{
public ICommand m_Command;
public
CommandButton()
: base()
{
}
public ICommand Command
{
get
{
return
m_Command;
}
set
{
m_Command = value;
}
}
}
}
نقوم بإدراج نافذة جيد ونضع عليها تحمين من CommandButton و نكتب أومرنا في أحداث هذان التحكم
using System;
using
System.Windows.Forms;
using
System.Drawing;
namespace
AttributeSample
{
public partial class Form1 : Form
{
private
System.ComponentModel.IContainer components = null;
private
CommandButton commandButton1;
private
CommandButton commandButton2;
private
System.Windows.Forms.ToolTip toolTip1;
public
Form1()
{
InitializeComponent();
commandButton1.Command = new Action1Command();
commandButton2.Command = new Action2Command();
}
protected
override void
Dispose(bool disposing)
{
if
(disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer
generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private
void InitializeComponent()
{
this.components
= new System.ComponentModel.Container();
this.toolTip1
= new System.Windows.Forms.ToolTip(this.components);
this.commandButton2
= new AttributeSample.CommandButton();
this.commandButton1
= new AttributeSample.CommandButton();
this.SuspendLayout();
//
//
toolTip1
//
this.toolTip1.IsBalloon
= true;
this.toolTip1.ToolTipIcon
= System.Windows.Forms.ToolTipIcon.Info;
//
//
commandButton2
//
this.commandButton2.Command
= null;
this.commandButton2.Location
= new System.Drawing.Point(156,
150);
this.commandButton2.Name
= "commandButton2";
this.commandButton2.Size
= new System.Drawing.Size(124,
37);
this.commandButton2.TabIndex
= 1;
this.commandButton2.Text
= "Action 2";
this.commandButton2.UseVisualStyleBackColor
= true;
this.commandButton2.MouseLeave
+= new System.EventHandler(this.commandButton2_MouseLeave);
this.commandButton2.Click
+= new System.EventHandler(this.commandButton2_Click);
this.commandButton2.MouseHover
+= new System.EventHandler(this.commandButton2_MouseHover);
//
//
commandButton1
//
this.commandButton1.Command
= null;
this.commandButton1.Location
= new System.Drawing.Point(13,
150);
this.commandButton1.Name
= "commandButton1";
this.commandButton1.Size
= new System.Drawing.Size(124,
37);
this.commandButton1.TabIndex
= 0;
this.commandButton1.Text
= "Action 1";
this.commandButton1.UseVisualStyleBackColor
= true;
this.commandButton1.MouseLeave
+= new System.EventHandler(this.commandButton2_MouseLeave);
this.commandButton1.Click
+= new System.EventHandler(this.commandButton2_Click);
this.commandButton1.MouseHover
+= new System.EventHandler(this.commandButton2_MouseHover);
//
// Form1
//
this.AutoScaleDimensions
= new System.Drawing.SizeF(6F,
13F);
this.AutoScaleMode
= System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize
= new System.Drawing.Size(292,
266);
this.Controls.Add(this.commandButton2);
this.Controls.Add(this.commandButton1);
this.Name
= "Form1";
this.Text
= "Form1";
this.ResumeLayout(false);
}
#endregion
private
void commandButton2_Click(object sender, EventArgs
e)
{
((ICommandHolder)sender).Command.Excute();
}
private
void commandButton2_MouseHover(object sender, EventArgs
e)
{
// Get
the class type to access its metadata.
Type
clsType = ((ICommandHolder)sender).Command.GetType();
// Get
type information for the method.
System.Reflection.MethodInfo mInfo = clsType.GetMethod("Excute");
// Get
the AnimalType attribute for the method.
HelpTestAttribute
HelpTesrAttr = (HelpTestAttribute)Attribute.GetCustomAttribute(mInfo, typeof(HelpTestAttribute));
CommandButton
cmd = sender as CommandButton;
Point
pt = new Point(cmd.Left
+ (cmd.Width / 2), cmd.Bottom);
toolTip1.ToolTipTitle = cmd.Text;
toolTip1.Show(HelpTesrAttr.Description, this,
pt);
}
private
void commandButton2_MouseLeave(object sender, EventArgs
e)
{
toolTip1.Hide(this);
}
}
}
الهم علمنا ما ينفعنا ونفعنا بما علمتنا وهب لنا من لدنك رحمة
أتمنى أن أكون قد أستطع أن أصل ما كان ضالتكم