如何在C# WinForm的ThirdForm中调整MainForm窗口大小?
解决C# WinForms中从ThirdForm修改MainForm窗口尺寸的问题
你遇到的NullReferenceException是因为打开ThirdForm时未正确设置Owner属性,导致this.Owner为null;另外即使设置了Owner,从SecondForm打开ThirdForm时,Owner指向的是SecondForm而非MainForm,直接修改Owner.Size只会改变SecondForm的尺寸,无法达到修改主窗体的需求。以下是几种可行的解决方案:
方案一:通过构造函数传递MainForm引用(推荐)
这种方式类型安全,直接明确传递目标窗体的引用,避免层级查找的复杂性。
- 修改
ThirdForm代码,新增接收MainForm的构造函数:
namespace WindowsFormsApplication8._1 { public partial class ThirdForm : Form { private readonly MainForm _mainForm; // 保留无参构造函数(设计器依赖) public ThirdForm() { InitializeComponent(); } // 新增接收MainForm实例的构造函数 public ThirdForm(MainForm mainForm) : this() { _mainForm = mainForm; } private void button1_Click(object sender, EventArgs e) { // 处理输入格式异常,避免程序崩溃 if (!int.TryParse(textBox1.Text, out int a)) { MessageBox.Show("请输入有效的数字"); return; } if (radioButton1.Checked) { // 修改MainForm的尺寸 _mainForm.Size = new Size(_mainForm.Width + a, _mainForm.Height + a); } else if (radioButton2.Checked) { // 修改自身尺寸(原有逻辑) this.Size = new Size(this.Width - a, this.Height - a); } } // 其他原有方法不变... } }
- 修改
MainForm中打开ThirdForm的代码:
private void изменениеToolStripMenuItem_Click_1(object sender, EventArgs e) { ThirdForm thirdForm = new ThirdForm(this); // 传递当前MainForm实例 thirdForm.ShowDialog(); }
- 修改
SecondForm中打开ThirdForm的代码,通过Owner获取MainForm实例:
private void buttonOK_Click_1(object sender, EventArgs e) { MainForm mainForm = Owner as MainForm; if (mainForm != null) { ThirdForm fm2 = new ThirdForm(mainForm); fm2.textBox1.Text = this.textBox1.Text; fm2.ShowDialog(); } }
方案二:通过设置Owner并递归查找MainForm
如果不想修改构造函数,可以通过设置Owner属性,再递归查找顶层的MainForm。
- 修改
MainForm打开ThirdForm的代码:
private void изменениеToolStripMenuItem_Click_1(object sender, EventArgs e) { ThirdForm thirdForm = new ThirdForm(); thirdForm.Owner = this; // 设置Owner为当前MainForm thirdForm.ShowDialog(); }
- 修改
SecondForm打开ThirdForm的代码:
private void buttonOK_Click_1(object sender, EventArgs e) { ThirdForm fm2 = new ThirdForm(); fm2.Owner = this.Owner; // 继承SecondForm的Owner(即MainForm) fm2.textBox1.Text = this.textBox1.Text; fm2.ShowDialog(); }
- 修改
ThirdForm的button1_Click方法,添加查找逻辑:
private void button1_Click(object sender, EventArgs e) { if (!int.TryParse(textBox1.Text, out int a)) { MessageBox.Show("请输入有效的数字"); return; } MainForm mainForm = FindMainForm(this); if (mainForm == null) { MessageBox.Show("无法找到主窗体"); return; } if (radioButton1.Checked) { mainForm.Size = new Size(mainForm.Width + a, mainForm.Height + a); } else if (radioButton2.Checked) { this.Size = new Size(this.Width - a, this.Height - a); } } // 递归查找顶层的MainForm private MainForm FindMainForm(Form currentForm) { if (currentForm is MainForm target) { return target; } return currentForm.Owner != null ? FindMainForm(currentForm.Owner) : null; }
方案三:使用静态属性保存MainForm实例
这种方式实现简单,但需注意窗体生命周期,避免内存泄漏。
- 修改
MainForm代码,添加静态属性:
namespace WindowsFormsApplication8._1 { public partial class MainForm : Form { public static MainForm Instance { get; private set; } public MainForm() { InitializeComponent(); Instance = this; // 初始化时赋值 } // 其他原有方法不变... } }
- 修改
ThirdForm的button1_Click方法:
private void button1_Click(object sender, EventArgs e) { if (!int.TryParse(textBox1.Text, out int a)) { MessageBox.Show("请输入有效的数字"); return; } if (MainForm.Instance != null && radioButton1.Checked) { MainForm.Instance.Size = new Size(MainForm.Instance.Width + a, MainForm.Instance.Height + a); } else if (radioButton2.Checked) { this.Size = new Size(this.Width - a, this.Height - a); } }
内容的提问来源于stack exchange,提问作者Xielir
相关产品推荐
相关产品推荐

