简体   繁体   中英

Set child form image from parent form?

So I am making some achievement pop ups for a lame game and so I made a custom messagebox form and I have been successful in setting the popups picturebox image with local images but I need help using embedded resources as images.

So far Ive used constructors to set the images and string but I can't use them for embedded images.

Parent Form:

MessageForm MsgFrm = new MessageForm
{
AchievementString = "L33t H4x0r - Reach 1337 score.",
PictureString = "C:\\Users\\Resources\\H4x0r_50x50.jpg"
};
MsgFrm.Show();

Child Form:

public string Achstring { get; set; }
public string Picstring { get; set; }
private void MessageForm_Load(object sender, EventArgs e)
{
achievement_lbl.Text = AchievementString;
achievement_pic.Image = Image.FromFile(PictureString);
}

Using the code above I can only use local images and my goal is to use images embedded in resources but to pass them as an arg of sorts as above.

You should either pass the Image object to a constructor or declare an Image property to set.

public MessageForm(Image img)
{
    InitializeComponent();
    achievement_pic.Image = img;
}

Or

Parent form

MessageForm MsgFrm = new MessageForm
{
    AchievementString = "L33t H4x0r - Reach 1337 score.",
    PictureImg = embeddedPicture
};
MsgFrm.Show();

Child form

public string Achstring { get; set; }
public Image PicImage { get; set; }

private void MessageForm_Load(object sender, EventArgs e)
{
    achievement_lbl.Text = AchievementString;
    achievement_pic.Image = PicImage
}

If you need more control in the parent form, you could make the control public or create a public property for the image.

public Control PicControl => achievement_pic;

Using the info Ed Plunkett gave me I was able to come up with the solution.

First you have to give the PictureBox the public modifier in it's properties then,

MessageForm MsgFrm = new MessageForm
{
AchievementString = "L33t H4x0r - Reach 1337 score.",
};
MsgFrm.achievement_pic.Image = Properties.Resources.H4x0r_50x50;
MsgFrm.Show();

Done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM