0

So I have this PictureBox and Label on form1 which is hidden and I want to show it when you press a Button in form2.

Form1 code:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.Label3.Hide()
    Me.PictureBox3.Hide()

Form2 Code:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    PictureBox3.Show()

But picturebox3 is not declared in form2 so how can I access it?

TheLethalCoder
  • 6,668
  • 6
  • 34
  • 69
Krizzo
  • 25
  • 4
  • 2
    I can smell bad design... But if you need to go this way, you should have an instance of Form2 in your Form1 instance. Pass it when you create Form1 and then declare a internal/public method you're going to call in Form2 which will do the thing that you need. – Alex Jul 12 '16 at 10:48
  • @Alex, In an MDI application, scenario like these looks valid. Instead of creating a new control, reusing an existing control is better. – Kira Jul 12 '16 at 11:22
  • @Anand What does that have to do with MDI? – Thorsten Dittmar Jul 12 '16 at 11:27
  • I just mentioned a use case against @Alex comment. I didn't mean like OP is asking about MDI forms – Kira Jul 12 '16 at 11:41
  • @Anand I still don't get how MDI is a use case of the OP's problem... Any situation where you need to access a property in one class instance from another class instance is a use case for this problem and the solution is always the same. – Thorsten Dittmar Jul 12 '16 at 12:29

3 Answers3

1

There are several ways. As others have stated in answers, the following would be one solution

  1. Declare a public property that returns the PictureBox in Form1
  2. 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 :-)

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
0

First of all, you need to be able to access your elements from the other forms (as you should make access modifiers internal or public). Then you can call the related elements from another form by creating instance or passing the form to a method in other form. You should check this link for detailed explanation by the way.

Community
  • 1
  • 1
ilkerkaran
  • 4,214
  • 3
  • 27
  • 42
0

In some situations, we need to use a control in more than one forms. This can be achieved by sharing the control between different forms

In your case, add the control in Form2 during button click, it should work.

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
     Me.Controls.Add(Form1.PictureBox)
End Sub

Note: Form1.PictureBox is a shared property. We should define it like this in Form1.vb

public Shared Property PictureBox As PictureBox
    Get
        Return Me.PictureBox3
    End Get
End Property
Kira
  • 1,403
  • 1
  • 17
  • 46
  • Im pretty new, what should i do after i wrote Me.Controls.Add(Form1.PictureBox) – Krizzo Jul 12 '16 at 13:51
  • Make sure your picture box is visible, have size, location, etc Your form will be repainted after the line Me.Controls.Add along with the picture box. – Kira Jul 13 '16 at 03:56