当计时器为 运行 C# 时无法单击按钮
Button cannot be clicked when timer is running C#
我已经申请了一个带退出按钮的选取框文本。该按钮在禁用定时器时工作正常,但一旦启用定时器就无法单击。
谁能帮帮我吗?我尝试在网上进行研究,但似乎没有答案,谢谢! :)
这是我的代码
public FORM_ENTRY()
{
InitializeComponent();
LabelWelcome.Text = "Welcome! Welkom! Selamat Datang! Wilkommen!";
timer1.Enabled = true;
LabelCheckedIn.Visible = false;
LabelEnjoy.Visible = false;
}
private void StartScroll()
{
System.Text.StringBuilder sb = new System.Text.StringBuilder(LabelWelcome.Text + " ");
while (true)
{
char ch = sb[sb.Length - 1];
sb.Remove(sb.Length - 1, 1);
sb.Insert(0, ch);
LabelWelcome.Text = sb.ToString();
LabelWelcome.Refresh();
System.Threading.Thread.Sleep(500);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
StartScroll();
}
private void ButtonExit_Click(object sender, EventArgs e)
{
this.Close();
}
使用DispatchTimer,它将运行在一个单独的线程上,事件将在UI线程上触发,因此您将能够修改您的UI
private DispatcherTimer dispatcherTimer;
public FORM_ENTRY()
{
InitializeComponent();
LabelWelcome.Text = "Welcome! Welkom! Selamat Datang! Wilkommen!";
LabelCheckedIn.Visible = false;
LabelEnjoy.Visible = false;
// Initialize Dispatcher
dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
// Five seconds delay
dispatcherTimer.Interval = new TimeSpan(0, 0, 5);
dispatcherTimer.Start();
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder(LabelWelcome.Text);
char ch = sb[sb.Length - 1];
sb.Remove(sb.Length - 1, 1);
sb.Insert(0, ch);
LabelWelcome.Text = sb.ToString();
}
private void ButtonExit_Click(object sender, EventArgs e)
{
this.Close();
}
我已经申请了一个带退出按钮的选取框文本。该按钮在禁用定时器时工作正常,但一旦启用定时器就无法单击。 谁能帮帮我吗?我尝试在网上进行研究,但似乎没有答案,谢谢! :)
这是我的代码
public FORM_ENTRY()
{
InitializeComponent();
LabelWelcome.Text = "Welcome! Welkom! Selamat Datang! Wilkommen!";
timer1.Enabled = true;
LabelCheckedIn.Visible = false;
LabelEnjoy.Visible = false;
}
private void StartScroll()
{
System.Text.StringBuilder sb = new System.Text.StringBuilder(LabelWelcome.Text + " ");
while (true)
{
char ch = sb[sb.Length - 1];
sb.Remove(sb.Length - 1, 1);
sb.Insert(0, ch);
LabelWelcome.Text = sb.ToString();
LabelWelcome.Refresh();
System.Threading.Thread.Sleep(500);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
StartScroll();
}
private void ButtonExit_Click(object sender, EventArgs e)
{
this.Close();
}
使用DispatchTimer,它将运行在一个单独的线程上,事件将在UI线程上触发,因此您将能够修改您的UI
private DispatcherTimer dispatcherTimer;
public FORM_ENTRY()
{
InitializeComponent();
LabelWelcome.Text = "Welcome! Welkom! Selamat Datang! Wilkommen!";
LabelCheckedIn.Visible = false;
LabelEnjoy.Visible = false;
// Initialize Dispatcher
dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
// Five seconds delay
dispatcherTimer.Interval = new TimeSpan(0, 0, 5);
dispatcherTimer.Start();
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder(LabelWelcome.Text);
char ch = sb[sb.Length - 1];
sb.Remove(sb.Length - 1, 1);
sb.Insert(0, ch);
LabelWelcome.Text = sb.ToString();
}
private void ButtonExit_Click(object sender, EventArgs e)
{
this.Close();
}