在c#winforms中的控件内水平和垂直地动态添加控件 [英] Align dynamically added controls horizontally and vertically within a control in c# winforms

查看:104
本文介绍了在c#winforms中的控件内水平和垂直地动态添加控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个程序动态添加图片框,指的是数据库中的总裁数。我如何将它们放在组合框中,并将组合框内的picturebox对齐?如果图片框很多,分组框应该拉伸。



我现在有这个代码:

  private void Form1_Load(object sender,EventArgs e)
{
conn.Open();

尝试
{
cmd = new SqlCommand(SELECT COUNT(Position)FROM TableVote WHERE Position ='+President+',conn);
Int32 PresCount =(Int32)cmd.ExecuteScalar();

TxtPresCount.Text = PresCount.ToString();对于(int i = 0; i< PresCount; ++ i)


{
GroupBox PresGB = new GroupBox();
{
PresGB.Size = new Size(491,152);
PresGB.Location = new Point(12,12);
PresGB.Text =总统;
this.Controls.Add(PresGB);
PresGB.SendToBack();

PictureBox PresPB = new PictureBox();
PresPB.Location = new Point(80 +(150 * i)+ 20,50);
PresPB.Size = new Size(75,75);
PresPB.BorderStyle = BorderStyle.Fixed3D;
PresPB.ImageLocation =imgPath;
this.Controls.Add(PresPB);
PresPB.BringToFront();
};
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
}

我想要的图片框位于组框内,

解决方案

也许 FlowLayoutPanel 控件已经做了你想要做的事情。只需创建您的图片框,并将它们添加到 FlowLayoutPanel 而不是 GroupBox



FlowLayoutPanel 根据其 FlowDirection 属性。设置 myFlowLayoutPanel.FlowDirection = FlowDirection.TopDown 以获得垂直排列的列表。



如果不想要多个行或列设置 WrapContents 属性为false。您还可以设置 AutoScroll 属性为true,如果控件不适合自动获取滚动条。



如果您希望拥有<$ c的边框$ c> GroupBox 您仍然可以将 FlowLayoutPanel 放入 GroupBox



要调整图片框之间的空间,您可以使用 保证金 属性。



这给了你很多控制布局,您不需要计算控制位置。另外,如果 FlowLayoutPanel 更改的大小自动重新排列。



更新:



我对您的代码有几个意见:


  1. 大括号使得它看起来像一个对象初始化器的语法 - 但它不是。

      GroupBox PresGB = new GroupBox ); //这行以分号
    {
    //结束,因此这只是一个与新的GroupBox()无关的代码块
    };

    您应该删除花括号。


  2. 组框的创建在循环内。我怀疑你想为每个图片框添加一个新的分组框。这就是为什么你只看到一张照片的原因。


  3. 您可以将图片框添加到表单而不是组框。


  4. 你使用隐藏的名字。 PresGB PresPB 很有可能被意外交换。缩写通常是名称不好的选择。


  5. 您不需要调用 SendToBack BringToFront ,因为您不希望控件重叠。


  6. 我不认为 GroupBox 是一个不错的选择。当然,如果图片数量增加但是屏幕受到限制,您可以使其变大,如果图片框不合适,您就不会收到。棒声。使用 FlowLayoutPanel 。它具有您正在寻找的所有魔法。


将此代码替换为for循环:

  var panel = new FlowLayoutPanel(); 
panel.SuspendLayout(); //在添加所有图片框之前不要计算布局
panel.Size = new Size(491,152);
panel.Location = new Point(12,12);
panel.BorderStyle = BorderStyle.Fixed3D;
panel.FlowDirection = FlowDirection.LeftToRight;
panel.AutoScroll = true; //如果需要,自动添加滚动条
panel.WrapContents = false; //单列中的所有图片框
this.Controls.Add(panel);

for(int i = 0; i< PresCount; ++ i)
{
var pictureBox = new PictureBox();
//位置由FlowLayoutPanel
计算pictureBox.Size = new Size(75,75);
pictureBox.BorderStyle = BorderStyle.FixedSingle;
pictureBox.ImageLocation =imgPath;
panel.Controls.Add(pictureBox);
}

panel.ResumeLayout();


I have this program that dynamically adds pictureboxes referring to the number of president in the database. How do i put them inside the groupbox and align the pictureboxes inside the groupbox? And the groupbox should stretch if the pictureboxes are many.

I have this codes now :

    private void Form1_Load(object sender, EventArgs e)
    {
        conn.Open();

        try
        {
            cmd = new SqlCommand("SELECT COUNT(Position) FROM TableVote WHERE Position='" + "President" + "'", conn);
            Int32 PresCount = (Int32)cmd.ExecuteScalar();

            TxtPresCount.Text = PresCount.ToString();

            for (int i = 0; i < PresCount; ++i)
            {
                GroupBox PresGB = new GroupBox();
                {
                    PresGB.Size = new Size(491, 152);
                    PresGB.Location = new Point(12, 12);
                    PresGB.Text = "President";
                    this.Controls.Add(PresGB);
                    PresGB.SendToBack();

                    PictureBox PresPB = new PictureBox();
                    PresPB.Location = new Point(80 + (150 * i) + 20, 50);
                    PresPB.Size = new Size(75, 75);
                    PresPB.BorderStyle = BorderStyle.Fixed3D;
                    PresPB.ImageLocation = "imgPath";
                    this.Controls.Add(PresPB);
                    PresPB.BringToFront();
                };
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            conn.Close();
        }
    }

I would want the pictureboxes to be inside the groupbox and align it inside.

解决方案

Maybe the FlowLayoutPanel control already does what you are trying to do. Just create your picture boxes and add them to a FlowLayoutPanel instead of a GroupBox.

FlowLayoutPanel automatically arranges controls in rows and/or columns depending on the value of its FlowDirection property. Set myFlowLayoutPanel.FlowDirection = FlowDirection.TopDown to get a vertical arranged list.

If you don't want multiple rows or columns set the WrapContents property to false. You can also set the AutoScroll property to true to automatically get scrollbars if the controls don't fit.

If you prefer to have the border of a GroupBox you can still put the FlowLayoutPanel into a GroupBox.

To adjust the space between the picture boxes you can use the Margin property.

This gives you a lot of control over the layout and you don't need to calculate the control positions. Also, if the size of the FlowLayoutPanel changes everything is rearranged automatically.

UPDATE:

I have a few comments on your code:

  1. The curly braces make this look like the syntax of an object initializer - but it isn't.

    GroupBox PresGB = new GroupBox(); // this line ends with a semicolon
    {
        // therefore this is just a block of code not related to new GroupBox()
    };
    

    You should remove the curly braces.

  2. The creation of the group box is inside the loop. I doubt that you want a new group box for each picture box. This is the reason why you only see a single picture. Each new group box hides all the previous ones.

  3. You add the picture boxes to the form instead of the group box.

  4. You use "cryptic" names. PresGB and PresPB are very likely to be swapped accidentally. Abbreviations are usually a bad choice for names.

  5. You don't need to call SendToBack or BringToFront since you don't want the controls to overlap anyway.

  6. I don't think GroupBox is a good choice. Of course you can make it bigger if the number of pictures increases but you are limited by the screen and you don't get scollbars if the picture boxes don't fit. Use a FlowLayoutPanel. It has all the "magic" that you are looking for.

Replace your for loop with this piece of code:

var panel = new FlowLayoutPanel();
panel.SuspendLayout(); // don't calculate the layout before all picture boxes are added
panel.Size = new Size(491, 152);
panel.Location = new Point(12, 12);
panel.BorderStyle = BorderStyle.Fixed3D;
panel.FlowDirection = FlowDirection.LeftToRight;
panel.AutoScroll = true; // automatically add scrollbars if needed
panel.WrapContents = false; // all picture boxes in a single row
this.Controls.Add(panel);

for (int i = 0; i < PresCount; ++i)
{
    var pictureBox = new PictureBox();
    // the location is calculated by the FlowLayoutPanel
    pictureBox.Size = new Size(75, 75);
    pictureBox.BorderStyle = BorderStyle.FixedSingle;
    pictureBox.ImageLocation = "imgPath";
    panel.Controls.Add(pictureBox);
}

panel.ResumeLayout(); 

这篇关于在c#winforms中的控件内水平和垂直地动态添加控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆