TextBoxRenderer Class

Note: This class is new in the .NET Framework version 2.0.

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

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public ref class TextBoxRenderer sealed
public final class TextBoxRenderer
public final class TextBoxRenderer

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.

#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
#using <System.dll>

using namespace System;
using namespace System::Text;
using namespace System::Drawing;
using namespace System::Windows::Forms;
using namespace System::Windows::Forms::VisualStyles;

namespace TextBoxRendererSample
    public ref class CustomTextBox : public Control
        TextFormatFlags textFlags;
        ComboBox^ textFormatFlagsComboBox;
        Rectangle textBorder;
        Rectangle textRectangle;
        StringBuilder^ textMeasurements;

            textFlags = TextFormatFlags::Default;
            textFormatFlagsComboBox = gcnew ComboBox();

            textMeasurements = gcnew StringBuilder();

            this->Location = Point(10, 10);
            this->Size = System::Drawing::Size(300, 200);
            this->Font = SystemFonts::IconTitleFont;
            this->Text = "This is a long sentence that will exceed " +
                "the text box bounds";

            textBorder.Location = Point(10, 10);
            textBorder.Size = System::Drawing::Size(200, 50);
            textRectangle.Location = Point(textBorder.X + 2,
                textBorder.Y + 2);
            textRectangle.Size =  System::Drawing::Size(textBorder.Size.Width - 4,
                textBorder.Height - 4);

            textFormatFlagsComboBox->Location = Point(10, 100);
            textFormatFlagsComboBox->Size = System::Drawing::Size(150, 20);
            textFormatFlagsComboBox->SelectedIndexChanged +=
                gcnew EventHandler(this, 

            // Populate the combo box with the TextFormatFlags value names.
            for each (String^ name in Enum::GetNames(TextFormatFlags::typeid))

            textFormatFlagsComboBox->SelectedIndex = 0;

        // Use DrawText with the current TextFormatFlags.

        virtual void OnPaint(PaintEventArgs^ e) override

            if (TextBoxRenderer::IsSupported)
                TextBoxRenderer::DrawTextBox(e->Graphics, textBorder, this->Text,
                    this->Font, textRectangle, textFlags, TextBoxState::Normal);

                this->Parent->Text = "CustomTextBox Enabled";
                this->Parent->Text = "CustomTextBox Disabled";

        // Assign the combo box selection to the display text.
        void textFormatFlagsComboBox_SelectedIndexChanged(
            Object^ sender, EventArgs^ e)
            this->textFlags = (TextFormatFlags)Enum::Parse(


    public ref class Form1 : public Form
            this->Size = System::Drawing::Size(350, 200);
            CustomTextBox^ textBox1 = gcnew CustomTextBox();

using namespace TextBoxRendererSample;

int 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::Run(gcnew Form1());


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

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0