将面板高度调整到标签高度

时间:2016-07-07 17:24:12

标签: c# winforms

我有面板,每个面板都有1个标签。一切都很好,除了一件事: 我无法使面板高度符合标签高度......

我正在使用此代码:

$next.click(function() {
      $.get("gallery_.php", function(data) {

})

我试过这样做:

private void loadUsbDevices()
{
    using (var searcher = new ManagementObjectSearcher(
        @"SELECT * FROM Win32_PnPEntity WHERE DeviceID LIKE 'USB%'"))
    {
        ManagementObjectCollection collection = searcher.Get();

        foreach (var device in collection)
        {
            Console.WriteLine(((string) device.GetPropertyValue("Name")) + " ("
                        + ((string) device.GetPropertyValue("ClassGuid")) + ") "
                        + ((string)device.GetPropertyValue("DeviceID")));
        }
    }
}

但是面板太小了......

2 个答案:

答案 0 :(得分:2)

在我看来,您正在使用面板以获得FlowLayoutPanel内标签周围的边距。如果是这种情况,请设置标签的边距而不要使用面板:

lbl.Margin = new Padding(5, 5, 80, 5);

lbl.Margin = new Padding(5); // If all margins are equal

构造函数声明为

public Padding(int left, int top, int right, int bottom)

public Padding(int all)

答案 1 :(得分:0)

您可以尝试在面板中对齐标签,将面板AutoSize设置为true并将AutoSizeMode设置为GrowAndShrink。然后,您可以将面板填充设置为5.这样您就不必担心标签大小或位置

foreach (var item in temp)
{
    Panel pan = new Panel();
    pan.Padding = new Padding(5);
    pan.AutoSize = true;
    pan.AutoSizeMode = AutoSizeMode.GrowAndShrink;
    pan.BackColor = (Color)cc.ConvertFromString("#" + item.Item3);
    Label lbl = new Label();
    lbl.Dock = DockStyle.Fill;
    lbl.Font = new Font("Arial", 12);
    lbl.ForeColor = Color.White;
    lbl.Text = item.Item2;
    lbl.AutoSize = true;
    lbl.MaximumSize = new Size(pan.Width - 5, 0);
    pan.Controls.Add(lbl);
    flowLayoutPanel1.Controls.Add(pan);
    location = new Point(location.X - pan.Height, location.Y);
}

编辑:忘了填充。