如何给文本框一个初始值? [英] How to give an initial value to textBox?

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

问题描述

我希望我的 C# 程序具有其 textboxes 的初始值.例如,在其中一个 textboxes 中,它应该显示 请输入您的姓名".

I want my C# program to have initial values for its textboxes. For example, in one of the textboxes, it should say "Please enter your name".

当您在 textbox 上单击(或 tabStop)时,初始值应该消失,用户将能够在 textbox 中输入他们的输入.

When you click (or tabStop) on the textbox, the initial value should disappear and the user will be able to enter their input to the textbox.

我可以使用 click_event 完成所有这些,但是使用这种方法初始文本的不透明度不会降低.我如何才能做到这一点?

I can do all this with click_event, but using this method the initial text would not have less opacity. How am I able to achieve this?

推荐答案

这就是我最终做到的:

Boolean first_time_click = true;

private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.ForeColor = System.Drawing.Color.Gray;
            textBox1.Text = "Enter the Text";
        }

private void For_First_Click()
        {
            if (first_time_click)
            {
                textBox1.Clear();
                textBox1.ForeColor = textBox1.ForeColor = SystemColors.WindowText;
            }
            first_time_click = false;
        }

private void textBox1_Click(object sender, EventArgs e)
        {
            For_First_Click();
        }

这篇关于如何给文本框一个初始值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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