Export (0) Print
Expand All

TextBoxRenderer Class

Provides methods used to render a text box control with visual styles. This class cannot be inherited.

System.Object
  System.Windows.Forms.TextBoxRenderer

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

'Declaration
Public NotInheritable Class TextBoxRenderer

The TextBoxRenderer type exposes the following members.

  NameDescription
Public propertyStatic memberIsSupportedGets a value indicating whether the TextBoxRenderer class can be used to draw a text box with visual styles.
Top

  NameDescription
Public methodStatic memberDrawTextBox(Graphics, Rectangle, TextBoxState)Draws a text box control in the specified state and bounds.
Public methodStatic memberDrawTextBox(Graphics, Rectangle, String, Font, TextBoxState)Draws a text box control in the specified state and bounds, and with the specified text.
Public methodStatic memberDrawTextBox(Graphics, Rectangle, String, Font, Rectangle, TextBoxState)Draws a text box control in the specified state and bounds, and with the specified text and text bounds.
Public methodStatic memberDrawTextBox(Graphics, Rectangle, String, Font, TextFormatFlags, TextBoxState)Draws a text box control in the specified state and bounds, and with the specified text and text formatting.
Public methodStatic memberDrawTextBox(Graphics, Rectangle, String, Font, Rectangle, TextFormatFlags, TextBoxState)Draws a text box control in the specified state and bounds, and with the specified text, text bounds, and text formatting.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

The TextBoxRenderer class provides a set of static methods that can be used to render a text box control with the current visual style of the operating system. Rendering a control refers to drawing the user interface of a control. This is useful if you are drawing a custom control that should have the appearance of the current visual style. To draw a text box, use one of the DrawTextBox methods. These methods provide a variety of options, such as applying text formatting or specifying text bounds.

If visual styles are enabled in the operating system and visual styles are applied to the client area of application windows, DrawTextBox will draw the text box with the current visual style. Otherwise, DrawTextBox will throw an InvalidOperationException. To determine whether the members of this class can be used, you can check the value of the IsSupported property.

This class wraps the functionality of a System.Windows.Forms.VisualStyles.VisualStyleRenderer that is set to one of the elements of the System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox.TextEdit class. For more information, see Rendering Controls with Visual Styles.

Windows XP Home Edition, Windows XP Professional x64 Edition, Windows Server 2003 Platform Note: Visual styles are supported only on these platforms.

The following code example demonstrates how to create a custom control that uses the DrawTextBox method to draw a text box. The control also allows the user to select one of the TextFormatFlags values to apply to the text box text.

Imports System
Imports System.Text
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Windows.Forms.VisualStyles

Namespace TextBoxRendererSample

    Class Form1
        Inherits Form

        Public Sub New()
            Me.Size = New Size(350, 200)
            Dim TextBox1 As New CustomTextBox()
            Controls.Add(TextBox1)
        End Sub

        <STAThread()> _
        Shared Sub Main()
            ' The call to EnableVisualStyles below does not affect whether  
            ' TextBoxRenderer draws the text box; as long as visual styles  
            ' are enabled by the operating system, TextBoxRenderer will  
            ' draw the text box.
            Application.EnableVisualStyles()
            Application.Run(New Form1())
        End Sub 
    End Class 

    Public Class CustomTextBox
        Inherits Control

        Private textFlags As TextFormatFlags = TextFormatFlags.Default 
        Private WithEvents comboBox1 As New ComboBox()
        Private textBorder As New Rectangle()
        Private textRectangle As New Rectangle()
        Private textMeasurements As New StringBuilder()

        Public Sub New()

            With Me
                .Location = New Point(10, 10)
                .Size = New Size(300, 200)
                .Font = SystemFonts.IconTitleFont
                .Text = "This is a long sentence that will exceed " + _
                    "the text box bounds" 
            End With

            textBorder.Location = New Point(10, 10)
            textBorder.Size = New Size(200, 50)
            textRectangle.Location = New Point(textBorder.X + 2, _
                textBorder.Y + 2)
            textRectangle.Size = New Size(textBorder.Size.Width - 4, _
                textBorder.Height - 4)

            comboBox1.Location = New Point(10, 100)
            comboBox1.Size = New Size(150, 20)

            ' Populate the combo box with the TextFormatFlags value names. 
            Dim name As String 
            For Each name In [Enum].GetNames(GetType(TextFormatFlags))
                comboBox1.Items.Add(name)
            Next name

            comboBox1.SelectedIndex = 0
            Me.Controls.Add(comboBox1)
        End Sub 

        ' Use DrawText with the current TextFormatFlags. 
        Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
            MyBase.OnPaint(e)

            If TextBoxRenderer.IsSupported Then
                TextBoxRenderer.DrawTextBox(e.Graphics, textBorder, Me.Text, _
                    Me.Font, textRectangle, textFlags, TextBoxState.Normal)
                Me.Parent.Text = "CustomTextBox Enabled" 
            Else 
                Me.Parent.Text = "CustomTextBox Disabled" 
            End If 
        End Sub 

        ' Assign the combo box selection to the display text. 
        Private Sub comboBox1_SelectedIndexChanged(ByVal sender As Object, _
            ByVal e As EventArgs) Handles comboBox1.SelectedIndexChanged

            Me.textFlags = CType([Enum].Parse(GetType(TextFormatFlags), _
                CStr(comboBox1.Items(comboBox1.SelectedIndex))), _
                TextFormatFlags)
            Invalidate()
        End Sub 

    End Class 
End Namespace

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft