05-10-12, 01:19 PM
كما تلاحظون في الأكواد اعلاه انه في كل مرة نحاول أن نقوم بتحويل المتغيرات الموجودة داخل أي Enum وهي عبارة عن Strings إلي قيمتها العادية فإننا نبني دالة لكي نسترجع القيمة وربما لاحظتم في المثال الثالث أعلاه الدوال التالية
بالتأكيد ليس منطقيا ان نكتب دالة لكل Enum علي حدة بل الأفضل أن نكتب دالة واحدة فقط نستفيد بها في استرجاع قيمة الثوابت الموجودة داخل كل Enum وبهذا نقلل من حجم الكود المكتوب.
لذلك الدالة التالية يمكن أن تحل محل اي دالة من الدوال الأربعة أعلاه بل أيضا من الممكن استخدامها مع أي Enum وكما ستلاحطون فإنه قد تم استخدام Generics لبناء الدالة ..........
والكود التالي يوضح التعديلات الجديدة بالمثال الثالث وستلاحظون ان الكود اصبح اقل من الكود الموجود بالمثال الثالث
الكود الموجود بالمرفقات بنسخة الفيجوال استوديو 2010
في المشاركات اللاحقة سوف نناقش كيف نستفيد أكثر من Enum و بطرق أخري مختلفة
تقبلوا تحياتي ولا تنسونا في دعائكم..............
أخوكم عمر
كود :
Private Function GetHatchStyleFromString(ByVal hatchString As String, ByVal ignoreCase As Boolean) As HatchStyle
Return CType([Enum].Parse(GetType(HatchStyle), hatchString, ignoreCase), HatchStyle)
End Function
Private Function GetContentAlinmentFromString(ByVal hatchString As String, ByVal ignoreCase As Boolean) As ContentAlignment
Return CType([Enum].Parse(GetType(ContentAlignment), hatchString, ignoreCase), ContentAlignment)
End Function
Private Function GetKnowColorString(ByVal ColorName As String, ByVal ignoreCase As Boolean) As KnownColor
Return CType([Enum].Parse(GetType(KnownColor), ColorName, ignoreCase), KnownColor)
End Function
Private Function GetDashStyleString(ByVal ColorName As String, ByVal ignoreCase As Boolean) As DashStyle
Return CType([Enum].Parse(GetType(DashStyle), ColorName, ignoreCase), DashStyle)
End Functionلذلك الدالة التالية يمكن أن تحل محل اي دالة من الدوال الأربعة أعلاه بل أيضا من الممكن استخدامها مع أي Enum وكما ستلاحطون فإنه قد تم استخدام Generics لبناء الدالة ..........
كود :
Private Function StringToEnum(Of T)(ByVal value As String, ByVal ignoreCase As Boolean) As T
If [Enum].IsDefined(GetType(T), value) Then
Return CType([Enum].Parse(GetType(T), value, ignoreCase), T)
End If
Throw New ArgumentException("value is not Enum")
End Functionكود :
Imports System.Drawing.Drawing2D
Public Class Form1
Private align As ContentAlignment = ContentAlignment.TopLeft
Private clr As Color = Color.Empty
Private hatch As HatchStyle = Nothing
Private pType As DashStyle = DashStyle.Dash
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
FillComboBoxsWithEnumData()
End Sub
Private Sub FillComboBoxsWithEnumData()
' Fill ComboBox1 with HatchStyle Enum
For Each hatchStyleObject As Object In [Enum].GetValues(GetType(HatchStyle))
If Not ComboBox1.Items.Contains(hatchStyleObject) Then ComboBox1.Items.Add(hatchStyleObject)
Next
' Fill ComboBox2 with ContentAlignment Enum
For Each contentAlignmentObject As Object In [Enum].GetValues(GetType(ContentAlignment))
If Not ComboBox2.Items.Contains(contentAlignmentObject) Then ComboBox2.Items.Add(contentAlignmentObject)
Next
' Fill ComboBox3 with KnowColor Enum
For Each knownColorObject As Object In [Enum].GetValues(GetType(KnownColor))
If Not ComboBox3.Items.Contains(knownColorObject) Then ComboBox3.Items.Add(knownColorObject)
Next
' Fill ComboBox4 with DashStyle Enum
For Each dashStyleObject As Object In [Enum].GetValues(GetType(DashStyle))
If Not ComboBox4.Items.Contains(dashStyleObject) Then ComboBox4.Items.Add(dashStyleObject)
Next
End Sub
Private Function StringToEnum(Of T)(ByVal value As String, ByVal ignoreCase As Boolean) As T
If [Enum].IsDefined(GetType(T), value) Then
Return CType([Enum].Parse(GetType(T), value, ignoreCase), T)
End If
Throw New ArgumentException("value is not Enum")
End Function
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
hatch = StringToEnum(Of HatchStyle)(Me.ComboBox1.SelectedItem.ToString, True)
Invalidate()
End Sub
Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
align = StringToEnum(Of ContentAlignment)(Me.ComboBox2.SelectedItem.ToString, False)
Invalidate()
End Sub
Private Sub ComboBox3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox3.SelectedIndexChanged
clr = Color.FromKnownColor(StringToEnum(Of KnownColor)(Me.ComboBox3.SelectedItem.ToString, True))
Invalidate()
End Sub
Private Sub ComboBox4_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox4.SelectedIndexChanged
pType = StringToEnum(Of DashStyle)(Me.ComboBox4.SelectedItem.ToString, True)
Invalidate()
End Sub
' Paint Something in the Form
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
Dim TestRectangle As New Rectangle(10, 10, 200, 200)
Dim frameRect As Rectangle = TestRectangle
frameRect.X -= 4
frameRect.Y -= 4
frameRect.Width += 8
frameRect.Height += 8
Using obrush As New Drawing2D.LinearGradientBrush(frameRect, ControlPaint.Light(clr), ControlPaint.Dark(clr), Drawing2D.LinearGradientMode.Vertical)
e.Graphics.FillRectangle(obrush, frameRect)
Using framePen As New Pen(Brushes.Black)
framePen.DashStyle = pType
e.Graphics.DrawRectangle(framePen, frameRect)
End Using
End Using
Using hBrush As New Drawing2D.HatchBrush(hatch, clr)
e.Graphics.FillRectangle(hBrush, TestRectangle)
Using borderPen As New Pen(clr)
borderPen.DashStyle = pType
e.Graphics.DrawRectangle(borderPen, TestRectangle)
End Using
End Using
If Me.Text IsNot Nothing Then
Dim format As New StringFormat()
format.LineAlignment = StringAlignment.Center
format.Trimming = StringTrimming.EllipsisCharacter
Select Case align
Case ContentAlignment.BottomCenter
format.Alignment = StringAlignment.Center
format.LineAlignment = StringAlignment.Far
Exit Select
Case ContentAlignment.BottomLeft
format.Alignment = StringAlignment.Near
format.LineAlignment = StringAlignment.Far
Exit Select
Case ContentAlignment.BottomRight
format.Alignment = StringAlignment.Far
format.LineAlignment = StringAlignment.Far
Exit Select
Case ContentAlignment.MiddleCenter
format.Alignment = StringAlignment.Center
format.LineAlignment = StringAlignment.Center
Exit Select
Case ContentAlignment.MiddleLeft
format.Alignment = StringAlignment.Near
format.LineAlignment = StringAlignment.Center
Exit Select
Case ContentAlignment.MiddleRight
format.Alignment = StringAlignment.Far
format.LineAlignment = StringAlignment.Center
Exit Select
Case ContentAlignment.TopCenter
format.Alignment = StringAlignment.Center
format.LineAlignment = StringAlignment.Near
Exit Select
Case ContentAlignment.TopLeft
format.Alignment = StringAlignment.Near
format.LineAlignment = StringAlignment.Near
Exit Select
Case ContentAlignment.TopRight
format.Alignment = StringAlignment.Far
format.LineAlignment = StringAlignment.Near
Exit Select
End Select
Dim fnt As New Font("Times /new Roman", 25.0F, FontStyle.Bold)
Using sb As New SolidBrush(Color.White)
e.Graphics.DrawString(Me.Text, fnt, sb, TestRectangle, format)
End Using
End If
End Sub
End Classفي المشاركات اللاحقة سوف نناقش كيف نستفيد أكثر من Enum و بطرق أخري مختلفة
تقبلوا تحياتي ولا تنسونا في دعائكم..............
أخوكم عمر
