There are several ways. As others have stated in answers, the following would be one solution
- Declare a public property that returns the
PictureBox
in Form1
- Pass an instance of
Form1
to Form2
, so Form2
can access this instance of Form1
and use the property
Source could look like this, for example:
In Form1
public PictureBox ThePictureBox
{
get { return this.pictureBox1; }
}
In Form2
private Form1 form1Instance;
public Form2(Form1 form1)
{
InitializeComponent();
form1Instance = form1;
}
public void Button_Click(object sender, EventArgs e)
{
this.form1Instance.ThePictureBox.Visible = true;
}
Another way would be: If Form2
is opened by Form1
, you could declare an event in Form2
, have Form1
subscribe to it and thus notify Form1
that the picture box should be visible. That way you do not have to expose a member that should otherwise be private to Form1
.
This could look like this:
In Form1
private void OpenForm2()
{
Form2 f2 = new Form2();
f2.ShowPictureBox += ShowPictureBox;
f2.Show();
}
private void ShowPictureBox(object sender, EventArgs e)
{
this.pictureBox.Visible = true;
}
In Form2
public event EventHandler<EventArgs> ShowPictureBox;
protected void OnShowPictureBox()
{
if (ShowPictureBox != null)
ShowPictureBox(this, EventArgs.Empty);
}
private void Button_Click(object sender, EventArgs e)
{
OnShowPictureBox();
}
I'm aware of the fact that your example code is Visual Basic, but as you tagged the question "C#" I guess you should be able to translate this to VB.NET easily - my VB.NET is not fluent enough :-)