منتدى فيجوال بيسك لكل العرب | منتدى المبرمجين العرب

نسخة كاملة : أداه TextBoxPlaceholder تسمح باظهار تلميح حول مربع النص عندما يكون فارغا
أنت حالياً تتصفح نسخة خفيفة من المنتدى . مشاهدة نسخة كاملة مع جميع الأشكال الجمالية .
بسم الله الرحمن الرحيم

السلام عليكم اعضاء المنتدى الكرام

قد طلب احد الاخوة في المنتدى طريقة عمل تلميح لمربع النص او مايعرف ب Placeholder ،

لذلك قمت ببناء اداه تشتق من TextBox ويمكنها فعل ذلك :

[attachment=26062]

يوجد بها 3 خصائص تحت تبويب Placeholder وهم

Hint : التلميح المطلوب اظهاره عندما يكون مربع النص فارغا
HideHintWhenFocused : هل تريد اخفاء التلميح عندما ينتقل التركيز للاداه 
HintColor : لون التلميح

[attachment=26063]

الكود الاصلي مأخوذ من هذا الموضوع : https://stackoverflow.com/a/50926164

ما قمت بتطويره هو خاصية تغيير اللون وتحديد اذا كنت تريد اخفاء التلميح ام لا وكذلك بعض التحسينات في المنطق من حيث دعم اللغة العربية والاتجاه من اليمين لليسار :

الكود كاملا هو : 


كود :
Imports System.ComponentModel

Public Class TextBoxPlaceholder
   Inherits TextBox


   Private m_Hint As String
   <Description("Specify The Text To Be Shawn as a Placeholder"),
   Category("Placeholder")>
   Public Property Hint As String
       Get
           Return m_Hint
       End Get
       Set(ByVal value As String)
           m_Hint = value
           Me.Invalidate()
       End Set
   End Property


   Private m_HideHintWhenFocused As Boolean = True
   <Description("Specify Wheather the hint will be hidden when the tool loses focus"),
   Category("Placeholder")>
   Public Property HideHintWhenFocused As Boolean
       Get
           Return m_HideHintWhenFocused
       End Get
       Set(ByVal value As Boolean)
           m_HideHintWhenFocused = value
           Me.Invalidate()
       End Set
   End Property


   Private m_HintColor As Color = SystemColors.GrayText
   <Description("Specify The Hint Color"),
   Category("Placeholder")>
   Public Property HintColor As Color
       Get
           Return m_HintColor
       End Get
       Set(ByVal value As Color)
           m_HintColor = value
           Me.Invalidate()
       End Set
   End Property
   Protected Overrides Sub WndProc(ByRef m As Message)
       MyBase.WndProc(m)

       If m.Msg = &HF Then
           If Not (Focused AndAlso HideHintWhenFocused) _
               AndAlso String.IsNullOrEmpty(Me.Text) _
               AndAlso Not String.IsNullOrEmpty(Me.Hint) Then
               Using g = Me.CreateGraphics()
                   TextRenderer.DrawText(g, Me.Hint, Me.Font, Me.ClientRectangle, HintColor, Me.BackColor,
                       TextFormatFlags.Top Or If(RightToLeft, TextFormatFlags.RightToLeft Or TextFormatFlags.Right, TextFormatFlags.Left))
               End Using
           End If
       End If
   End Sub

   Protected Overrides Sub OnTextChanged(e As EventArgs)
       MyBase.OnTextChanged(e)
       If String.IsNullOrEmpty(Me.Text) AndAlso Not HideHintWhenFocused Then
           Invalidate()
       End If
   End Sub
End Class

قم بتحميل الكلاس واضفه الى مشروعك ثم قم بعمل Build Solution ستجد الاداه ظهرت في مربع الادوات 

[attachment=26064]

ومرفق ايضا مثال للاطلاع

اتمنى ان احصل على اقتراحاتكم للتطوير ،

شكرا