Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
156 views
in Technique[技术] by (71.8m points)

c# - Resize form from another form

I am running two forms simultaneously and I am trying to resize Form1 by calling a Form1 method with an event in Form2. With the following code the proper size values are displayed in the console, but the size of Form1 does not change. I have tried a number of approaches but I don't see why this does not work.

In Form1:

public void ResizeForm()
{
    Console.WriteLine(this.Size.ToString());
    this.Size = new System.Drawing.Size(600, 300);
}

In Form2:

private void ResizeCheckbox_CheckedChanged(object sender, EventArgs e)
{
    Form1 form = new Form1();
    form.ResizeForm();
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should pass the instance of the current Form1 to the second form. Add an instance in Form2 and then get it from Form1

Form2

Form1 _form1;

public Form2(Form1 form1)
 {
  InitializeComponent();
   _form1 = form1;
 }

private void ResizeCheckbox_CheckedChanged(object sender, EventArgs e)
{
    _form1.ResizeForm();            
}

Then open Form2 in the main form like this.

Form2 form2 = new Form2();
form2.Show((Form1)this); //I'm not sure if you need to cast "this" to From1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...