Modify form1 properties from form2 ( not access variable of form1 )

B

Boki

Hi All,

I want to change WindowState of form1 from form2.

I tried these two methods, but no luck on both.

(1) Declare a public method:

/* function of form1 */
public void active_this_form()
{
this.WindowState = FormWindowState.Normal;
this.Focus();
this.Show();
}

After call this function from form2, form1 keeps minimized.

(2) Use reference:

/* Code in form1 */
public partial class Form1 : Form
{
public static Form1 staticVar = null;
....

/* Code in form2 */
Form1.staticVar.WindowState = FormWindowState.Normal;

This code can't compile.

The second method can modify a variable, but seems can't modify the
properties of form ?

Thanks!
Best regards,
Boki.
 
K

Karthik D V

Hi ,
I hope the following code will help you.
On Click OK button in second button, i'm changing the
FirstForm properties.

Thanks,
Karthik D V

using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Utilities
{
public partial class SecondForm : Form {

public SecondForm()
{
InitializeComponent();

}

private void SecondForm_Load(object sender, EventArgs e)
{

}

private void btnOk_Click(object sender, EventArgs e)
{
FirstForm.Instance.WindowState = FormWindowState.Normal;
FirstForm.Instance.TopMost = true; //This line will avoid
the minimization of the form.
FirstForm.Instance.Show();
}
}

public partial class FirstForm : Form
{
public static FirstForm instance = null;

public static FirstForm Instance
{
get
{
if (instance == null)
{
instance = new FirstForm();
}
return instance;
}

}

private FirstForm()
{
InitializeComponent();

}
}
 
B

Boki

Hi ,
I hope the following code will help you.
On Click OK button in second button, i'm changing the
FirstForm properties.

Thanks,
Karthik D V

using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Utilities
{
public partial class SecondForm : Form {

public SecondForm()
{
InitializeComponent();

}

private void SecondForm_Load(object sender, EventArgs e)
{

}

private void btnOk_Click(object sender, EventArgs e)
{
FirstForm.Instance.WindowState = FormWindowState.Normal;
FirstForm.Instance.TopMost = true; //This line will avoid
the minimization of the form.
FirstForm.Instance.Show();
}
}

public partial class FirstForm : Form
{
public static FirstForm instance = null;

public static FirstForm Instance
{
get
{
if (instance == null)
{
instance = new FirstForm();
}
return instance;
}

}

private FirstForm()
{
InitializeComponent();

}
}

Hi,

After do that, I saw two FirstForms....



Best regards,
Boki.
 
L

leon.friesema

Boki said:
Hi,
After do that, I saw two FirstForms....
Best regards,
Boki.
<hr>

Assuming you're creating a SDI envir, the Form2 (the caller) doesn't "know" Form1 the callee.
You might want to try to pass Form1 as 'ref' parameter to the ctor of Form2 (check for null on WindowState change) : like this:

public partial class Form2
{
private static Form1 f1;
// ctor:
public Form2(ref Form1 Callee)
{
f1 = Callee;
}
public void MaxForm1()
{
if (f1 == null)
{
f1 = new Form1;
}
f1.WindowState = FormWindowState.Maximized;
f1.Show();
}
}

Leon
 
P

Peter Duniho

Boki said:
[...]
(1) Declare a public method:

/* function of form1 */
public void active_this_form()
{
this.WindowState = FormWindowState.Normal;
this.Focus();
this.Show();
}

After call this function from form2, form1 keeps minimized.

This should work, or at least something similar to it. Have you tried
putting the Focus() and/or Show() calls before the WindowState call?

Also, if you can call a public method on form1, why can't you just set
the property directly if you have to? In either case, you need a
reference to the form instance. If you can do one, you should be able
to do the either.

This is a good example of why you should post a complete-but-concise
example of code that reliably reproduces your problem. The above isn't
that. With a complete-but-concise example of code that reliably
reproduces the problem, it would be easier for someone to actually
compile your code and see what's going on.
(2) Use reference:

/* Code in form1 */
public partial class Form1 : Form
{
public static Form1 staticVar = null;
....

/* Code in form2 */
Form1.staticVar.WindowState = FormWindowState.Normal;

This code can't compile.

Why not? What error do you get? How do you initialize "staticVar"?
The second method can modify a variable, but seems can't modify the
properties of form ?

There's no reason you shouldn't be able to modify a writeable property
of a Form instance, regardless of how you do it. You have jumped to an
incorrect conclusion based on some problem (but you haven't described
the problem completely, so it's impossible to say where the error in
your conclusion came from).

That said, this "staticVar" method is pretty bad, unless you really do
have a singleton form and you use exactly the code that Karthik posted
(that is, with a private constructor, so that you can only instantiate
the form through the Instance property).

You could also use the code that Leon posted, but as I mentioned in my
comments to your #1 attempt, it doesn't seem as though getting at the
form1 instance is actually your problem, since you apparently can call
the public method you created.

Again, a concise-but-complete sample of code would go a long way to
making your question make more sense here.

Pete
 
G

Garfilone

Hi All,

I want to change WindowState of form1 from form2.

I tried these two methods, but no luck on both.

(1) Declare a public method:

/* function of form1 */
public void active_this_form()
{
this.WindowState = FormWindowState.Normal;
this.Focus();
this.Show();
}

After call this function from form2, form1 keeps minimized.

(2) Use reference:

/* Code in form1 */
public partial class Form1 : Form
{
public static Form1 staticVar = null;
...

/* Code in form2 */
Form1.staticVar.WindowState = FormWindowState.Normal;

This code can't compile.

The second method can modify a variable, but seems can't modify the
properties of form ?

Thanks!
Best regards,
Boki.

Expose the property of Form1 to Form2
 
G

Garfilone

Hi ,
I hope the following code will help you.
On Click OK button in second button, i'm changing the
FirstForm properties.

Thanks,
Karthik D V

using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Utilities
{
public partial class SecondForm : Form {

public SecondForm()
{
InitializeComponent();

}

private void SecondForm_Load(object sender, EventArgs e)
{

}

private void btnOk_Click(object sender, EventArgs e)
{
FirstForm.Instance.WindowState = FormWindowState.Normal;
FirstForm.Instance.TopMost = true; //This line will avoid
the minimization of the form.
FirstForm.Instance.Show();
}
}

public partial class FirstForm : Form
{
public static FirstForm instance = null;

public static FirstForm Instance
{
get
{
if (instance == null)
{
instance = new FirstForm();
}
return instance;
}

}

private FirstForm()
{
InitializeComponent();

}
}

Return the ref of a whole form is not fit i think
 

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