【问题标题】:How to move label's text right to left,when a character hides in left it shows on right?如何将标签文本从右向左移动,当一个字符隐藏在左边时它显示在右边?
【发布时间】:2016-07-23 06:29:52
【问题描述】:

这是我的关键代码:

using System;
using System.Windows.Forms;
namespace Scroller
{
    public partial class Form1 : Form
    {
        int i, j;
        bool k = false;
        public Form1()
        {
            InitializeComponent();                                  
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            label1.Text = "Time:"+ System.DateTime.Now.ToString();
            i--;
            j = i + this.Width;
            if (i < this.Width && i > 0)
            {
                label1.Left = i;
            }
            else
            if (i < 0 && k == false)
            {
                label1.Left = i;
                k = true;
            }
            else
            if (i < 0 && k == true)
            {
                label1.Left = j;
                k = false;
            }

            if (i < 0 - label1.Width)
            {
                i = this.Width - label1.Width;
            }
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            label1.Text = "Time:"+  System.DateTime.Now.ToString();
            i = this.Width - label1.Width;
            label1.Left = i;
        }
    }
}

我想要的效果是整个时间字符串从右到左移动。当文本的一个像素在左侧消失时(因为它超出了表单的左边框),该像素将显示在右侧。

也就是说,不能通过删除字符串的第一个字符并附加到最​​后一个字符来达到效果。

我知道使用两个标签会更容易。在表单中设置一个位置,并在表单中隐藏另一个位置。同时移动它们。

当第一个标签触及表单的左边框时,第二个标签触及表单的右边框。第一个移出,第二个移入。直到第二个完全移入,重置它们的 x 位置。

但我只想使用一个标签。所以我选择快速切换标签的位置,并试图“欺骗”用户的眼睛。 问题是label在左右切换的时候闪的很明显。虽然我把定时器的时间间隔设置在20以下,问题还是存在。

你能帮我解决闪退问题或启发我其他方法,只需使用一个标签和一个计时器来制作我需要的效果吗?

谢谢。如果我的问题描述得不够清楚或需要更多代码,请告诉我。

【问题讨论】:

    标签: c# label controls


    【解决方案1】:

    我认为您无法解决在 windows 窗体中更改标签位置的闪烁问题。

    另一种解决方案是将标签宽度设置为与表单宽度相同的大小,使标签文本使用空格填充所有宽度,并使计时器始终获取最后一个字符并将其放在字符串的开头。

    下面的示例代码。

    private void timer1_Tick(object sender, EventArgs e)
    {
        label1.Text = label1.Text.Substring(label1.Text.Length - 1) + label1.Text.Remove(label1.Text.Length - 1);
    }
    
    private void Form1_Load(object sender, EventArgs e)
    {
        // The total spaces required to fill the form vary from form.width and the initial label.text.width
        // Width | Spaces
        //  177  |   13
        //  228  |   30
        //  297  |   53
        //  318  |   60
    
        // The spacesEnd = 60 work for a form with width 319
        int spacesBegin = 0, spacesEnd = 60;
    
        label1.Text = "Time:" + System.DateTime.Now.ToString();
        label1.AutoSize = false;
        label1.Left = -3;
        label1.Width = this.Width - 1;
        label1.Height = 15;
        label1.BorderStyle = BorderStyle.FixedSingle;
    
        for (int i = 0; i < spacesBegin; i++)
            label1.Text = " " + label1.Text;
        for (int i = 0; i < spacesEnd; i++)
            label1.Text += " ";
    
        Timer timer = new Timer();
        timer.Tick += timer1_Tick;
        timer.Interval = 50;
        timer.Start();
    }
    

    【讨论】:

    • 我运行了你的代码。并将标签的宽度设置为319。这里有一些问题:1.当时间文本遇到右侧时,它会卡住几毫秒。 2.因为文本的最小移动是基于每个空格的,一个空格的宽度等于几个像素。这会使文本的移动不是很顺畅。 3.当右侧的xx/ xx:xx:xx 消失时,左侧不会出现相同的内容。有没有更好的解决方案?也许使用填充而不是空间会更好?
    猜你喜欢
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-05
    • 2010-09-12
    • 2017-05-28
    • 1970-01-01
    相关资源
    最近更新 更多