如何给textBox赋予初始值?

发布于 2024-12-04 15:57:23 字数 252 浏览 0 评论 0原文

我希望我的 C# 程序的文本框具有初始值。例如,在其中一个文本框中,应显示“请输入您的姓名”

当您单击(或 tabStop)文本框时,初始值应该消失,用户将能够在文本框中输入内容。

我可以使用 click_event 完成所有这些操作,但使用此方法初始文本的不透明度不会降低。我怎样才能实现这一目标?

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".

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

祁梦 2024-12-11 15:57:23

这就是我最终做到的:

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();
        }

This is how I finally did it:

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();
        }
千柳 2024-12-11 15:57:23

我假设您正在谈论 winform (tabstop),您必须在事件按键中处理它。您可以使用下面的代码:

TextBox1.Select(0, TextBox1.Text.Length);

这将选择文本,一旦用户开始输入,窗口就会为您删除它

您可以使用相同的代码为 TabStop 提供此行为

I assume you are talking about winform (tabstop) you have to handle it within the event key-press. you can use the below code:

TextBox1.Select(0, TextBox1.Text.Length);

this will select the text and window will remove it for you as soon as the user start to typing

you can use the same code to have this behavior also for TabStop

归属感 2024-12-11 15:57:23

您需要做的就是设置文本框的 .Text 属性,并在用户单击(或按 Tab 键)开始输入时使用 GotFocus 事件来清除该框。

请始终记住,除了鼠标之外,还有更多方法可以导航窗体,因此请使用 GotFocus 事件来确定用户何时进入控件,并使用 Validated 事件来确定用户何时更改数据并退出控件。

All you need to do is set the Textbox's .Text property and use GotFocus event to clear the box when the person clicks (or tabs) into it to start typing.

Always remember that there are more ways than the mouse to navigate a form, so use the GotFocus event to determine when the user enters a control, and use the Validated event to determine when they've changed data and exited the control.

记忆消瘦 2024-12-11 15:57:23

对于这种类型的效果,您需要 java 脚本。因为 java 脚本为您提供了鼠标悬停和鼠标移出的功能,这些功能为您提供了与您在本搜索栏页面中看到的相同功能。如果您需要代码回复我我可以给您。

For this type of effect you need java script.Because java script provide you functionality of mouse hover and mouse out these are the functions which provide you the same functionality which u seeing in this page of search bar. If you need code reply me i can give you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文