C# 盒子要在里面对齐。例如,如果我的预计数等于4,那么它将在groupbox中动态创建PictureBox。它所做的只是居中/填充分组框。其他的图片框看不见。你能给出一个参考我上面代码的示例代码吗?因为它太复杂了。我不明白为什么我的pictureboxes只停

C# 盒子要在里面对齐。例如,如果我的预计数等于4,那么它将在groupbox中动态创建PictureBox。它所做的只是居中/填充分组框。其他的图片框看不见。你能给出一个参考我上面代码的示例代码吗?因为它太复杂了。我不明白为什么我的pictureboxes只停,c#,winforms,dynamic,C#,Winforms,Dynamic,盒子要在里面对齐。例如,如果我的预计数等于4,那么它将在groupbox中动态创建PictureBox。它所做的只是居中/填充分组框。其他的图片框看不见。你能给出一个参考我上面代码的示例代码吗?因为它太复杂了。我不明白为什么我的pictureboxes只停留在一个位置。@LyndonBrozTonelete:我添加了一些代码,还对您的代码添加了一些注释。我想知道是否可以在我的每个PictureBox下面放置dynamic radiobutton。。也有可能吗?@LyndonBrozTonelet


盒子要在里面对齐。例如,如果我的预计数等于4,那么它将在groupbox中动态创建PictureBox。它所做的只是居中/填充分组框。其他的图片框看不见。你能给出一个参考我上面代码的示例代码吗?因为它太复杂了。我不明白为什么我的pictureboxes只停留在一个位置。@LyndonBrozTonelete:我添加了一些代码,还对您的代码添加了一些注释。我想知道是否可以在我的每个PictureBox下面放置dynamic radiobutton。。也有可能吗?@LyndonBrozTonelete:RadioButton控件有一个
Image
属性,您可以使用它来代替图片框。
CheckAlign
ImageAlign
属性使您可以控制布局。
    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();
        }
    }
this.PresPB.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
        | System.Windows.Forms.AnchorStyles.Left) 
        | System.Windows.Forms.AnchorStyles.Right)));
this.PresPB.Dock = System.Windows.Forms.DockStyle.Fill;
this.Controls.Add(PresPB);
PresGB.Controls.Add(PresPB);
GroupBox PresGB = new GroupBox(); // this line ends with a semicolon
{
    // therefore this is just a block of code not related to new GroupBox()
};
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();