tags:

views:

69

answers:

3

Basically, I have a settings window, and when you click "OK", it's suppose to apply settings to the main form (eg, set font of a control, etc), and then close.

frmmain frm = new frmmain();
frm.OLVAltBackColor = Color.Aquamarine ;

I tried that, but it only applies the settings to that instance, and you can see it if you do frm.Show();

I'm trying to make it so the already opened form has it's control's properties changed.

A: 

Apply the property change to the form that already exists and is already shown instead of creating a new form and changing that one.

Greg D
it will not let me access the properties of the already existing form :/ Unless I'm doing something wrong?
Mike
A: 

In this code you're creating a new instance of the frmmain. Any changes you make to that new object will happen in the new object, not the one you actually want to change.:

frmmain frm = new frmmain(); //Creating a new object isn't the way.
frm.OLVAltBackColor = Color.Aquamarine ;

What you're looking for is a way to call on the already existant frmmain class and change the property of that.

Edit, for example:

using System;
class Statmethod
{
  //A class method declared
  static void show()
  {
    int x = 100;
    int y = 200;
    Console.WriteLine(x);
    Console.WriteLine(y);
  }

  public static void Main()
  {
    // Class method called without creating an object of the class
    Statmethod.show();
  }
}
Sergio Tapia
A: 

What you are trying to do is not working because you are creating a NEW instance of your main form and updating that rather than the first instance. It is possible to update the main form by keeping a reference to it in your settings form... but...

...it sounds like you are approaching this from the wrong direction.

Don't make the settings form dependent on the main form. Instead create the settings form from the main dialog.

class SettingsForm : Form
{
   // You need to ensure that this color is updated before the form exits
   // either make it return the value straight from a control or set it 
   // as the control is updated
   public Color OLVAltBackColor
   {
       get;
       private set;
   }
}

In your main form (I'm assuming some kind of button or menu click)

private void ShowSettingsClicked(object sender, EventArgs args)
{
   using (SettingsForm settings = new SettingsForm())
   {
       // Using 'this' in the ShowDialog parents the settings dialog to the main form
       if (settings.ShowDialog(this) == DialogResult.OK)
       {
           // update settings in the main form
           this.OLVAltBackColor = settings.OLVAltBackColor;
       }
   }

}
Matt Breckon
This worked after fidling around (had to add This.Dialogresult = DialogResult.Ok;) - Learn something new everyday :) tysm
Mike
You can set that in the designer by changing the DialogResult property of your OK button to be 'OK' and the DialogResult of your cancel button to be 'Cancel'.
Matt Breckon