111,077
社区成员




public Form1()
{
InitializeComponent();
// 外框容器
Panel panel_box = new Panel()
{
Size = new Size(200, 400),
Location = new Point(100, 10),
};
this.Controls.Add(panel_box);
// 文字容器
Panel panel_content = new Panel() { BackColor = Color.SteelBlue, };
panel_box.Controls.Add(panel_content);
// 添加一堆文字
for (int i = 0; i < 111; i++)
{
Label panel_text = new Label()
{
AutoSize = false,
Size = new Size(200, 22),
Text = i + ".",
Location = new Point(0, i * 20),
ForeColor = Color.White
};
panel_content.Controls.Add(panel_text);
}
panel_content.AutoSize = true;
// 建立滚动条
VScrollBar scrollBar = new VScrollBar()
{
Size = new Size(20, 420),
Location = new Point(300, 10),
};
this.Controls.Add(scrollBar);
// 滚动动作
scrollBar.Scroll += (s, e) =>
{
double y = ((double)panel_content.Height - panel_box.Height) / 92; // 多高都是92是固定值
int offset = (int)(-scrollBar.Value * Math.Ceiling(y));
panel_content.Top = offset < -panel_content.Height + panel_box.Height ? -panel_content.Height + panel_box.Height : offset;
};
}
}