One form to another

T

Timothy Taylor

I need to set the property of one form from another(example: set
form1.backcolor on a button click on form2)
I have my main form and my main form launches form2. when hitting ok on
form 2 i need it to set the backcolor of my main form and then close
form2. How would i do this? What is the best way?

Thanks,

-Tim
 
N

Neil Enns [MSFT]

There's probably many ways to do this, but here's some pseudo code for the
way I've done similar things in the past:

class Form2 : Form
{
private Colour myColour;

public Colour MyColour
{
get { return myColour; }
set { myColour = value; }
}

private void button1_click(object sender, EventArgs e)
{
this.myColour = whateverYoureReadingTheColourFromOnForm2;
this.Close()
}
}

Then in Form1 you just do the following:

// Whatever method invokes Form2
{
Form2 myForm2 = new Form2();
myForm2.ShowDialog();
this.BackColor = myForm2.MyColour;
}

--
Neil Enns
Lead Program Manager
Microsoft Mobile Devices Product Group

This posting is provided "AS IS" with no warranties, and confers no rights.
 
T

Timothy Taylor

Yes, i know that would work, however that is launching a new form1. I need
to keep the old form1 the whole time. Form1 launches form2, gets a color,
and goes back to the same form one.

I need this to allow my user to select a color without it resetting all
their current changes to the form.

Thanks for your time,

-Tim
 
N

Neil Enns [MSFT]

Mmm, the code I posted below shouldn't get rid of Form1, should it? When
you're in the following code it's running Form1:

There's nothing in there that would make Form1 go away.


--
Neil Enns
Lead Program Manager
Microsoft Mobile Devices Product Group

This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

Chris Tacke, eMVP

I think pre-SP1, when the dilaog form is closed, the parent isn't
automatically brought to the fore, so you must manually do that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top