如何键入一个标签? [英] how to type to a label?

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

问题描述

我想有一个多行文本框,当你键入它,它流的标签,但标签必须有15最大长度等等之类,一旦它在文本框中达到15个字符,它应该开始覆盖标签,因为它达到了它的最大长度

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

由于任何人谁可以帮助

推荐答案

我不知道你想达到什么样的覆盖

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

有可以使用至少三种方法:

There are at least three methods you can use:


  • 从文本框中显示总是最后15个字符,如描述的的 Olivier的答案

结算标签的文本各15个字符插入并开始在标签重新灌装,您可以使用此代码来实现这一点:

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


  • 您也可以在覆盖字符由字符时,文本长度超过15个字符,但是,我想它的不可以您想实现的,因为它会在文本框中造成混乱的东西;),但是,它可以被用来作为一种的视觉效果:)。如果你想为这个代码片段,让我知道:)。
    更新
    下面是第三重写方法的代码:

  • 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;
    }
    


  • 这篇关于如何键入一个标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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