C# 如何通过单击按钮动态生成图片框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18540325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:31:17  来源:igfitidea点击:

How to dynamically produce pictureboxes, via button click?

c#.netwinformsvisual-studio-2010

提问by Codefun64

I'm trying to add pictureboxes dynamically to a C# win32 form for a near-production-quality application I'm trying to build, and I've got it pretty much down.

我正在尝试将图片框动态添加到 C# win32 表单中,以用于我尝试构建的接近生产质量的应用程序,但我已经把它搞定了。

The problem I'm running into is that I can't seem to add pictureboxes or controls to the form dynamically, in a method. I have added them to the form via the Form1 initializing method, but if I add controls in, say, a button_click method, it simply doesn't add them unless I have a panel container, and type panel1.Controls.Add(stuff). But, then the picturebox appears in a completely different location than intended - and blocked by, apparently, the panel itself. Most of the picturebox is blocked by the panel, and part of it is -outside- the panel. I have no idea what's going on.

我遇到的问题是我似乎无法在方法中动态地向表单添加图片框或控件。我已经通过 Form1 初始化方法将它们添加到表单中,但是如果我在 button_click 方法中添加控件,除非我有一个面板容器,否则它不会添加它们,然后键入 panel1.Controls.Add(stuff) . 但是,然后图片框出现在与预期完全不同的位置 - 并且显然被面板本身挡住了。大部分图片框被面板挡住了,一部分在面板之外。我不知道是怎么回事。

Here's the code I'm using that I learned online to add the pictureboxes, in a button click method:

这是我在网上学到的用于在按钮单击方法中添加图片框的代码:

PictureBox pb = new PictureBox();
        pb.Size = new Size(this.Size.Width / 14, this.Size.Width / 12);  //I use this picturebox simply to debug and see if I can create a single picturebox, and that way I can tell if something goes wrong with my array of pictureboxes. Thus far however, neither are working.
        pb.BackgroundImage = Properties.Resources.cardback;
        pb.BackgroundImageLayout = ImageLayout.Stretch;
        pb.Location = new Point(50, 50);
        pb.Anchor = AnchorStyles.Left;
        pb.Visible = true;
        InitializeComponent();
        this.Controls.Add(pb);
        PictureBox[] pbName = new PictureBox[totaldeckcount];
        for (int i = 0; i < totaldeckcount; i++)
        {
            pbName[i] = new PictureBox();
            pbName[i].Size = new Size(this.Size.Width / 14, this.Size.Width / 12);
            pbName[i].BackgroundImage = Properties.Resources.cardback;
            pbName[i].BackgroundImageLayout = ImageLayout.Stretch;
            pbName[i].Image = Properties.Resources.cardback;
            pbName[i].Anchor = AnchorStyles.Left;
            pbName[i].Visible = true;
            int x = 0;
            int y = 15;
            if (i > 10)
            {
                y += (int)((this.Size.Height * i) + 30);
            }
            x = (int)((this.Size.Width / 12) * Math.IEEERemainder(i, 10));
            pbName[i].Location = new Point(x, y);
            this.Controls.Add(pbName[i]);
        }

Cardback is a working texture, I've seen the thing pop up when I try making a picturebox with it in Form1's method, so that's not the problem. The problem doesn't -appear- to be my syntax, since I was able to copy the

Cardback 是一种有效的纹理,当我尝试在 Form1 的方法中使用它制作图片框时,我看到它弹出,所以这不是问题。问题不是 - 似乎 - 是我的语法,因为我能够复制

PictureBox pb = new PictureBox();

code directly into the Form1 method and it executed just fine.

直接将代码写入 Form1 方法,它执行得很好。

I can't find anything online via google, and this has me completely stumped.

我无法通过谷歌在网上找到任何东西,这让我完全被难住了。

采纳答案by Codefun64

It turns out, the problem is that there was a picturebox acting as a background image for the program, that was almost as large as the winform itself. The picturebox blocked all controls that were programatically created, thus giving the illusion of the code doing nothing.

事实证明,问题在于有一个作为程序背景图像的图片框,它几乎和 winform 本身一样大。图片框阻止了所有以编程方式创建的控件,从而给人以代码无所作为的错觉。

回答by yasmuru

May this code help you to get concept : This is the one i'm working for creating picturebox,textbox and button dynamically in flowlayoutpanel.

可能这段代码可以帮助您获得概念:这是我正在工作的用于在 flowlayoutpanel 中动态创建图片框、文本框和按钮的代码。

   PictureBox[] pics = new PictureBox[50];
    TextBox[] txts = new TextBox[50];
    Button[] butns = new Button[50];
    FlowLayoutPanel[] flws = new FlowLayoutPanel[50]
    static int brh =0; 


    for (int i = 0; i < totalnumbers; i++)
        {
            flws[i] = new FlowLayoutPanel();
            flws[i].Name = "flw" + i;
            flws[i].Location = new Point(3,brh);
            flws[i].Size = new Size(317,122);
            flws[i].BackColor = Color.DarkCyan;
            flws[i].BorderStyle = BorderStyle.Fixed3D;
            flws[i].Disposed += Form1_Disposed;               
            flws[i].Click += new EventHandler(butns_Click);

            pics[i] = new PictureBox();
            pics[i].Location = new Point(953, 95 + brh);
            pics[i].Name = "pic" + i;
            pics[i].Size = new Size(300, 75);
            pics[i].ImageLocation = "E:/image"+i;
            flws[i].Controls.Add(pics[i]);

            txts[i] = new TextBox();
            txts[i].Name = "txt" + i;
            txts[i].Location = new Point(953, 186 + brh);
            txts[i].TextChanged += Form1_TextChanged;
            flws[i].Controls.Add(txts[i]);

            butns[i] = new Button();
            butns[i].Click += new EventHandler(butns_Click);
            butns[i].Text = "submit";
            butns[i].Name = "but" + i;
            butns[i].Location = new Point(1100, 186 + brh);

            flws[i].Controls.Add(butns[i]);
            this.Controls.Add(flws[i]);
            flowLayoutPanel1.Controls.Add(flws[i]);
            brh += 130;
        }  


 private void butns_Click(object sender, EventArgs e)
    {
        // you can add the procces to perform after dynamically created button pressed
     }