问题描述:
- 创建“自定义控件”。将其属性 AutoScroll 设置为“true”。将其背景颜色更改为绿色。
- 创建第二个“自定义控件”。将其背景颜色更改为红色。
- 在主窗体上放置第一个自定义控件
- 在代码中创建第二个控件的 20 个实例
- Add a button and in the button:
- 在代码中设置它们在循环中的位置,如 c.Location = new Point(0, y);
- y += c.高度;
- Run App.
- 按下按钮
- 滚动容器
- 再次按下按钮,有人可以解释一下吗为什么0不是容器形式的开头?!控制权已转移...
在回答之前:
1)是的,事情需要这样
2)代码示例如下:
public partial class Form1 : Form
{
List<UserControl2> list;
public Form1()
{
InitializeComponent();
list = new List<UserControl2>();
for (int i = 0; i < 20; i++)
{
UserControl2 c = new UserControl2();
list.Add(c);
}
}
private void Form1_Load(object sender, EventArgs e)
{
foreach (UserControl2 c in list)
userControl11.Controls.Add(c);
}
private void button1_Click(object sender, EventArgs e)
{
int y = 0;
foreach (UserControl2 c in list)
{
c.Location = new Point(0, y);
y += c.Height;
}
}
}
这是因为Location http://msdn.microsoft.com/en-us/library/system.windows.forms.control.location.aspx给出控件左上角相对于其容器左上角的坐标.因此,当您向下滚动时,位置将会改变。
以下是修复方法:
private void button1_Click(object sender, EventArgs e)
{
int y = list[0].Location.Y;
foreach (UserControl2 c in list)
{
c.Location = new Point(0, y);
y += c.Height;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)