C# 如何将文本框字体更改回默认值,并在单击或选项卡切换到时清除默认文本?
在C#Windows窗体应用程序中,当用户单击(或制表符)文本框时,默认文本被清除并将字体样式恢复为Windows默认样式,我如何通过编程使其恢复到 我只是在做一个基本的测试表格。我不打算包含我正在使用的失败代码,因为我已经尝试了很多不同的东西。我需要有人帮我填空。我会把文本框中的文本变成灰色和斜体。当文本框被标记或单击时,文本需要消失,字体样式和颜色需要设置回windows默认值。我花了一整天的时间来搞砸这件事,我知道这应该很简单,但我一辈子都搞不清楚。求救!我在网上看到的大多数信息都有ASP、HTML和Java,但我似乎不会偶然发现一个C#示例C# 如何将文本框字体更改回默认值,并在单击或选项卡切换到时清除默认文本?,c#,fonts,textbox,default,C#,Fonts,Textbox,Default,在C#Windows窗体应用程序中,当用户单击(或制表符)文本框时,默认文本被清除并将字体样式恢复为Windows默认样式,我如何通过编程使其恢复到 我只是在做一个基本的测试表格。我不打算包含我正在使用的失败代码,因为我已经尝试了很多不同的东西。我需要有人帮我填空。我会把文本框中的文本变成灰色和斜体。当文本框被标记或单击时,文本需要消失,字体样式和颜色需要设置回windows默认值。我花了一整天的时间来搞砸这件事,我知道这应该很简单,但我一辈子都搞不清楚。求救!我在网上看到的大多数信息都有ASP
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#编写的好例子。几天前我浏览了一个页面,但是还没有上市,所以我没有注意到它,现在我再也找不到了。我试过几种方法,现在我都糊涂了 该事件是否适用于您?您需要存储一个值以指示文本已更改。我会考虑创建一个派生的文本框,如下所示:
/// <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);
}
}
//
///表示Windows TextBox控件,该控件在
///控件为空。
///
公共类占位符文本框:文本框
{
私有布尔集;
私人色彩(彩色);;
私有颜色_值背景颜色;
私有字体_valueFont;
私人色彩?\u前景色;
私人色彩?\u占位符背景色;
专用字体(占位符字体);;
///
///获取或设置当文本为空时显示的文本。
///
[可浏览(正确)]
[类别(“外观”)]
[说明(“文本框为空时显示的文本。”)]
[默认值(“”)
公共字符串占位符文本{get;set;}
///
///获取或设置占位符文本的名称。
///
[可浏览(正确)]
[类别(“外观”)]
[说明(“占位符文本的颜色”)]
公共颜色前景色
{
获取{return\u placeholder forecolor???\u valueForeColor;}
设置
{
如果(值==\u值前景色)
_占位符前景色=空;
其他的
_占位符前景色=值;
}
}
///
///获取或设置显示占位符文本时的背景。
///
[可浏览(正确)]
[类别(“外观”)]
[说明(“显示占位符文本时的背景颜色”)]
公共颜色背景色
{
获取{return\u PlaceholderBackColor???\u valueBackColor;}
设置
{
如果(值==\u值背景色)
_占位符backcolor=null;
其他的
_占位符背景色=值;
}
}
///
///获取或设置控件在显示占位符文本时使用的。
///
[可浏览(正确)]
[类别(“外观”)]
[说明(“显示占位符文本时控件使用的字体。”)]
公共字体占位符字体
{
获取{return}
设置{u占位符Font=value.Equals(Font)?null:value;}
}
///
///获取或设置控件的前景色。
///
///
///表示控件的前景色的。
///
公共覆盖颜色前景色
{
获取{return\u valueForeColor;}
设置
{
_valueForeColor=值;
如果(_集)
base.ForeColor=值;
}
}
///
///获取或设置控件的背景色。
///
///
///表示控件背景的。
///
公共覆盖颜色背景色
{
获取{return\u valueBackColor;}
设置
{
_valueBackColor=值;
如果(_集)
base.BackColor=值;
}
}
///
///获取或设置控件显示的文本的字体。
///
///
///要应用于控件显示的文本的。
///默认值是属性的值。
///
公共覆盖字体
{
获取{return\u valueFont;}
设置
{
_valueFont=值;
如果(_集)
base.Font=值;
}
}
公共占位符文本框()
{
_valueForeColor=base.ForeColor;
_valueBackColor=base.BackColor;
_valueFont=base.Font;
}
///
///引发事件。
///
///包含事件数据的。
受保护的覆盖无效OnGotFocus(事件参数e)
{
如果(!\u集)
{
Text=String.Empty;
base.ForeColor=\u valueForeColor;
base.BackColor=\u valueBackColor;
base.Font=\u valueFont;
_设置=真;
}
基地,昂戈特福克斯(e);;
}
///
///引发事件。
///
///包含事件数据的。
受保护的覆盖void OnLostFocus(事件参数e)
{
if(Text==String.Empty)
{
文本=占位符文本;
base.ForeColor=占位符ForeColor;
base.BackColor=占位符BackColor;
base.Font=占位符字体;
_set=false;
}
base.OnLostFocus(e);
}
}
我真的认为OP知道如何设置占位符文本,只需要知道GotFocus
事件存在。然而,这是一个优雅的,全面的解决方案,OP应该绝对考虑。我昨晚躺在床上读这篇文章。这看起来正是我需要的。我今天在wqork。我不是程序员。我拿了