在运行时将 PictureBox 添加到窗体 [英] Add PictureBox to form at runtime

查看:43
本文介绍了在运行时将 PictureBox 添加到窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个将生成图片框的 C# 程序:

I'm making a C# program that will generate a PictureBox:

private void Form1_Load(object sender, EventArgs e)
{
    PictureBox picture = new PictureBox
    {
        Name = "pictureBox",
        Size = new Size(16, 16),
        Location = new Point(100, 100),
        Image = Image.FromFile("hello.jpg"),
    };
}

但是,控件没有显示在我的表单上.为什么不呢?

However, the control doesn't show up on my form. Why not?

推荐答案

你可以试试这个..你需要使用 this.Controls.Add(picture);

you can try this.. you need use this.Controls.Add(picture);

private void Form1_Load(object sender, EventArgs e)
    {
        var picture = new PictureBox
        {
            Name = "pictureBox",
            Size = new Size(16, 16),
            Location = new Point(100, 100),
            Image = Image.FromFile("hello.jpg"),

        };
        this.Controls.Add(picture);
    }

如果你想在运行时从表单中删除.

and if you want to remove from form at runtime.

 //remove from form
 this.Controls.Remove(picture);
  //release memory by disposing
 picture.Dispose();

;

这篇关于在运行时将 PictureBox 添加到窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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