如何使用单个标签,以便多次单击它会显示以下控件?

问题描述 投票:-1回答:1

我正在使用Windows窗体制作一种视觉新颖的游戏,并且具有此功能,其中在窗体的右下角有一个带有文本”单击此消息以继续”的标签。我想找到一种更短的方法,使我只能使用单个标签继续下一个序列(例如在textBox中显示下一个文本)。

我已经尝试过将多个标签彼此叠加,然后,如果已经单击了一个标签,我将使其下一个序列的可见属性为false。

        private void LblContinue_Click(object sender, EventArgs e)
        {
            lbl1.Visible = false;
            lbl2.Visible = true;
            charIndex = 0;
            lbl2.Text = string.Empty;
            Thread t = new Thread(new ThreadStart(this.TypewriteText2));
            t.Start();
            lblContinue.Visible = false;
        }

        private void LblContinue2_Click(object sender, EventArgs e)
        {
            lbl2.Visible = false;
            lbl3.Visible = true;
            charIndex = 0;
            lbl3.Text = string.Empty;
            Thread t = new Thread(new ThreadStart(this.TypeWriteText3));
            t.Start();
            lblContinue2.Visible = false;
        }

尽管此方法有效,但我希望例如,文本框中的文本将显示“ Hello”,然后单击lblContinue后将显示“ World”,然后再次单击后将显示“ Everyone”。别介意其他代码,我想强调lblContinue.VisiblelblContinue2.Visible

c# winforms label
1个回答
0
投票

为什么不只使用一个标签和诸如int之类的其他变量来跟踪状态。每次单击标签时,请检查当前状态并相应地更改文本。类似于以下内容:

    string[] states = { "Hello", "World", "Everyone" };
    int currentState = 0;

    private void stateLabel_Click(object sender, EventArgs e)
    {
        stateLabel.Text = states[currentState];

        //Select next state
        currentState++;
        if (currentState >= states.Length)
        {
            currentState = 0;
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.