Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...