0

I have a windows form application where i have picturebox1 in Form. I have a user control and a button1 in that userControl. On click of that button i want to change the picturebox1 image in Form. Please check my code:

public partial class LoggedInForm : Form
{
    public LoggedInForm()
    {
        InitializeComponent();

    }
    public PictureBox setImg
    {
        get { return pictureBox1; }
        set { pictureBox1 = value; }
    }
 }

public partial class AddUserGroup : UserControl
{
    private void Button1_Click(object sender, EventArgs e)
    {
        LoggedInForm x = new LoggedInForm();
        x.setImg.Image = NewProj.Properties.Resources.Logo;

    }
 }
  • What is the issue? – EylM Aug 08 '19 at 11:05
  • image does not change to the new one, on button click. i tried making the picturebox public. still does not work. – Joy D'souza Aug 08 '19 at 11:07
  • You are creating a new form by calling: LoggedInForm x = new LoggedInForm(); Then you change the picture, but you dont call .Show or ShowDialog. – EylM Aug 08 '19 at 11:08
  • Add `x.Show()` if you want to see the image. Well, surely not what you want. Raise an event instead so the form class can subscribe it and modify the image. Most basic way to do so is to call base.OnClick so the user control's Click event fires. – Hans Passant Aug 08 '19 at 11:09
  • can you provide some code or guide to change image on existing form. the control is present on that form. – Joy D'souza Aug 08 '19 at 11:12
  • [Pass click event of child control to the parent control](https://stackoverflow.com/a/36130796/3110834) – Reza Aghaei Aug 08 '19 at 14:13

1 Answers1

0

I am not sure, if I unterstood your question correctly. But you can show an image in a PictureBox this way:

public partial class LoggedInForm : Form
{
    public LoggedInForm()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        //Shows the Bitmap on the PictureBox
        pictureBox1.Image = NewProj.Properties.Resources.Logo;
    }
}

The PictureBox have to be on the LoggedInForm. Then you can assign an Image Object to it and it appears immediately on the window.

You have two partial classes and probably two forms and it seems that you want to update the first with the second window. If you really want to run them simultaneously you can get more information on this Question. It is a bit more complicated.

Oliver
  • 93
  • 1
  • 7