Click here to Skip to main content
15,883,948 members
Articles / Desktop Programming / Windows Forms
Tip/Trick

Dynamically positioning a form/dialog, whilst also keeping it all on one monitor/screen

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
25 Aug 2015CPOL 5.7K   3  
Constraining a form/dialog to a single monitor

Introduction

Sometimes you want a little more "smarts" when it comes to dynamically placing a form or dialog box, so that it doesn't position some of the form to the right or below the physical monitor, and it doesn't span monitors (in a multi-monitor setup), in much the same way as a ContextMenuStrip.

Background

I wrote this primarily for the "right-click shows dialog box" function, but it probably has other uses too. I have multiple monitors, and I just don't like seeing a dialog box split across 2 monitors.

Using the code

I want to right-click on the background in a group box. Visual Studio doesn't expose the event for this, so I had to add it into my main form's load.

C#
        private void frmMain_Load(object sender, EventArgs e)
        {
            //this can go anywhere in the form load
            this.groupBoxTest.Click +=
                new EventHandler(this.groupBoxTest_Click);

Next is the code to handle the click event. 

C#
        private void groupBoxTest_Click(object sender, EventArgs e)
        //Note that VisualStudio doesn't generate the click event code for a group box.
        //The hook is at the end of frmMain_Load.
        {
            GroupBox me = (GroupBox)sender;

            //The EventHandler builds an EventArgs as the second parameter,
            //but ideally I'd like a MouseEventArgs, so I simply cast e to em.
            MouseEventArgs em = (MouseEventArgs)e;

            if (em.Button == MouseButtons.Right)
            {
                frmSettings newFrmSettings = new frmSettings(this);
                MoveFormToRelativeLocation(me, new Point(em.X, em.Y), newFrmSettings);

                //Any other changes to the dialog box can go here
                //(setting text or whatever)

                newFrmSettings.ShowDialog();
            }

        }

Finally comes the location manager stuff. I actually put this in my ScreenHandler class, but I've presented it here without that reference.

C#
        private void MoveFormToRelativeLocation(Control sender, Point pointInSender, Form form)
        {
            form.StartPosition = FormStartPosition.Manual;

            Point formTL = sender.PointToScreen(pointInSender);

            Screen myScreen = Screen.FromPoint(formTL);

            Point ScreenBR = new Point(
                myScreen.WorkingArea.Left + myScreen.WorkingArea.Width,
                myScreen.WorkingArea.Top + myScreen.WorkingArea.Height);

            if (formTL.X + form.Width > ScreenBR.X) formTL.X = ScreenBR.X - form.Width;
            if (formTL.Y + form.Height > ScreenBR.Y) formTL.Y = ScreenBR.Y - form.Height;
            if (formTL.X < 0) formTL.X = 0;
            if (formTL.Y < 0) formTL.Y = 0;

            form.Location = formTL;
        }

 

History

V1 20150826 082600

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO Noble Software Systems Ltd
United Kingdom United Kingdom
I've been programming in various languages since 1980. I started in Basic at college and moved swiftly to Z80 assembler. After that I was an operator on a Burroughs mini for a time, then after a little Burroughs COBOL I moved to UNIVAC COBOL for many years. (During which time I became an IT contractor). During that time I touched UNIVAC assembler a bit, as well as the usual SSG etc. I did a bit of RPG, though I never really liked that. After COBOL I went to Gupta SQLWindows for a few years (which I still maintain was a good OO language). Then a move to Oracle PL/SQL and a few years as senior programmer for a large American company, also some years in the DBA team at then end of my time there. Somewhere in the middle of that I spent a year working in PIC assembler and C on a Palm PDA. After Oracle I moved to the other end of things in an IT sense, and discovered FPGAs and VHDL programming. Oh my, that's a brain-bender coming from computer programming. Anyhow, I am also interfacing FPGAs to a PC, and that's where C# comes in.

Comments and Discussions

 
-- There are no messages in this forum --