如何检测Control.PreferredSize何时更改? [英] How can I detect when Control.PreferredSize changes?

查看:87
本文介绍了如何检测Control.PreferredSize何时更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Control.PreferredSize 以确定 ScrollableControl.AutoScrollMinSize 应该用于表单。只要控件的PreferredSize属性发生更改,就需要设置该属性,但似乎没有Control.PreferredSizeChanged事件。有没有一种方法可以检测此属性的更改时间(可能使用 Control.WndProc )?如果可以避免,我宁愿避免轮询属性。

I'm using Control.PreferredSize in order to determine what the ScrollableControl.AutoScrollMinSize should be for a Form. This will need to be set whenever the control's PreferredSize property changes, but there doesn't appear to be a Control.PreferredSizeChanged event. Is there a way to detect when this property changes (possibly using Control.WndProc)? I would prefer to avoid polling the property if it can be avoided.

推荐答案

您可以覆盖OnLayout或OnPaint。

You can override OnLayout or OnPaint.

    private Size m_CurrentPreferedSize;
    protected override void OnLayout(LayoutEventArgs e)
    {
        base.OnLayout(e);
        Size newSize = PreferredSize;
        if(m_CurrentPreferedSize != newSize)
        {
           m_CurrentPreferedSize  = newSize;
           //Your code here 
        }
    }

PreferredSize is每次通话都计算得出。

PreferredSize is calculated on every call.

这篇关于如何检测Control.PreferredSize何时更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆