What makes a Winform position initially stale?

Posted by msorens on Stack Overflow See other posts from Stack Overflow or by msorens
Published on 2009-07-14T17:14:37Z Indexed on 2010/04/30 23:37 UTC
Read the original article Hit count: 220

The code below demonstrates a very simple problem; I am hoping that I am just missing a setting that someone might be able to reveal.

Goal

(1) Launch main winform (MainForm).
(2) Press button to display secondary winform (ShadowForm) that is semi-transparent and should exactly overlay MainForm.

What Actually Happens

Scenario 1: Launch main winform then press button: ShadowForm displays with correct size but incorrect location, lower and to the right (as if it was cascaded). Press button to close ShadowForm again. Press button once more to reopen ShadowForm and now it is in the correct position, covering MainForm.

Scenario 2: Launch main winform, move it around, then press button: ShadowForm displays with correct size but incorrect location (where the MainForm was before moving it). Press button to close; press again to reopen and now ShadowForm is in the correct position.

using System;
using System.Windows.Forms;

namespace LocationTest
{
    static class Program
    {
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
    }

    public class MainForm : Form
    {
        ShadowForm shadowForm = new ShadowForm();
        Button button1 = new Button();
        System.ComponentModel.IContainer components = null;

        public MainForm()
        {
            this.SuspendLayout();
            this.button1.Location = new System.Drawing.Point(102, 44);
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.Text = "button1";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.Add(this.button1);
            this.ResumeLayout(false);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (shadowForm.Visible) { shadowForm.Hide(); }
            else
            {
                shadowForm.Size = Size; // this always works
                shadowForm.Location = Location; // this fails first time, but works second time!
                shadowForm.Show();
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null)) { components.Dispose(); }
            base.Dispose(disposing);
        }
    }

    public class ShadowForm : Form
    {
        private System.ComponentModel.IContainer components = null;

        public ShadowForm()
        {
            this.SuspendLayout();
            this.BackColor = System.Drawing.Color.Black;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Opacity = 0.5;
            this.Click += new System.EventHandler(this.ShadowForm_Click);
            this.ResumeLayout(false);
        }

        private void ShadowForm_Click(object sender, EventArgs e)
        {
            Hide();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null)) { components.Dispose(); }
            base.Dispose(disposing);
        }
    }
}

© Stack Overflow or respective owner

Related posts about winforms

Related posts about overlay