我正在使用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.Visible
和lblContinue2.Visible
。
为什么不只使用一个标签和诸如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;
}
}