如何通过按钮点击动态生成图片框?

时间:2013-08-30 19:38:06

标签: c# .net winforms visual-studio-2010

我正在尝试动态地将图片框添加到C#win32表单中,以便我正在尝试构建一个接近生产质量的应用程序,而且我已经将它降级了。

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

这是我正在使用的代码,我通过按钮点击方法在线学习添加图片框:

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是一种工作纹理,当我尝试用Form1的方法制作一个带有它的图片框时,我看到了这个东西弹出来,所以这不是问题所在。问题不会出现 - 我的语法,因为我能够复制

PictureBox pb = new PictureBox();

直接编写Form1方法,并执行得很好。

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

2 个答案:

答案 0 :(得分:5)

这段代码可以帮助您获得概念: 这是我正在为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
     }

答案 1 :(得分:3)

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