Läs på engelska Redigera

Dela via


TextBoxRenderer Class

Definition

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

C#
public sealed class TextBoxRenderer
C#
public static class TextBoxRenderer
Inheritance
TextBoxRenderer

Examples

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.

C#
using System;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

namespace TextBoxRendererSample
{
    class Form1 : Form
    {
        public Form1()
            : base()
        {
            this.Size = new Size(350, 200);
            CustomTextBox TextBox1 = new CustomTextBox();
            Controls.Add(TextBox1);
        }

        [STAThread]
        static void 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());
        }
    }

    public class CustomTextBox : Control
    {
        private TextFormatFlags textFlags = TextFormatFlags.Default;
        ComboBox comboBox1 = new ComboBox();
        Rectangle textBorder = new Rectangle();
        Rectangle textRectangle = new Rectangle();
        StringBuilder textMeasurements = new StringBuilder();

        public CustomTextBox()
            : base()
        {
            this.Location = new Point(10, 10);
            this.Size = new Size(300, 200);
            this.Font = SystemFonts.IconTitleFont;
            this.Text = "This is a long sentence that will exceed " +
                "the text box bounds";

            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);
            comboBox1.SelectedIndexChanged +=
                new EventHandler(comboBox1_SelectedIndexChanged);

            // Populate the combo box with the TextFormatFlags value names.
            foreach (string name in Enum.GetNames(typeof(TextFormatFlags)))
            {
                comboBox1.Items.Add(name);
            }

            comboBox1.SelectedIndex = 0;
            this.Controls.Add(comboBox1);
        }

        // Use DrawText with the current TextFormatFlags.
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            if (TextBoxRenderer.IsSupported)
            {
                TextBoxRenderer.DrawTextBox(e.Graphics, textBorder, this.Text,
                    this.Font, textRectangle, textFlags, TextBoxState.Normal);

                this.Parent.Text = "CustomTextBox Enabled";
            }
            else
            {
                this.Parent.Text = "CustomTextBox Disabled";
            }
        }

        // Assign the combo box selection to the display text.
        void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.textFlags = (TextFormatFlags)Enum.Parse(
                typeof(TextFormatFlags),
                (string)comboBox1.Items[comboBox1.SelectedIndex]);
            Invalidate();
        }
    }
}

Remarks

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.

Properties

IsSupported

Gets a value indicating whether the TextBoxRenderer class can be used to draw a text box with visual styles.

Methods

DrawTextBox(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.

DrawTextBox(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.

DrawTextBox(Graphics, Rectangle, String, Font, TextBoxState)

Draws a text box control in the specified state and bounds, and with the specified text.

DrawTextBox(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.

DrawTextBox(Graphics, Rectangle, TextBoxState)

Draws a text box control in the specified state and bounds.

Applies to

Produkt Versioner
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

See also