PC Review


Reply
Thread Tools Rate Thread

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

 
 
Boki
Guest
Posts: n/a
 
      1st Aug 2007
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.

 
Reply With Quote
 
 
 
 
Karthik D V
Guest
Posts: n/a
 
      1st Aug 2007

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();

}
}

Boki wrote:
> 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.


 
Reply With Quote
 
 
 
 
Boki
Guest
Posts: n/a
 
      1st Aug 2007
On 8 1 , 12 38 , Karthik D V <Karthik.Vee...@gmail.com> wrote:
> 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();
>
> }
> }
>
> Boki wrote:
> > 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.


Hi,

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



Best regards,
Boki.

 
Reply With Quote
 
Karthik D V
Guest
Posts: n/a
 
      1st Aug 2007
How it is possible ?

FirstForm is a singleton ...

Boki wrote:
> On 8 1 , 12 38 , Karthik D V <Karthik.Vee...@gmail.com> wrote:
> > 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();
> >
> > }
> > }
> >
> > Boki wrote:
> > > 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.

>
> Hi,
>
> After do that, I saw two FirstForms....
>
>
>
> Best regards,
> Boki.


 
Reply With Quote
 
leon.friesema@frost-nospam-bits.nl
Guest
Posts: n/a
 
      1st Aug 2007
Boki <(E-Mail Removed)> wrote on 8/1/2007 10:10:08 AM
>On 8 1 , 12 38 , Karthik D V <Karthik.Vee...@gmail.com> wrote:
>> 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();
>>
>> }
>> }
>>
>> Boki wrote:
>> > 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.

>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
 
Reply With Quote
 
Peter Duniho
Guest
Posts: n/a
 
      1st Aug 2007
Boki wrote:
> [...]
> (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
 
Reply With Quote
 
Garfilone
Guest
Posts: n/a
 
      2nd Aug 2007
On Aug 1, 12:00 pm, Boki <boki.digi...@gmail.com> wrote:
> 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

 
Reply With Quote
 
Garfilone
Guest
Posts: n/a
 
      2nd Aug 2007
On Aug 1, 12:38 pm, Karthik D V <Karthik.Vee...@gmail.com> wrote:
> 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();
>
> }
> }
>
> Boki wrote:
> > 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.


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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reference a variable in form1 from form2 in C#? Brian Underhill via DotNetMonster.com Microsoft Dot NET 1 26th Oct 2005 06:40 PM
variable declaration in form1 accessible in form2... =?Utf-8?B?TWFyY2VsIFNhdWNpZXI=?= Microsoft VB .NET 9 13th Oct 2005 05:09 PM
On form1 I instantiate form2 with a list box. I do I handle listboxclick from form1 PAPutzback Microsoft VB .NET 5 23rd Dec 2004 08:13 PM
Form2 open and Fill in Form1 panel, but Form2 "Activited" not functioning A-PK Microsoft Dot NET Framework Forms 0 19th Mar 2004 08:56 AM
Form1 open Form2, Form2 how to change text in Form1.label ? Tee Microsoft VB .NET 3 11th Mar 2004 09:49 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:40 AM.