如何让文本框的文字来回移动?

发布于 2025-01-05 09:17:02 字数 1094 浏览 0 评论 0原文

你好,atm 我有这个代码

    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (checkBox1.Checked == true)
        {
            x = 0;
            timer1.Enabled = true;
            timer1.Start();
        }
        else
        {
            timer1.Enabled = false;
        }
    }
    private int x = 0;
    private int y = 0;
    private void timer1_Tick(object sender, EventArgs e)
    {
        if (x <= 10)
        {
            x++;
            string ping = new string(' ', x) + "hello";
            label1.Text = ping;
            if (x == 10)
            {
                y = 10;
            }
        }
        else if (y > 0)
        {
            y--;
            string pong = new string(' ', y) + "hello";
            label1.Text = pong;
            if (y == 0)
            {
                x = 0;
            }
        }
    }

,目前标签的最大长度为 15 个字符,我希望它保持这种状态。

但我希望它而不是使用“hello”将我输入到文本框中的文本并执行此操作。

然而,它必须采用 15 并减去文本框文本的长度,以便保持标签最大长度 15 完整,同时在文本框中显示整个单词,但我不知道该怎么做,我已经尝试了很多方法,但是我不明白任何帮助将不胜感激。 :D

hello atm I have this code

    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (checkBox1.Checked == true)
        {
            x = 0;
            timer1.Enabled = true;
            timer1.Start();
        }
        else
        {
            timer1.Enabled = false;
        }
    }
    private int x = 0;
    private int y = 0;
    private void timer1_Tick(object sender, EventArgs e)
    {
        if (x <= 10)
        {
            x++;
            string ping = new string(' ', x) + "hello";
            label1.Text = ping;
            if (x == 10)
            {
                y = 10;
            }
        }
        else if (y > 0)
        {
            y--;
            string pong = new string(' ', y) + "hello";
            label1.Text = pong;
            if (y == 0)
            {
                x = 0;
            }
        }
    }

at the moment the label has a maximum length of 15 characters and i want it to stay that way.

but i want it to instead of using "hello" to take the text i input into a textbox and do it.

however it has to take 15 and subtract the length of the textboxes text in order to keep the labels max length of 15 intact while displaying the entire word in the textbox aswell but i cant figure out how to do it i have tried plenty of things but i cannot figure it out any help would be greatly appreciated. :D

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

总攻大人 2025-01-12 09:17:02

您使用的“ping”和“pong”一词,加上您的标题“来回移动”,使我相信您可以通过在每次勾选时更改标签的 TextAlign 属性来实现您想要的结果。

如果这是您想要的结果,则根本不必添加空格。文本将在标签中显示为从左边缘到右边缘。您可以考虑使用 TRIM() 修剪文本属性,以确保两侧不存在空格,否则会导致其显示对齐不正确。

Your use of the words "ping" and "pong", plus your title saying "move back and forth" leads me to believe the result you want can be achieved by changing the TextAlign property of the label upon each tick.

If this is the result you want, you won't have to add spaces at all. The text will appear to go from left to right edges in the label. You might consider trimming the text property with TRIM() to ensure no spaces exist on either side that would make it appear aligned incorrectly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文