我正在尝试将图片框动态添加到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方法中,并且执行得很好。

我无法通过Google在网上找到任何东西,这使我感到完全困惑。

最佳答案

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

关于c# - 如何通过单击按钮动态生成图片框?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18540325/

10-13 06:56