c#winforms动态创建的Picturebox的实例化不起作用

时间:2017-07-27 17:27:02

标签: c# winforms

我试图实例化在运行时创建的两个图片框。我已经将它们分配给一个面板,我尝试在表单加载事件中实例化它。 这是我的班级,有两个图片框和面板:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;

namespace AudioNodeGUI.Klassen
{
    public class DateiÖffnenUndAuswählenSteuerelement : Form
    {
        public int xLoc = 0;
        public int yLoc = 0;
        public Point loc1;
        public Point loc2;
        public Panel elementpanel;

        public DateiÖffnenUndAuswählenSteuerelement()
        {
            this.loc1 = new Point(xLoc, yLoc);
            this.loc2 = new Point(xLoc + 41, yLoc);
            this.elementpanel = new Panel
            {
                Name = "elementpanel",
                Size = new Size(154, 22),
                Location = new Point(xLoc, yLoc),
            };
        }

        public Panel Zeichnen()
        {
            PictureBox browseImage = new PictureBox()
            {
                Name = "browseimage",
                Size = new Size(41, 22),
                Location = loc1,
                Image = Image.FromFile(@"F:\AudioNodeGUI\images\Browse_used_files.jpg"),
                Visible = true,
            };
            PictureBox fileImage = new PictureBox()
            {
                Name = "fileimage",
                Size = new Size(113, 22),
                Location = loc2,
                Image = Image.FromFile(@"F:\AudioNodeGUI\images\Filebutton1.jpg"),
                Visible = true,
            };
            browseImage.Click += new EventHandler(browseImage_Clicked);
            fileImage.Click += new EventHandler(fileImage_Clicked);
            this.elementpanel.Controls.Add(browseImage);
            this.elementpanel.Controls.Add(fileImage);
            return elementpanel;
        }

        public void browseImage_Clicked(object sender, System.EventArgs e)
        {

        }

        public void fileImage_Clicked(object sender, System.EventArgs e)
        {
        OpenFileDialog open = new OpenFileDialog();
        open.Filter = "Audio Files(*.wav; *.mp3; *.aif; *.aiff)";
        }
    }
}

在表单加载事件中,我尝试实例化自定义控件:

private void AudioNodeWindow_Load(object sender, EventArgs e)
    {
        AudioNodeGUI.Klassen.DateiÖffnenUndAuswählenSteuerelement element = new AudioNodeGUI.Klassen.DateiÖffnenUndAuswählenSteuerelement();
        Panel panel = element.Zeichnen();
        this.Controls.Add(panel);

    }

但是当我运行程序时,控件没有显示出来。你知道我哪里弄错了吗?

0 个答案:

没有答案