how to type to a label?

2020-03-31 03:51发布

问题:

I am trying to have a multi line textbox that when you type in it streams it to the label, BUT the label has to have a max length of 15 so like once it reaches 15 characters in the textbox it should start overwriting the label since it reached it's max length

thanks to anyone who can help

回答1:

I'm not sure what kind of overwriting You want to achieve.

There are at least three methods you can use:

  • displaying always the last 15 characters from the textbox, as described in the Olivier's answer

  • clearing the label's text each 15 characters inserted and start filling in the label over again, you can use this code to achieve this:

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        String text = textBox1.Text.Replace("\r\n", "|");
    
        int startIndex = ((text.Length - 1) / 15) * 15;
    
        label1.Text = text.Substring(Math.Max(0, startIndex));
    }
    
  • you can also overwrite char by char when text length is over 15 characters, however, I suppose it's not what you would like to achieve, because it would cause a mess in the textbox ;), however, it can be used as a kind of visual effect :). If you want code snippet for this, let me know :). Update Here's the code for the third overwriting method:

    String lastText = "";
    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        String textBoxText = textBox1.Text.Replace("\r\n", "|");
    
        if (textBoxText.Length > lastText.Length)
        {
            int charIndex = (textBoxText.Length - 1) % 15;
    
            if (charIndex >= 0)
            {
                label1.Text = label1.Text.Insert(charIndex, textBoxText.Substring(textBoxText.Length - 1));
                if (charIndex < textBoxText.Length - 1)
                {
                    label1.Text = label1.Text.Remove(charIndex + 1, 1);
                }
            }
        }
        else
        {
            int charIndex = textBoxText.Length % 15;
    
            if (textBoxText.Length >= 15)
            {
                label1.Text = label1.Text.Insert(charIndex, textBoxText[textBoxText.Length - 15].ToString());
                if (charIndex < textBoxText.Length - 1)
                {
                    label1.Text = label1.Text.Remove(charIndex + 1, 1);
                }
            }
            else
            {
                label1.Text = label1.Text.Remove(label1.Text.Length - 1, 1);
            }
        }
    
        lastText = textBoxText;
    }
    


回答2:

Add onchange event on text box :

if (textBox1.Text.Length<=15)  
{  
    label1.Caption=textBox1.Text;  
}

For example



回答3:

Add a handler to the TextChanged event of the TextBox that sets the Label's content based on the text. E.g. (untested, might have your concept wrong or be off by one somewhere)

int startIndex = Math.Max(0, myTextBox.Text.Length - 15);
int endIndex = Math.Min(myTextBox.Text.Length - 1, startIndex);
myLabel.Text = myTextBox.Text.Substring(startIndex, endIndex - startIndex);

Also, though it doesn't change your question/answer, you might want to look at using a TextBlock instead of a Label. It allows things like line wrapping. See some of the differences here: http://joshsmithonwpf.wordpress.com/2007/07/04/differences-between-label-and-textblock/ (in WPF, should be similar whatever you're doing, though)



回答4:

My solution always displays the last 15 characters in the label

private void textBox1_TextChanged(object sender, EventArgs e)
{
    string s = textBox1.Text.Replace("\r\n", "|");
    int length = s.Length;
    if (length > 15) {
        label1.Text = s.Substring(length - 15);
    } else {
        label1.Text = s;
    }
}

I also replace the line-breaks with |. Since your textbox is in multiline mode, line-breaks are entered when you hit <Enter>.