顯示「在此輸入...」,直到用戶將文本輸入TextBox
是當今衆所周知的可用性功能。如何在C#中實現此功能?如何實現顯示「在此輸入」的文本框?
我的想法是覆蓋OnTextChanged
,但邏輯從和「這裏類型」處理文本的變化是有點棘手...
顯示「類型這裏」在初始化和在第一次輸入時將其刪除很容易,但是我希望每當輸入的文本變空時顯示消息。
顯示「在此輸入...」,直到用戶將文本輸入TextBox
是當今衆所周知的可用性功能。如何在C#中實現此功能?如何實現顯示「在此輸入」的文本框?
我的想法是覆蓋OnTextChanged
,但邏輯從和「這裏類型」處理文本的變化是有點棘手...
顯示「類型這裏」在初始化和在第一次輸入時將其刪除很容易,但是我希望每當輸入的文本變空時顯示消息。
可以繪製字符串「請在此處鍵入」到文本框的背景,直到空
如果這是ASP.NET那麼你可以嘗試TextBoxWatermark。
如果這是Windows窗體,則已在SO中回答了here。
爲什麼要使用OnTextChanged? 當TextBox獲得焦點時,我會建議刪除文本「Type here」。 當控件失去焦點並且沒有輸入文本時,可以再次顯示文本。
結果相同,不需要棘手的邏輯。
如果這是ASP.NET(相對的WinForms),你可以這樣做:
如果您正在使用jQuery,將它添加到您的文檔準備(或者無論你初始化頁面):
var $textbox = $("textbox selector"); // assumes you select a single text box
if ($textbox.val() == "") {
$textbox.val("Type here to...");
$textbox.one('focus', function() {
$(this).attr('value', '');
});
}
如果您選擇多個文本框(將if語句放在元素中的每個文本框中),則需要進行一些小的重構。
東西已經爲我工作:
this.waterMarkActive = true;
this.textBox.ForeColor = Color.Gray;
this.textBox.Text = "Type here";
this.textBox.GotFocus += (source, e) =>
{
if (this.waterMarkActive)
{
this.waterMarkActive = false;
this.textBox.Text = "";
this.textBox.ForeColor = Color.Black;
}
};
this.textBox.LostFocus += (source, e) =>
{
if (!this.waterMarkActive && string.IsNullOrEmpty(this.textBox.Text))
{
this.waterMarkActive = true;
this.textBox.Text = "Type here";
this.textBox.ForeColor = Color.Gray;
}
};
凡bool waterMarkActive
是一個類的成員變量和textBox
是TextBox
。這可能應該封裝:)雖然這種方法可能存在一些問題,但我目前還沒有意識到任何問題。
我最近發現Windows支持文本框中的水印;他們被稱爲提示橫幅(請參閱here)。這很容易實現:
// Within your class or scoped in a more appropriate location:
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam);
// In your constructor or somewhere more suitable:
SendMessage(textBox.Handle, 0x1501, 1, "Please type here.");
凡textBox
是TextBox
一個實例,0x1501
是Windows消息EM_SETCUEBANNER
的代碼中,wParam
可以是TRUE
(非零)或FALSE
(零),lParam
是你想展示的水印。 wParam
指示何時應該顯示提示橫幅;如果設置爲TRUE
,則即使控件具有焦點,也會顯示提示橫幅。
喜歡這個'DllImport'!謝謝您的發佈。 – Kristopher 2015-12-08 21:17:47
這應該是被接受的答案。提示橫幅想法很好.. – 2016-03-19 16:10:08
如果您使用多行文本框,提示橫幅將不起作用。 – 123iamking 2017-03-06 14:05:12
基於@ Pooven的答案(謝謝!),我創建了這個類。適用於我。
/// <summary>
/// A textbox that supports a watermak hint.
/// </summary>
public class WatermarkTextBox : TextBox
{
/// <summary>
/// The text that will be presented as the watermak hint
/// </summary>
private string _watermarkText = "Type here";
/// <summary>
/// Gets or Sets the text that will be presented as the watermak hint
/// </summary>
public string WatermarkText
{
get { return _watermarkText; }
set { _watermarkText = value; }
}
/// <summary>
/// Whether watermark effect is enabled or not
/// </summary>
private bool _watermarkActive = true;
/// <summary>
/// Gets or Sets whether watermark effect is enabled or not
/// </summary>
public bool WatermarkActive
{
get { return _watermarkActive; }
set { _watermarkActive = value; }
}
/// <summary>
/// Create a new TextBox that supports watermak hint
/// </summary>
public WatermarkTextBox()
{
this._watermarkActive = true;
this.Text = _watermarkText;
this.ForeColor = Color.Gray;
GotFocus += (source, e) =>
{
RemoveWatermak();
};
LostFocus += (source, e) =>
{
ApplyWatermark();
};
}
/// <summary>
/// Remove watermark from the textbox
/// </summary>
public void RemoveWatermak()
{
if (this._watermarkActive)
{
this._watermarkActive = false;
this.Text = "";
this.ForeColor = Color.Black;
}
}
/// <summary>
/// Applywatermak immediately
/// </summary>
public void ApplyWatermark()
{
if (!this._watermarkActive && string.IsNullOrEmpty(this.Text)
|| ForeColor == Color.Gray)
{
this._watermarkActive = true;
this.Text = _watermarkText;
this.ForeColor = Color.Gray;
}
}
/// <summary>
/// Apply watermak to the textbox.
/// </summary>
/// <param name="newText">Text to apply</param>
public void ApplyWatermark(string newText)
{
WatermarkText = newText;
ApplyWatermark();
}
}
我剛開始學習C#這學期,所以我不是專家,但是這個工作對我來說: (這是使用Windows窗體)
private void Form1_Load(object sender, EventArgs e)
{
textBox1.SelectionStart = 0; //This keeps the text
textBox1.SelectionLength = 0; //from being highlighted
textBox1.ForeColor = Color.Gray;
}
private void textBox_MouseMove(object sender, MouseEventArgs e)
{
Cursor.Current = Cursors.IBeam; //Without this the mouse pointer shows busy
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (textBox1.Text.Equals("Type here...") == true)
{
textBox1.Text = "";
textBox1.ForeColor = Color.Black;
}
}
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (textBox1.Text.Equals(null) == true || textBox1.Text.Equals("") == true)
{
textBox1.Text = "Type here...";
textBox1.ForeColor = Color.Gray;
}
}
產生相似的輸出TO HTML WATERMARK
這是我的文本框「水印」或「預覽」文本的代碼 - 非常棒!使用Windows窗體應用程序。
備註:本示例有3個文本框,分別爲「鼠標離開」事件和「鼠標輸入」事件分別具有以下方法。
private void textBoxFav_Leave(object sender, EventArgs e) {
TextBox textbox = (TextBox)sender;
if (String.IsNullOrWhiteSpace(textbox.Text)) {
textbox.ForeColor = Color.Gray;
if (textbox.Name == "textBoxFavFood") {
textbox.Text = "Favorite Food";
}
else if (textbox.Name == "textBoxFavDrink") {
textbox.Text = "Favorite Drink";
}
else if (textbox.Name == "textBoxFavDesert") {
textbox.Text = "Favorite Desert";
}
}
else {
textbox.ForeColor = Color.Black;
}
}
private void textBoxFav_Enter(object sender, EventArgs e) {
TextBox textbox = (TextBox)sender;
if (textbox.Text == "Favorite Food" || textbox.Text == "Favorite Drink" || textbox.Text == "Favorite Desert") {
textbox.Text = "";
textbox.ForeColor = Color.Black;
}
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern Int32 SendMessage(IntPtr hWnd, int msg, int wParam, [MarshalAs(UnmanagedType.LPWStr)]string lParam);
const int EM_SETCUEBANNER = 0x1501;
public Form1()
{
InitializeComponent();
SendMessage(textBox1.Handle, EM_SETCUEBANNER, 1, "Username");
SendMessage(textBox2.Handle, EM_SETCUEBANNER, 1, "Password");
}
const int EM_SETCUEBANNER = 0x1501; – bitrixhater 2016-05-21 13:47:04
將您的評論合併到代碼中,感謝您的輸入.. – 2017-10-19 16:50:16
在C#中的文本框的最後一個版本具有以下屬性:PlaceholderText,其中完成所有工作。所以你只需要設置「Type here ...」作爲這個屬性的值。
認爲您需要向我們展示該用戶的MSDN鏈接,因爲我無法找到文檔中的任何內容來備份它。 – 2017-10-19 16:46:32
如果您想要避免控制調整大小問題和數據綁定問題,並使代碼更簡單(確定,這是可疑的),您可以使用標籤並切換其可見性。然後根據@喬爾的回答
private void FilterComboBox_GotFocus(object sender, EventArgs e)
{
FilterWatermarkLabel.Visible = false;
}
private void FilterComboBox_LostFocus(object sender, EventArgs e)
{
if (!FilterWatermarkLabel.Visible && string.IsNullOrEmpty(FilterComboBox.Text))
{
FilterWatermarkLabel.Visible = true;
}
}
的圖像,也避免了數據綁定問題的另一種方法是在這裏 https://msdn.microsoft.com/en-us/library/bb613590(v=vs.100).aspx
。我確定他的課(感謝基地!)基於艾哈邁德·蘇萊曼Flasha使用的答案
/// <summary>
/// A textbox that supports a watermak hint.
/// Based on: https://stackoverflow.com/a/15232752
/// </summary>
public class WatermarkTextBox : TextBox
{
/// <summary>
/// The text that will be presented as the watermak hint
/// </summary>
private string _watermarkText;
/// <summary>
/// Gets or Sets the text that will be presented as the watermak hint
/// </summary>
public string WatermarkText
{
get { return _watermarkText; }
set { _watermarkText = value; }
}
/// <summary>
/// Whether watermark effect is enabled or not
/// </summary>
private bool _watermarkActive;
/// <summary>
/// Gets or Sets whether watermark effect is enabled or not
/// </summary>
public bool WatermarkActive
{
get { return _watermarkActive; }
set { _watermarkActive = value; }
}
/// <summary>
/// Create a new TextBox that supports watermak hint
/// </summary>
public WatermarkTextBox()
{
this.WatermarkActive = _watermarkActive;
this.Text = _watermarkText;
}
protected override void OnCreateControl()
{
base.OnCreateControl();
if (this.WatermarkActive)
CheckWatermark();
}
protected override void OnGotFocus(EventArgs e)
{
base.OnGotFocus(e);
CheckWatermark();
}
protected override void OnLostFocus(EventArgs e)
{
base.OnLostFocus(e);
CheckWatermark();
}
public void CheckWatermark()
{
if ((this.WatermarkActive) && String.IsNullOrWhiteSpace(this.Text))
{
ForeColor = Color.Gray;
this.Text = _watermarkText;
}
else if ((this.WatermarkActive) && (!String.IsNullOrWhiteSpace(this.Text)))
{
if (this.Text == _watermarkText)
this.Text = "";
ForeColor = Color.Black;
}
else
ForeColor = Color.Black;
}
}
下面的類:
public class TextBoxHint : TextBox
{
string _hint;
[Localizable(true)]
public string Hint
{
get { return _hint; }
set { _hint = value; OnHintChanged(); }
}
protected virtual void OnHintChanged()
{
SendMessage(this.Handle, EM_SETCUEBANNER, 1, _hint);
}
const int EM_SETCUEBANNER = 0x1501;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern Int32 SendMessage(IntPtr hWnd, int msg, int wParam, [MarshalAs(UnmanagedType.LPWStr)]string lParam);
}
顯示「鍵入這裏......」直到用戶將文本輸入到TextBox是當今衆所周知的可用性功能。如何在C#中實現此功能?
設置textbox.text爲「類型這裏...「
創建活動,說box_click()
- >將這個代碼在你的方法
private void box_Click(object sender, EventArgs e)
{
Textbox b = (Textbox)sender;
b.Text = null;
}
現在這個方法分配給了 」輸入「 事件您文本框(可能是一個或多個)
是這個ASP.NET還是windows窗體? – M4N 2010-03-21 12:54:08
你對什麼技術感興趣?它的ASP.NET,WinForms或WPF,也許silverlight? 任何方式它被稱爲「水印文本框」,你可以找到很多關於每種技術。 – Shimmy 2010-03-21 12:57:42
WinForms。 - 啊,不知道這個詞。謝謝! – mafu 2010-03-21 13:06:30