设计的组件在运行时不会在C#中更新

时间:2018-05-24 14:11:23

标签: c#

我设计了一个这样的简单组件:

public sealed partial class MySimpleComponent : Control
{
    public PictureBox picRed=new PictureBox();

    public MySimpleComponent()
    {
        SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        InitializeComponent();
        this.Controls.Add(picRed);
        picRed.Image = global::InterLockingV1.Properties.Resources.RedLamp1;
        picRed.SizeMode=PictureBoxSizeMode.StretchImage;
        picRed.Dock=DockStyle.Fill;
    }

    public MySimpleComponent(IContainer container)
    {
        container.Add(this);
        InitializeComponent();
    }
}

当我编译我的代码时,我可以看到工具箱中的组件并可以在表单上拖动它,它在设计时工作得很好(我可以看到图片框中的红灯图像),但是当我执行程序时,没有图像图片框上的apears, 我试图在运行时更改它,但没有改变:

MySimpleComponent1.picRed.Image=Image.FromFile(@"MyImagePath");
你能帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

尝试将代码放入Form_Load事件