Click here to Skip to main content
15,900,110 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I have a main form and another independent form. I want to set the picturebox.visible value to true on my mainform by clicking a button on the other form. How can I do this?
Posted
Updated 4-Mar-10 8:28am
v2

One way to do it would be enable a property on one form and then call it from the other.
 
Share this answer
 
Assuming that 'mainform' instanciates the 'otherform' then you should raise an event.

There is an example in this tip.[^]. It uses a string value but you can easily alter it to pass a bool to set the Visible property of the PictureBox.

[edit] OK - so why the 1 vote? Accurate information that follows OOP best practices along with a link to example code, along with an explanation below on why the advice given previously was not great. What more could I do to help the OP? [edit]
 
Share this answer
 
v2
Not sure how you're creating the forms, but let's say that the main form is created first and the independent form is created from the main form. So, in your independent form declaration, pass in the main form so that you can access it's properties

C#
IndependentForm form2 = New IndependentForm(this);
form2.Show();


The IndependentForm would look like
C#
public partial class IndependentForm : Form
{
  MainForm _parent;
  Boolean _pictureBoxVisble;

  public IndependentForm(MainForm parent)
  {
    _parent = parent;
    _pictureBoxVisible = false;
  }

  public void Button1_Click(object sender, EventArgs e)
  {
    _pictureBoxVisble = !_pictureBoxVisible;
    _parent.SetPictureBoxVisible(_pictureBoxVisible);
  }
}


Then, when you click the button on the independent form, you have the reference to the initial form and you can call a property or function on the main form.

C#
mainForm.SetPictureBoxVisible(TrueOrFalseValue);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900