1

在 C# Windows 窗体应用程序中,如何以编程方式使其在用户单击(或选项卡)上/(到)文本框时,清除默认文本并将字体样式恢复为 Windows 默认值?

我只是在做一个基本的测试表格。我不会包含我正在使用的失败的代码,因为我已经尝试了很多不同的东西。我有点需要帮助填补空白。我将 textBox 中的文本设为灰色和斜体。当文本框被选项卡或单击时,文本需要消失,字体样式和颜色需要设置回 windows 默认值。我整天都在搞这个,我知道它应该很简单,但我一辈子都无法弄清楚。求救!我在网上看到的大多数信息都有 ASP、HTML 和 Java,但我似乎无法偶然发现 C# 示例。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();
    }

    private void textBox1_Click(object sender, EventArgs e)
    {

    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {

    }

}
}

我进行了广泛搜索,找不到用 C# 编写的好示例。几天前我跑了一个页面,但还没有上市,所以我没有注意它,现在我再也找不到它了。我尝试了几种方法,现在我很困惑。

4

2 回答 2

2

您需要存储一个值来指示文本已更改。我会考虑创建一个派生的 TextBox,如下所示:

/// <summary>
/// Represents a Windows TextBox control that displays placeholder text when the
/// control is empty.
/// </summary>
public class PlaceholderTextBox : TextBox
{
    private bool _set;
    private Color _valueForeColor;
    private Color _valueBackColor;
    private Font _valueFont;
    private Color? _PlaceholderForeColor;
    private Color? _PlaceholderBackColor;
    private Font _PlaceholderFont;

    /// <summary>
    /// Gets or sets the text that is shown when the <see cref="TextBox"/> is empty.
    /// </summary>
    [Browsable(true)]
    [Category("Appearance")]
    [Description ("The text that is shown when the TextBox is empty.")]
    [DefaultValue("")]
    public string PlaceholderText { get; set; }

    /// <summary>
    /// Gets or sets the <see cref="Color"/> of the placeholder text.
    /// </summary>
    [Browsable(true)]
    [Category("Appearance")]
    [Description("The color of the placeholder text.")]
    public Color PlaceholderForeColor
    {
        get { return _PlaceholderForeColor ?? _valueForeColor; } 
        set
        {
            if (value == _valueForeColor)
                _PlaceholderForeColor = null;
            else
                _PlaceholderForeColor = value;
        }
    }

    /// <summary>
    /// Gets or sets the <see cref="Color"/> of the background when displaying placeholder text.
    /// </summary>
    [Browsable(true)]
    [Category("Appearance")]
    [Description("The color of the background when displaying placeholder text.")]
    public Color PlaceholderBackColor
    {
        get { return _PlaceholderBackColor ?? _valueBackColor; } 
        set
        {
            if (value == _valueBackColor)
                _PlaceholderBackColor = null;
            else
                _PlaceholderBackColor = value;
        }
    }

    /// <summary>
    /// Gets or sets the <see cref="Font"/> used by the control when displaying placeholder text.
    /// </summary>
    [Browsable(true)]
    [Category("Appearance")]
    [Description("the Font used by the control when displaying placeholder text.")]
    public Font PlaceholderFont
    {
        get { return _PlaceholderFont ?? Font; }
        set { _PlaceholderFont = value.Equals(Font) ? null : value; }
    }

    /// <summary>
    /// Gets or sets the foreground color of the control.
    /// </summary>
    /// <returns>
    /// A <see cref="Color"/> that represents the control's foreground color.
    /// </returns>
    public override Color ForeColor
    {
        get { return _valueForeColor; }
        set
        {
            _valueForeColor = value;
            if(_set)
                base.ForeColor = value;
        }
    }

    /// <summary>
    /// Gets or sets the background color of the control.
    /// </summary>
    /// <returns>
    /// A <see cref="Color"/> that represents the background of the control.
    /// </returns>
    public override Color BackColor
    {
        get { return _valueBackColor; }
        set
        {
            _valueBackColor = value;
            if(_set)
                base.BackColor = value;
        }
    }

    /// <summary>
    /// Gets or sets the font of the text displayed by the control.
    /// </summary>
    /// <returns>
    /// The <see cref="Font"/> to apply to the text displayed by the control. 
    /// The default is the value of the <see cref="Control.DefaultFont"/> property.
    /// </returns>
    public override Font Font
    {
        get { return _valueFont; }
        set
        {
            _valueFont = value;
            if(_set)
                base.Font = value;
        }
    }


    public PlaceholderTextBox()
    {
        _valueForeColor = base.ForeColor;
        _valueBackColor = base.BackColor;
        _valueFont = base.Font;
    }


    /// <summary>
    /// Raises the <see cref="E:System.Windows.Forms.Control.GotFocus"/> event.
    /// </summary>
    /// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data.</param>
    protected override void OnGotFocus(EventArgs e)
    {
        if (!_set)
        {
            Text = String.Empty;
            base.ForeColor = _valueForeColor;
            base.BackColor = _valueBackColor;
            base.Font = _valueFont;
            _set = true;
        }

        base.OnGotFocus(e);
    }

    /// <summary>
    /// Raises the <see cref="E:System.Windows.Forms.Control.LostFocus"/> event.
    /// </summary>
    /// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data. </param>
    protected override void OnLostFocus(EventArgs e)
    {
        if (Text == String.Empty)
        {
            Text = PlaceholderText;
            base.ForeColor = PlaceholderForeColor;
            base.BackColor = PlaceholderBackColor;
            base.Font = PlaceholderFont;
            _set = false;
        }

        base.OnLostFocus(e);
    }
}
于 2013-03-21T21:42:46.490 回答
1

GotFocus该活动可能适合您吗?

于 2013-03-21T21:35:40.507 回答