“调整大小结束”相当于用户控件

发布于 2024-08-28 05:30:22 字数 200 浏览 2 评论 0原文

我正在写一个用户控件。我想在调整大小完成后绘制用户控件。我无法找到任何相当于 Windows 窗体的“ResizeEnd”的事件。

用户控件是否有等效的事件?

请注意,在这种情况下,用户控件的父控件本身就是一个 UserControl,因此我无法将其(父用户控件)转换为表单。由于我正在使用框架,因此我无法访问将显示此用户控件的表单。

I am writing a UserControl. I want to draw the user control when the resize is done. I am not able to find any event equivalent to "ResizeEnd" of a windows form.

Is there any equivalent event for user controls?

Please note that in this case the parent control of the user control is itself an UserControl, so I cannot convert it (parent user control) into a form. As I am using a framework, I cannot access the form on which this user control will be displayed.

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

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

发布评论

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

评论(2

黯然 2024-09-04 05:30:22

没有同等的。表单具有模态大小调整循环,当用户单击表单的边缘或角落时启动。子控件无法以这种方式调整大小,它只能看到其 Size 属性的更改。

通过向用户控件添加 Sizing 属性来解决此问题。表单可以轻松地从其 OnResizeBegin/End() 覆盖中分配它。跟踪 UC 的 Load 事件中的 Parent 属性,直到找到 Form 也是可能的:

public bool Resizing { get; set; }

private void UserControl1_Load(object sender, EventArgs e) {
  if (!this.DesignMode) {
    var parent = this.Parent;
    while (!(parent is Form)) parent = parent.Parent;
    var form = parent as Form;
    form.ResizeBegin += (s, ea) => this.Resizing = true;
    form.ResizeEnd += (s, ea) => this.Resizing = false;
  }
}

There is no equivalent. A form has a modal sizing loop, started when the user clicks the edge or a corner of the form. Child controls cannot be resized that way, it only sees changes to its Size property.

Solve this by adding a Sizing property to your user control. The form can easily assign it from its OnResizeBegin/End() overrides. Following the Parent property in the UC's Load event until you find the Form is possible too:

public bool Resizing { get; set; }

private void UserControl1_Load(object sender, EventArgs e) {
  if (!this.DesignMode) {
    var parent = this.Parent;
    while (!(parent is Form)) parent = parent.Parent;
    var form = parent as Form;
    form.ResizeBegin += (s, ea) => this.Resizing = true;
    form.ResizeEnd += (s, ea) => this.Resizing = false;
  }
}
陌路黄昏 2024-09-04 05:30:22

Hans 解决方案正在工作(看起来这是唯一的方法),但它需要每种形式的这些处理程序,这些处理程序使用您的控件(这并不总是可以接受的)。

因此,您可以使用简单的解决方法,在调整大小时启动计时器。
每次更改大小时,您的计时器都会重新启动。并且只有当一段时间(_timer.Interval)没有大小变化时,它才会调用 ResizeFinished() 方法。

    private Timer _timer;

    public MyControl() 
    {
        _timer = new Timer();
        _timer.Interval = 500;
        _timer.Tick += (sender, e) => ResizeFinished();
    }

    protected override void OnSizeChanged(EventArgs e)
    {
        base.OnSizeChanged(e);
        _timer.Start();
    }

    private void ResizeFinished()
    {
        _timer.Stop();
        // Your code
    }

如您所见,您的代码仅在上次大小更改调用后 500 毫秒后才会被调用。

The Hans solution is working (and it looks like it is the only way to do it), but it's requires these handlers in every form, which using your control (which is not always acceptable).

So, you can use simple workaround, starting timer when resizing occurs.
Every time size will be changed your timer will be restarted. And only when there will be no size changes for some time (_timer.Interval) it will invoke ResizeFinished() method.

    private Timer _timer;

    public MyControl() 
    {
        _timer = new Timer();
        _timer.Interval = 500;
        _timer.Tick += (sender, e) => ResizeFinished();
    }

    protected override void OnSizeChanged(EventArgs e)
    {
        base.OnSizeChanged(e);
        _timer.Start();
    }

    private void ResizeFinished()
    {
        _timer.Stop();
        // Your code
    }

As you can see, your code will be invoked only after 500 ms after last size changed invocation.

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