gpt4 book ai didi

c# - 表单无法通过计时器事件顺利调整大小

转载 作者:行者123 更新时间:2023-12-02 22:39:46 24 4
gpt4 key购买 nike

我的表单底部有一个网格控件,如果用户想要显示/隐藏它,它可以显示或隐藏。所以一种方法是很好地使用窗体的 AutoSize 并将该网格的 Visuble 属性更改为 true 或 false,......但我想让我们让它更酷一点!所以我想让表格调整得更慢一点,就像车库门一样!所以我在窗体上放置了一个计时器,并在计时器滴答作响的同时开始一点一点地增加窗体的高度...

所以当用户说显示/隐藏网格时是这样的:

    timer1.Enabled = true;
timer1.Start();

在 timer_click 事件上类似这样的事情:

    this.Height = this.Height + 5;
if(this.Height -10 > ErrorsGrid.Bottom )
timer1.Stop();

它有点管用,但仍不完美。例如,它在一开始就滞后,像一秒钟一样停止调整大小,然后又开始调整大小...所以现在考虑到这个想法,你建议我应该做哪些改变才能使这个东西看起来更好,工作得更好?

最佳答案

尝试使用 System.Timers.Timer 代替。您可以阅读有关可用 .net 计时器之间差异的更多信息 here ,但我认为您的问题归结为:

"[System.Windows.Forms.Timer] events raised by this timer class are synchronous with respect to the rest of the code in your Windows Forms app. This means that application code that is executing will never be preempted by an instance of this timer class..."

这对 System.Timers.Timer 来说不是问题。只需确保将该对象的 SynchronizingObject 设置为您的表单,以便 elapsed 事件在 UI 线程上执行。

例子:

public partial class Form1 : Form
{
System.Timers.Timer timer = new System.Timers.Timer(100);

public Form1()
{
InitializeComponent();

timer.AutoReset = true;
timer.SynchronizingObject = this;
timer.Elapsed += timer_Elapsed;
}

void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
this.Height += 5;

if (this.Height -10 > ErrorsGrid.Bottom)
timer.Stop();
}

void button1_Click(object sender, EventArgs e)
{
timer.Start();
}
}

关于c# - 表单无法通过计时器事件顺利调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10905970/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com