简体   繁体   中英

Temperature TextBox In C#

我需要一些代码将标准C#TextBox转换为温度TextBox,这意味着在文本框中的文本末尾添加“°C”,并使用不同于默认颜色的另一种颜色。

To get the degree symbol you can use character code 176 eg

Char degree = (Char)176

You can then append this to your textbox content or I would just add a label to the right of the textbox with the degree symbol if you want to control the forecolor easily.

TextBox is a plain text editor. To get different colours you would have to muck around with a rich text box. Why not put the "°C" in a label positioned to the right of the text box? That would also make your parsing and rendering code much easier.

You could probably create your own control which inherits from TextBox and then override Text property to automaticaly add °C though other color inside the same TextBox could be problem.

Why you want to have °C in TextBox ? Can't it just be label right after TextBox ? You can set static text and color to what you want.

The other solutions proposed here are probably sufficient for your application; however, if you had the need to implement this with re-usability in mind, here is a custom control solution which you may extend to better suit your application:

public class TemperatureTextBox : ContainerControl
{
    private const int BORDER_SIZE = 1;

    // Exposes text property of text box,
    // expose other text box properties as needed:
    public override string Text
    {
        get { return textBox.Text; }
        set { textBox.Text = value; }
    }

    private TextBox textBox = new TextBox()
    {
        Text = string.Empty,
        BorderStyle = BorderStyle.None,
        Dock = DockStyle.Fill
    };

    private Label label = new Label()
    {
        Text = "°C",
        TextAlign = ContentAlignment.MiddleCenter,
        Size = new Size()
        {
            Width = 32
        },
        BackColor = SystemColors.Window,
        Dock = DockStyle.Right
    };

    public TemperatureTextBox()
    {
        this.BackColor = SystemColors.Window;
        this.Padding = new Padding(BORDER_SIZE);
        this.Controls.Add(label);
        this.Controls.Add(textBox);
        this.PerformLayout();
    }

    // Constrain control size to textbox height plus top and bottom border:
    protected override void OnResize(EventArgs e)
    {
        base.OnResize(e);
        this.Height = (textBox.Height + (BORDER_SIZE * 2));
    }

    // Render a border around the control:
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        e.Graphics.DrawRectangle(
            SystemPens.ControlDarkDark,
            new Rectangle()
            {
                Width = (this.Width - BORDER_SIZE),
                Height = (this.Height - BORDER_SIZE)
            });
    }
}

Simply create a new class and drop this code in and rebuild you solution. It will create a new TemperatureTextBox control in the toolbox which can be dropped onto a new form and visually designed.

This example exposes the Text property of the underlying text box by overriding the custom control's text property. You may want to expose other properties, and events depending on what your application needs to accomplish.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM