我正在 VS windowsforms 中编写 C# 代码 我有一个问题,我需要通过单击 Form2 窗口中的按钮来调整 Form1 父窗口的大小。主要问题是我不明白如何访问Form1。我可以通过 (this.Size) 访问 Form2。
我一直在尝试通过 this.Parent.Size 和 this.Owner.Weight,但它抛出一个错误(NullReferenceException 未处理) WindowsFormsApplication8.1.exe 中未处理的“System.NullReferenceException”类型异常)
我有3种形式: 表格一:
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)
{
}
}
}
表格2
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)
{
}
}
}
表格3
{
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)
{
}
}
}
需要从 ThirdForm 更改 Mainform 比例
对于使用当前代码的最快方法,您需要设置表单的 owner,然后沿链传播此所有权 - Form3 的所有者将是 Form1(不是最好的主意)。
在 Form1 中,将代码更改为:
private void вводToolStripMenuItem_Click_1(object sender, EventArgs e)
{
SecondForm secondForm = new SecondForm();
secondForm.ShowDialog(this); // <-- This line is changed
изменениеToolStripMenuItem.Visible = true;
}
在 Form2 中,将代码更改为:
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 现在应该可以正常工作,无需进一步更改。
您使用的方法不是最好的,因为它将所有表单紧密耦合在一起,如果一个表单发生更改,那么其他表单也必须更新。
更好的选择是使用Events。这样,您可以调整每个表单处理事件的方式,并处理其自身的大小调整和操作,而无需外部代码更改表单的外观或行为。
首先声明一个类似于以下内容的新类:
public class NewSizeEventArgs : EventArgs
{
public Size Size { get; set; }
public int Value { get; set; }
}
将 Form3 更改为:
// Add this to the Form3 class
public event EventHandler<NewSizeEventArgs> ChangeSize;
将按钮处理程序更改为如下所示:
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);
}
将 Form2 更改为:
// 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
}
最后将Form1修改为:
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);
}