如何更改文本框背景文字? [英] How to change text box background text?

查看:131
本文介绍了如何更改文本框背景文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,先生,我正在使用窗口应用程序,我需要一个文本框来要求用户输入您的姓名,它应该用浅色文字书写,当用户单击该文本框时,资料应消失,并且用户应该能够像普通文字一样书写.请帮助我.

Hi sir, i am working with window application, i need a text box to ask the user to enter your name, it should be written in text box with lite color, when user clicks on text box the mater should disappear and the user should able to write like normal text. please help me.

推荐答案

将新的TextBox拖放到您的表单上.转到文本框的属性,然后将文本"设置为:
Drag and drop a new TextBox on your form. Go to the textbox''s properties and set Text as:
Please Enter Name...


更改ForeColor属性并设置所需的颜色.

单击事件图标并添加以下两个事件:
进入,离开.

在每个事件上使用以下代码:


Change the ForeColor property and set the color you want.

Click on the events icon and add the following two events:
Enter, Leave.

Use the following code on each event:

private void txt_YourTextBox_Enter(object sender, EventArgs e)
{
   if (txt_YourTextBox.Text == "Please Enter Name...")
   {
      txt_YourTextBox.ForeColor = Color.Black;
      txt_YourTextBox.Text = "";
   }
}

private void txt_YourTextBox_Leave(object sender, EventArgs e)
{
   if (txt_YourTextBox.Text.Length == 0)
   {
      txt_YourTextBox.ForeColor = Color.BlueViolet;
      txt_YourTextBox.Text = "Please Enter Name...";
   }
}




这是解决方案,

您必须以load
的形式更改文本框前的颜色
Hi,

This is the solution,

you have to change the textbox fore color in the form load

textBox1.ForeColor = Color.Black;




并且您必须在文本框中的鼠标单击事件中编写此代码





and you have to write this code in the text box mouse click event


private void textBox1_MouseClick(object sender, MouseEventArgs e)
{
    textBox1.Text = "";
    textBox1.ForeColor = Color.Black;
}



试试这个:)

谢谢.



Try this :)

Thanks.


您可以使用onFocus和lostFocus事件来实现.

当TextBox获得焦点时,您可以清理文本框,当文本框丢失时,可以根据需要进行绘制.
You can do it using the onFocus and lostFocus event.

When the TextBox gets the focus, you can clean the textbox, and when it lost, you can paint if needed.


这篇关于如何更改文本框背景文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆