473,406 Members | 2,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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.

Aug 1 '07 #1
7 2542

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.
Aug 1 '07 #2
On 8 1 , 12 38 , Karthik D V <Karthik.Vee...@gmail.comwrote:
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.

Aug 1 '07 #3
How it is possible ?

FirstForm is a singleton ...

Boki wrote:
On 8 1 , 12 38 , Karthik D V <Karthik.Vee...@gmail.comwrote:
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.
Aug 1 '07 #4
Boki <bo**********@gmail.comwrote on 8/1/2007 10:10:08 AM
>On 8 1 , 12 38 , Karthik D V <Karthik.Vee...@gmail.comwrote:
>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
Aug 1 '07 #5
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
Aug 1 '07 #6
On Aug 1, 12:00 pm, Boki <boki.digi...@gmail.comwrote:
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

Aug 2 '07 #7
On Aug 1, 12:38 pm, Karthik D V <Karthik.Vee...@gmail.comwrote:
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

Aug 2 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Brian Underhill via DotNetMonster.com | last post by:
I have a textbox in form2 that i need to use in form1. How do I get acces to it? I am having trouble finding an example on this...probably due to inexperience thanks brian
0
by: Kitchen Bin | last post by:
HELP PLEASE!! The C# Visual Studio IDE is failing to load an inherited form into the IDE from a base form when I add items to an array property of a user control on the base form. That sounds...
5
by: nadir b | last post by:
hi I don't know how to change for exemple a form1 caption text from form2 don't forget that form2 has created from form1 I want sample code with c# *** Sent via Developersdex...
1
by: Richard | last post by:
Hello there, I have a form that is called from a Sub Main procedure using application.run(Form1). On my main form there is a button to open an instance of Form2 and then at the same time hide...
5
by: John | last post by:
Hi, I can't find a simple example for a simple(?) problem. I am working on an application with a variable in form1, that variable is needed in form2 for a calculation but i can't get that...
9
by: Marcel Saucier | last post by:
Hello everybody, I want to declare an array in form1, load it in form1 and then access that array in form2. New in VB, It took me a while to understand that declaring variables right under class...
3
by: Mateusz Rajca | last post by:
Hello, I have Form1 - the main form with some controls including a button. I have Form2 - the properties form with a combobox and button. The user can select the colors of a button in the...
3
by: R. Harris | last post by:
Hi. I have 2 forms: form1 form2 On Form2 I have a listbox and a button. When I click the button it calls a function from form1 and within that function it updates the listbox on form2. My...
5
by: GoGs | last post by:
HEEEELLLLLPP I hawe two Open form... Form1 and Form2 How I can from Form2 send text to textbox of Form1 //public System.Windows.Forms.TextBox textBox1; //Form1 fo = new Form1();...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.