Über Textbox Zeichnen oder in Textbox, nur wie?

  • VB.NET
  • .NET (FX) 4.5–4.8

Es gibt 2 Antworten in diesem Thema. Der letzte Beitrag () ist von RodFromGermany.

    Über Textbox Zeichnen oder in Textbox, nur wie?

    Moin, ich hab ein Control erstellt in dem eine Textbox drinnen ist, jetzt möchte ich gerne über die
    Textbox Zeichnen oder innen drin, jetzt meine frage ob da jemand eine Lösung zu hat bzw ob es überhaupt
    möglich ist, ich hab schon diverse Themen durchsucht aber nichts hilfreiches
    gefunden, über ein Beispiel würde ich mich sehr freuen.

    Hier die Textbox:
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Public Class XTextBox
    2. Inherits Control
    3. Dim WithEvents TxtBox As New TextBox
    4. Dim G As Graphics
    5. Sub New()
    6. MyBase.New()
    7. SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint, True)
    8. SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
    9. BackColor = Color.FromArgb(50, 50, 50)
    10. ForeColor = Color.FromArgb(100, 100, 100)
    11. Size = New Size(150, 30)
    12. Font = New Font("Arial", 9, FontStyle.Regular)
    13. NewTextBox() : Controls.Add(TxtBox)
    14. End Sub
    15. Sub NewTextBox()
    16. With TxtBox
    17. .Multiline = False
    18. .ForeColor = Color.FromArgb(100, 100, 100)
    19. .BackColor = Color.FromArgb(50, 50, 50)
    20. .Text = String.Empty
    21. .TextAlign = HorizontalAlignment.Left
    22. .BorderStyle = BorderStyle.None
    23. .Font = Font
    24. .Size = New Size(Width - 16, TxtBox.Height)
    25. .Location = New Point(8, 8)
    26. .UseSystemPasswordChar = UseSystemPasswordChar
    27. End With
    28. End Sub
    29. <Browsable(False), EditorBrowsable(EditorBrowsableState.Never)> Public Shadows Property Multiline() As Boolean
    30. Get
    31. Return False
    32. End Get
    33. Set(ByVal value As Boolean)
    34. End Set
    35. End Property
    36. Private _FocusColor As Color = Color.FromArgb(70, 70, 70)
    37. Public Property FocusColor() As Color
    38. Get
    39. Return _FocusColor
    40. End Get
    41. Set(ByVal value As Color)
    42. _FocusColor = value
    43. End Set
    44. End Property
    45. Private _FocusHighLight As Boolean = True
    46. Public Property FocusHighLight() As Boolean
    47. Get
    48. Return _FocusHighLight
    49. End Get
    50. Set(ByVal value As Boolean)
    51. _FocusHighLight = value
    52. End Set
    53. End Property
    54. Private _TextAlignment As HorizontalAlignment
    55. Public Shadows Property TextAlignment() As HorizontalAlignment
    56. Get
    57. Return _TextAlignment
    58. End Get
    59. Set(ByVal value As HorizontalAlignment)
    60. _TextAlignment = value
    61. TxtBox.TextAlign = value
    62. Invalidate()
    63. End Set
    64. End Property
    65. Private _UseSystemPasswordChar As Boolean = False
    66. Public Shadows Property UseSystemPasswordChar() As Boolean
    67. Get
    68. Return _UseSystemPasswordChar
    69. End Get
    70. Set(ByVal value As Boolean)
    71. _UseSystemPasswordChar = value
    72. TxtBox.UseSystemPasswordChar = UseSystemPasswordChar
    73. Invalidate()
    74. End Set
    75. End Property
    76. Private _MaxLength As Integer = 1000
    77. Public Shadows Property MaxLength() As Integer
    78. Get
    79. Return _MaxLength
    80. End Get
    81. Set(ByVal value As Integer)
    82. _MaxLength = value
    83. TxtBox.MaxLength = MaxLength
    84. Invalidate()
    85. End Set
    86. End Property
    87. Private Sub KeyHandel(sender As Object, e As KeyEventArgs) Handles TxtBox.KeyDown
    88. MyBase.OnKeyDown(e)
    89. End Sub
    90. Protected Overrides Sub OnReSize(ByVal e As System.EventArgs)
    91. TxtBox.Size = New Size(Width - 16, TxtBox.Height)
    92. TxtBox.Location = New Point(8, CInt(Height / 2 - (TxtBox.Size.Height / 2)))
    93. Invalidate()
    94. End Sub
    95. Protected Overrides Sub OnForeColorChanged(ByVal e As System.EventArgs)
    96. TxtBox.ForeColor = ForeColor
    97. Invalidate()
    98. End Sub
    99. Protected Overrides Sub OnFontChanged(ByVal e As System.EventArgs)
    100. TxtBox.Font = Font
    101. TxtBox.Location = New Point(8, CInt(Height / 2 - (TxtBox.Size.Height / 2)))
    102. Invalidate()
    103. End Sub
    104. Protected Overrides Sub OnMouseClick(ByVal e As MouseEventArgs)
    105. TxtBox.Focus()
    106. End Sub
    107. Protected Overrides Sub OnGotFocus(ByVal e As System.EventArgs)
    108. MyBase.OnGotFocus(e)
    109. TxtBox.Focus()
    110. End Sub
    111. Sub TextChangeTxtBox() Handles TxtBox.TextChanged
    112. Text = TxtBox.Text
    113. End Sub
    114. Sub TextChange() Handles MyBase.TextChanged
    115. TxtBox.Text = Text
    116. End Sub
    117. Dim IsEnter As Boolean = False
    118. Protected Overrides Sub OnEnter(ByVal e As System.EventArgs)
    119. If _FocusHighLight = True Then
    120. IsEnter = True
    121. Invalidate()
    122. End If
    123. MyBase.OnEnter(e)
    124. End Sub
    125. Protected Overrides Sub OnLeave(ByVal e As System.EventArgs)
    126. If _FocusHighLight = True Then
    127. IsEnter = False
    128. Invalidate()
    129. End If
    130. MyBase.OnLeave(e)
    131. End Sub
    132. Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
    133. G = e.Graphics : G.SmoothingMode = SmoothingMode.AntiAlias
    134. G.Clear(BackColor)
    135. G.FillRectangle(New SolidBrush(Color.FromArgb(200, 80, 80, 80)), New Rectangle(0, 0, Width, CInt(Height / 2))) 'Schatten hinten
    136. G.FillRectangle(New SolidBrush(Color.FromArgb(50, 50, 50)), New Rectangle(3, 3, Width - 7, Height - 7)) 'Fläche Innen
    137. G.DrawRectangle(New Pen(Color.FromArgb(0, 0, 0)), New Rectangle(0, 0, Width - 1, Height - 1)) 'Umrandung außen
    138. G.DrawRectangle(New Pen(Color.FromArgb(0, 0, 0)), New Rectangle(3, 3, Width - 7, Height - 7)) 'Innen umrandung
    139. If IsEnter = True Then
    140. G.DrawRectangle(New Pen(_FocusColor), New Rectangle(0, 0, Width - 1, Height - 1))
    141. End If
    142. End Sub
    143. End Class


    Lg Steve
    @Steven Meinst Du vielleicht so was - Stichwort WatermarkTextBox:
    Hintergrundtext in TextBox
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!