Reputation: 1
I am writing C# code in VS windowsforms I have a problem, I need to resize the parent window of Form1 by clicking a button in the Form2 window. The main problem is that I don't understand how to access Form1. I can access Form2 via (this.Size).
I've been trying to get through this.Parent.Size and this.Owner.Weight, but it throws an error (NullReferenceException not handled Unhandled exception of type "System.NullReferenceException" in WindowsFormsApplication8.1.exe )
I have 3 forms: Form1:
namespace WindowsFormsApplication8._1
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void выходToolStripMenuItem_Click_1(object sender, EventArgs e)
{
Application.Exit();
}
private void вводToolStripMenuItem_Click_1(object sender, EventArgs e)
{
SecondForm secondForm = new SecondForm();
secondForm.ShowDialog();
изменениеToolStripMenuItem.Visible = true;
}
private void изменениеToolStripMenuItem_Click_1(object sender, EventArgs e)
{
ThirdForm thirdForm = new ThirdForm();
thirdForm.ShowDialog();
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
form2
namespace WindowsFormsApplication8._1
{
public partial class SecondForm : Form
{
public SecondForm()
{
InitializeComponent();
}
private void SecondForm_Load(object sender, EventArgs e)
{
}
private void buttonOK_Click_1(object sender, EventArgs e)
{
ThirdForm fm2 = new ThirdForm();
fm2.textBox1.Text = this.textBox1.Text;
fm2.ShowDialog();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
}
}
form3
{
public partial class ThirdForm : Form
{
public ThirdForm()
{
InitializeComponent();
}
private void ThirdForm_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
int a = int.Parse(textBox1.Text);
int pixels = 100;
if (radioButton1.Checked)
{
this.Owner.Size = new Size(this.Width + a, this.Height + a);
}
else if (radioButton2.Checked)
{
this.Size = new Size(this.Width - a, this.Height - a);
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
Need to change Mainform scale from ThirdForm
Upvotes: -1
Views: 55
Reputation: 3271
For the quickest method with your current code, you need to set the owner of the form and then propagate this ownership along the chain - the Owner of Form3 will be Form1 (not the best idea).
In Form1, change your code to be as:
private void вводToolStripMenuItem_Click_1(object sender, EventArgs e)
{
SecondForm secondForm = new SecondForm();
secondForm.ShowDialog(this); // <-- This line is changed
изменениеToolStripMenuItem.Visible = true;
}
In Form2, change your code to be as:
private void buttonOK_Click_1(object sender, EventArgs e)
{
ThirdForm fm2 = new ThirdForm();
fm2.textBox1.Text = this.textBox1.Text;
fm2.ShowDialog(this.Owner); // <-- This line is changed
}
Form3 should now work without further changes.
The method you are using is not the best as it tightly couples all the forms together, if one Form changes, then the others will have to be updated as well.
A better option is to use Events. This way, you can adjust how each form handles the event and will take care of its own sizing and manipulation without external code changing how a form looks or behaves.
Start by declaring a new class similar to:
public class NewSizeEventArgs : EventArgs
{
public Size Size { get; set; }
public int Value { get; set; }
}
Change Form3 to be:
// Add this to the Form3 class
public event EventHandler<NewSizeEventArgs> ChangeSize;
Change your button handler to look like this:
if (radioButton1.Checked)
{
ChangeSize?.Invoke(this, new NewSizeEventArgs {Size = this.Size, Value = a});
//this.Owner.Size = new Size(this.Width + a, this.Height + a);
}
else if (radioButton2.Checked)
{
this.Size = new Size(this.Width - a, this.Height - a);
}
Change Form2 to be:
// Add this to the Form3 class
public event EventHandler<NewSizeEventArgs> ChangeSize;
private void buttonOK_Click_1(object sender, EventArgs e)
{
ThirdForm fm2 = new ThirdForm();
fm2.ChangeSize += (o, args) => { ChangeSize?.Invoke(o, args); };
fm2.textBox1.Text = this.textBox1.Text;
fm2.ShowDialog(this.Owner); // <-- This line is changed
}
Finally, Change Form1 to be:
private void вводToolStripMenuItem_Click_1(object sender, EventArgs e)
{
SecondForm secondForm = new SecondForm();
SecondForm.ChangeSize += SecondFormOnChangeSize;
secondForm.ShowDialog(this); // <-- This line is changed
изменениеToolStripMenuItem.Visible = true;
}
private void SecondFormOnChangeSize(object sender, NewSizeEventArgs e)
{
// Handle the event...
this.Size = new Size(e.Size.Width + e.Value, e.Size.Height + e.Value);
}
The usage of Events can provide you with a lot of flexibility in other areas as well, not just in this one small section. It is often used of notifying Parents of a change without necessarily know who or what the parent is.
Upvotes: 0