0

在 Visual Basic 中,我做了一个标签来显示文本框的长度,现在如何让它显示你在文本框中还剩下多少个字符?我的意思是在 Twitter 上,你如何限制这么多字符。我也想要它,这样当它达到 10 或更低时,标签变成红色,然后超过 10,标签变成黑色。如果问题不应该出现在这个论坛中,请原谅我,我只是不知道如何做到这一点。

4

2 回答 2

1

在 Windows 窗体中,您可以编写

 label1.Text = (textBox1.MaxLength - textBox1.Text.Length).ToString();
于 2012-09-03T15:05:05.150 回答
1

The above answer has an error because it is trying to convert a decimal to a string, so you need to wrap that in brackets and call .ToString() as for your colours idea I used percentages. If you have used more than 50% and less than 75% of your character allowance, then make the text orange. If you have used more than 75% then make it red.

  lblRemaining.Text = string.Format("{0} characters remaining", (textBox1.MaxLength - textBox1.TextLength).ToString());
  decimal percentageUsed = ((decimal)textBox1.Text.Length / (decimal)textBox1.MaxLength) * 100;
  if (percentageUsed >= 50 && percentageUsed < 75)
    lblRemaining.ForeColor = Color.Orange;
  else if (percentageUsed >= 75)
    lblRemaining.ForeColor = Color.Red;
  else
    lblRemaining.ForeColor = Color.Green;
于 2012-09-03T15:30:09.287 回答