تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
مقال- أفكار في الدوت نت Enum
#12
كيف تستخدم كل من EnumListBox/EnumComboBox كونترول

في المشاركة السابقة أوضحت كبف تبني كونترول يقبل Enum فقط وبعد عمل Build ستجد نسخة من هذه الكونترول في صندوق الأدوات والكود التالي يوضح كيفية إستخدام أحدهما أو كليهما كالأتي


كود :
Public Class Form1

Private align As ContentAlignment = ContentAlignment.TopLeft
Private clr As Color = Color.Empty

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim knownColorType As Type = GetType(KnownColor)
Me.EnumListBox1.EnumType = knownColorType

Dim contentAlignmentType As Type = GetType(ContentAlignment)
Me.EnumComboBox1.EnumType = contentAlignmentType

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 EnumListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EnumListBox1.SelectedIndexChanged
clr = Color.FromKnownColor(StringToEnum(Of KnownColor)(Me.EnumListBox1.SelectedItem.ToString, True))
Invalidate()
End Sub

Private Sub EnumComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EnumComboBox1.SelectedIndexChanged
align = StringToEnum(Of ContentAlignment)(Me.EnumComboBox1.SelectedItem.ToString, False)
Invalidate()
End Sub

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)
e.Graphics.DrawRectangle(framePen, frameRect)
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", 20.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
وستلاحظون أننا نقوم بتعريف Type من أي Enum ثم نمرره الي Property EnumType والكود التالي يوضح كيفية تمرير القيم الي Property EnumType


كود :
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim knownColorType As Type = GetType(KnownColor)
Me.EnumListBox1.EnumType = knownColorType

Dim contentAlignmentType As Type = GetType(ContentAlignment)
Me.EnumComboBox1.EnumType = contentAlignmentType

End Sub
بالمرفقات ستجدون نسخة من الكونترول وكيفية استخدامهم والكود بنسخة الفيجوال 2010

ولمستخدمي النسخ السابقة لنسخة 2010 يمكنهم نسخ الكود واستخدامه.............

ملحوظة أخيرة:
بالرغم أنني استخدمت جميع Enum لرسم أشياء علي الفورم لكن الأمر لايقتصر علي ذلك فقط فالفكرة بالنهاية واحدة ويمكن الإستفادة منها مع اي Enum ..............الأمر متروك لإبداعكم.....

تقبلوا تحياتي ولاتنسونا في دعاؤكم.............

أخوكم عمر


الملفات المرفقة
.rar   Enum_Controls_src.rar (الحجم : 101.63 ك ب / التحميلات : 45)
}}}
تم الشكر بواسطة:


الردود في هذا الموضوع
مقال- أفكار في الدوت نت Enum - بواسطة Raggi Tech - 05-10-12, 01:31 PM

المواضيع المحتمل أن تكون متشابهة .
الموضوع : الكاتب الردود : المشاهدات : آخر رد
  التعامل مع الصور Images في بيئة الدوت نت باستخدام +GDI - مقدمة RaggiTech 3 6,535 30-07-21, 05:14 PM
آخر رد: kebboud
  مقال: الكومبو بوكس ComboBox كيف تضيف أيقونات Blue Sky 1 3,531 30-06-19, 10:41 AM
آخر رد: invocker
  التــعامل مع cmd من خلال الدوت نت مبتدئ في الاحتراف 3 4,158 02-06-18, 12:36 AM
آخر رد: YousefOkasha
  أفكار في الجرافكس AlignRectangle silverlight 0 1,768 14-10-17, 02:02 PM
آخر رد: silverlight
  تفقيط الارقام فى الدوت نت مبرمج أوتار 17 14,029 20-04-17, 12:21 PM
آخر رد: محمد بوقزاحة
  [مقال] تشغيل برمجيات الدوت نت بدون تنصيب النت فروم ويرك m0075 13 11,518 13-02-14, 08:29 PM
آخر رد: Omar Mekkawy
  مقال- كيفية الاستغناء عن الداتا بيز التقليدية في برامجنا – ألجزء الأول RaggiTech 1 3,775 06-10-12, 12:23 AM
آخر رد: RaggiTech
  مقال- تطوير الكونترول Property Attributes الجزء الثالث RaggiTech 0 2,534 06-10-12, 12:20 AM
آخر رد: RaggiTech
  مقال- تطوير الكونترول Skin Control RaggiTech 0 2,691 06-10-12, 12:08 AM
آخر رد: RaggiTech
  مقال- كيف تصنع ملفات المساعدة Help Files وتربطها مع برنامجك RaggiTech 0 3,510 05-10-12, 07:30 PM
آخر رد: RaggiTech

التنقل السريع :


يقوم بقرائة الموضوع: بالاضافة الى ( 1 ) ضيف كريم