110,571
社区成员
发帖
与我相关
我的任务
分享
Public Class Form1
Private groupView As Panel
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
groupView = New Panel With {.Dock = DockStyle.Fill, .BorderStyle = BorderStyle.FixedSingle, .AutoScroll = True}
AddHandler groupView.Resize, AddressOf ResizePanel
For i As Integer = 0 To 20
Dim btn As New Button With {.Text = "Button" & i.ToString}
groupView.Controls.Add(btn)
Next
Me.Controls.Add(groupView)
End Sub
Private Sub ResizePanel(ByVal sender As Object, ByVal e As EventArgs)
groupView.AutoScroll = False
Dim heightNext As Integer = 3
For Each Control In groupView.Controls
Dim startLeft As Integer = 3
Dim deltaWidth As Integer = (groupView.Width - Control.Width) / 2
If deltaWidth > 0 Then
startLeft = deltaWidth
End If
Control.Left = startLeft
Control.Top = heightNext
heightNext = Control.Bottom + 20
Next
groupView.AutoScroll = True
End Sub
End Class
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private Panel groupView;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
groupView = new Panel { Dock = DockStyle.Fill, BorderStyle = BorderStyle.FixedSingle, AutoScroll = true };
groupView.Resize += ResizePanel;
for (int i = 0; i <= 20; i++)
{
Button btn = new Button { Text = "Button" + i.ToString() };
groupView.Controls.Add(btn);
}
this.Controls.Add(groupView);
}
private void ResizePanel(object sender, EventArgs e)
{
groupView.AutoScroll = false;
int heightNext = 3;
foreach (Control control in groupView.Controls)
{
int startLeft = 3;
int deltaWidth = (groupView.Width - control.Width) / 2;
if (deltaWidth > 0)
{
startLeft = deltaWidth;
}
control.Left = startLeft;
control.Top = heightNext;
heightNext = control.Bottom + 20;
}
groupView.AutoScroll = true;
}
}
}