显示标签的文本框输入?

时间:2014-07-18 01:13:36

标签: c# winforms

我遇到了将按钮显示在文本框中的输入到标签的问题。这就是它的样子。

enter image description here

现在忘记单选按钮和复选框。我希望用户在文本框内输入的内容与占位符“Name:”一起显示在按钮右侧的标签上。

//                  Name TextBox
    //***********************************************************
    //Enter your name textbox
    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }
    //Empties the textbox once it's focused
    private void textBox1_Enter(object sender, EventArgs e)
    {   
        if(textBox1.Focus())
            textBox1.Text = String.Empty;
    }
    //Resets the placeholder text for password textbox
    private void textBox1_Leave(object sender, EventArgs e)
    {
        if(!textBox1.Focused)
            textBox1.Text = "Name: ";
    }
    //***********************************************************

    //                  Password TextBox
    //***********************************************************
    //Enter your password textbox
    private void textBox2_TextChanged(object sender, EventArgs e)
    {

    }
    //Empties the password  textbox once it's focused
    private void textBox2_Enter(object sender, EventArgs e)
    {
        if(textBox2.Focus())
            textBox2.Text = String.Empty;
    }
    //Resets the placeholder text for password textbox
    private void textBox2_Leave(object sender, EventArgs e)
    {
        if(!textBox2.Focused)
            textBox2.Text = "Password: ";
    }
    //***********************************************************


    //Display Name button
    private void button1_Click(object sender, EventArgs e)
    {
        label5.Text = textBox1.Text;
    }
    //Display password button
    private void button2_Click(object sender, EventArgs e)
    {
        label6.Text = textBox2.Text;
    }

1)只是为了澄清:我已经在文本框中将初始文本作为占位符。一旦失去焦点,占位符文本将重新出现在文本框内。我已经使用了focus()方法和聚焦属性,因为我根本不知道其中的区别。 (我不知道是否应该在另一个问题中询问两者之间的区别,所以请让我知道)

2)现在,当我在文本框中输入whateva,然后按下显示按钮,再次出现默认的占位符文本,输入不会出现在标签中。当然,我也不希望占位符文本出现在标签上。

显然我是Windows Forms的新手,更糟糕的是,在编写WinForms应用程序时,我发现很难清楚地表达我的问题。因此,如果我的问题中缺少任何代码,请告诉我。

2 个答案:

答案 0 :(得分:2)

您只需要更改支票,如果用户进行了任何更改,那么您将不会重新显示占位符:

//Resets the placeholder text for password textbox
    private void textBox2_Leave(object sender, EventArgs e)
    {
        if(!textBox2.Focused && textBox2.Text.Trim() == String.Empty)
            textBox2.Text = "Password: ";
    }

//Resets the placeholder text for password textbox
    private void textBox1_Leave(object sender, EventArgs e)
    {
        if(!textBox1.Focused && textBox1.Text.Trim() == String.Empty)
            textBox1.Text = "Name: ";
    }

TextBox.Focus()是一种使文本框成为表单活动控件的方法。它还将 TextBox.Focused 属性设置为true。

答案 1 :(得分:1)

这看起来像是一个家庭作业问题,所以我不会给你你答案,但我会帮你提出一些建议。

  1. 您需要停止使用控件名称进行延迟。我们都这样做。但它必须停止。从现在开始的六个月,你将不记得label5是什么。
    • 我建议您开始重命名表单上的控件。例如,将Name文本框更改为nameTextBox,并将Name按钮更改为nameButton,并将Name标签更改为nameLabel。
  2. 断点。使用它们。如果某些内容无法按预期运行,请在您希望发生某些事情的行上设置断点。例如,如果你写:
    • nameLabel.Text = nameTextBox.Text;然后你应该在那一行设置一个断点,调试你的应用程序并逐步完成,看看输出窗口看不正确的东西。
相关问题