如何在不实际调整大小的情况下触发 Control.Resize 事件?
我不会对控件进行子类化。 尝试通过 Control.Size = Control.Size 触发事件会失败,因为即使新大小实际上不同,它也不会触发。
I'm not subclassing the control. Trying to trigger the event via Control.Size = Control.Size
fails, since it does not trigger then even unless the new size is actually different.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您要子类化
Control
,则可以直接调用OnResize
,或在 API 上公开它:但是,您不能对任意控件执行此操作。 您可以来回更改
Size
吗? 或者,您可以使用反射,但这很糟糕:If you are subclassing
Control
, you can callOnResize
directly, or expose it on the API:However, you can't do this for arbitrary controls. You could change the
Size
to-and-fro? Alternatively, you could use reflection, but that is hacky:我总是通过调用控件的调整大小事件处理程序来执行此操作:
I always do this by calling the Control's Resize event handler:
只需使用以下命令更改控件的大小: Control.Size = new Size(x,y);
更改控件的大小将发出该控件的调整大小事件,并且该控件应该调整大小。
或者,如果您只想重绘控件,则执行以下操作:
Control.Invalidate();
Just change the size of the control using: Control.Size = new Size(x,y);
Changing the size of the control will issue a resize event for that control and the control should resize.
Alternatively if you just want to redraw the control then do:
Control.Invalidate();
为什么要这样做,在什么场景下? 例如,当您在控件本身中(即在派生控件类中)时,您可以调用 OnResize。 (或者当您在外面时通过反射。)
除此之外,您可能还需要更改控件的大小,因为这就是 Resize 事件的用途:)
Why do you want to do this, and in what scenario? You can call OnResize, for example, when you're in the control itself (ie. in your derived control class). (Or via Reflection, when you are outside.)
Apart from that, you'll probably have to change the control's size, since that is what the Resize event is for :)