views:

162

answers:

2

Hi, I'll first tell you what I am trying to do, and then how I am trying to do it. If there is a better way please let me know.

I am working with two forms - lets call them form_main and form_preferences

When the form_preferences form is up, I want form_main to be disabled until a button (save button) on the form_preferences is clicked.

So here is what I am doing: Button is clicked in form_main

form_preferences frm_p = new form_preferences();
frm_p.Visible = true;
this.enabled = false;

//so far so good

When I hit save on form_preferences it calls a public static method in form_main which needs to enable the form again. Unfortunately I cannot do this directly because it is a static method.

I tried creating a new instance of the form, but then I would have to create the new instance and destroy the original one which seems like a big waste and inefficient. Is there a better way to do this?

+1  A: 

Change the form_main method to an instance method instead of static. Pass the instance of form_main to your sub_form when you create it, so it keeps a reference.

Matt Howells
I tried this, but the frm_main.Enabled = true doesn't do anything on the main_form that I passed when I created this second form. public partial class frmsettings : Form { Form frm_main; public frmsettings(Form frmparent) { InitializeComponent(); frm_main = frmparent; } private void cmdsave_settings_Click(object sender, EventArgs e) { frm_main.Enabled = true; this.Close(); }
Didn't know that doesn't keep formats.Basically in my public class, I have a public sub_form(Form frmparent){} which sets a global variable in the class equal to that that was passed in. Later on when it is called it seems to have no effect on the parent form. Am I missing something here?
Okay so frm_main.Enabled = true; does not work, but frm_main.Close(); does for some reason....
Nevermind, I'm an idiot, it works. I had a different object disabled instead of the form. Thanks for the help!
A: 

If there would be only one copy of your main form then make your form as singleton or use a static reference in your program to access it from within a static method.

In your main method do the following...

public static class Program {

  public static YourFrom form = null;

  public static void Main(string args[]) {
    Program.form = new YourFrom();
    Application.Run(form);   
  }
}

And whenever you need to access the form object in your class use the Program.form object to access it.

S M Kamran