locked
Aligning Forms Side-By-Side RRS feed

  • Question

  • Greetings,

    I'm not certain if I'm making this overly difficult or not, but here goes...

    I have three forms: frmMain, frmConfig, frmFind.  This form has 2 buttons on it: btnOpenFrmFind and btnOpenFrmConfig.

    I want to be able to click btnOpenFrmConfig, and frmConfig will load and attach itself to the right-side edge of frmMain.  Likewise, I want to be able to click btnOpenFrmFind, and frmFind will load and attach itself to the left-side edge of frmMain.

    This is how I am displaying the forms.

            private void btnFindUser_Click(object sender, EventArgs e)
            {
                Form frmFindUser = new frmFindUser();
                frmFindUser.Visible = true;
                this.btnFindUser.Enabled = false;
            }

    It may not be the best way to go about dispaying an already-created form, I admit.  I set the .Visible property to true, and then disable the button for opening that form, because if I click the button more than once it will open up another instance of the form, which is something that I don't want to happen ;-)

            private void frmConfig_Load(object sender, EventArgs e)
            {
                this.Location.X = 5;
            }

    This is the load event from one of the forms.  The 5 doesn't matter - I only put that there so that the project would build while I was looking into something else.  My problem is that doing a this.Location.X = frmMain.Location.X + frmMain.Width won't work (the plan was to take frmMain's location, add it's width, so that frmConfig would alight to the right-side edge of frmMain.

    Code completion when I type "this.Location.X = frmConfig." has nothing listed for location, size, etc (MousePosition, but that's obviously not what I'm looking for).

    What am I missing here?  Am I loading the form incorrectly?  Or is the ability to align forms side-by-side a bit more complicated than I imagined?  (I remember doing something easy like this in VB4 "back in the day", so I assumed the same methods held true today).

    

    Thursday, March 22, 2012 10:20 PM

Answers

  • I think your calculation for a location is good.  Your problem is passing that to frmConfig.

        public partial class frmConfig : Form
        {
            public frmConfig()
            {
                InitializeComponent();
            }
    
            public frmConfig(Point location)
                : this()
            {
                this.Location = location;
            }
    
            private void frmConfig_Load(object sender, EventArgs e)
            {
                // you need to add events to keep this form aligned when parent form moves
            }
        }

    Try using the overloaded constructor, or something similar.

    Hope this helps.

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    • Marked as answer by Get-SPKen Monday, March 26, 2012 8:44 PM
    Thursday, March 22, 2012 10:32 PM

All replies