简体   繁体   English

我应该在哪里将代码放在C#中的窗体上以调整大小/重新放置控件?

[英]Where should I put the code to resize/reposition controls on a form in C#?

I'm using Windows Forms and I'd like to write some code to change the layout of each of the controls on the form whenever anything gets scrolled or resized. 我正在使用Windows窗体,并且我想编写一些代码来在滚动或调整大小时更改窗体上每个控件的布局。 I assume there must be a standard way of doing this before a form paint is done. 我认为在完成模板绘制之前必须有一种标准的方法。

EDIT: There is a DataGridView on the form. 编辑:在窗体上有一个DataGridView。 I want to change the layout whenever a column width is changed or the horizontal scroll bar is moved. 每当更改列宽或移动水平滚动条时,我都想更改布局。

Override those two methods in your form: 覆盖表单中的这两种方法:

protected override void OnResize(EventArgs e)
{
    base.OnResize(e);
}

protected override void OnScroll(ScrollEventArgs se)
{
    base.OnScroll(se);
}

You don't need to create any positioning and resizing code if you place your objects inside a TableLayoutPanel . 如果将对象放置在TableLayoutPanel则无需创建任何定位和调整大小的代码。 This control acts pretty much as HTML table, well not quite exactly so. 这个控件几乎就像HTML表格一样,完全不一样。

Take a look at the following link how to use TableLayoutPanel : 看一下下面的链接如何使用TableLayoutPanel

TableLayoutPanel Class (System.Windows.Forms) TableLayoutPanel类(System.Windows.Forms)

whenever anything gets scrolled or resized 一旦有事情获取滚动或调整

Please be precise. 请精确。


What do you expect to change size? 您希望改变什么尺寸?
Where does the scrolling occur? 滚动发生在哪里? (in the form, in list box or other) (以表格,列表框或其他形式)

If you want to change layout in form resize, you can do it in Form.Resize event handler. 如果要在表单调整大小中更改布局,可以在Form.Resize事件处理程序中进行。

For scrolling in form, use ScrollEventArgs 要滚动表单,请使用ScrollEventArgs

Take a look at these questions as well. 看看这些问题。

Scrolling 卷动

Form Resize event - MSDN 表单调整大小事件-MSDN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM