简体   繁体   中英

C# 'this.Width' or 'this.Height' is returned wrong after user resized the form

I've got a "normal" form, the user may resize it and so on. If he clicks a specific button, the formBorderStyle is changed to "none", so that he may not resize it anymore and so that the form size is not dynamic anymore. After this happened, the code is supposed to return form Width & Height. I do this by using

this.FormBorderStyle = FormBorderStyle.None;
            mid.X = this.Width / 2;
            mid.Y = this.Height / 2;

Note: "mid" is just a normal Point.

The problem:

The code ALWAYS returns the size 1080; 720. This is because the starting size is like that. The code is supposed to deliver the changed size (eg if the user changes the form size to 2000; 1000 it should deliver 2000;1000, not 1080;720. Why doesn't the program update the Size-values / Why are the old values delivered?

Thanks!

EDIT:

Here's the complete event that delivers the size:

private void button1_Click(object sender, EventArgs e)
        {
            button1.Visible = false;
            this.FormBorderStyle = FormBorderStyle.None;
            mid.X = this.Width / 2;
            mid.Y = this.Height / 2;
            drawerUpdateSchedule.Start();
        }

Check the accepted answer in this thread, looks like an answer to your question. Especially

private void Form1_Resize(object sender, System.EventArgs e)
{
   Control control = (Control)sender;
   //Access width and height withㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ
   control.Size.Height; 
   control.Size.Width;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM