0

suppose i have have two sdi form and when apps run then a sdi form show and from there i am showing another sdi form but the problem is i can drga any where the second sdi form which i do not want. in case of MDI form the mdi child form can not be drag out of mdi form boundary.so in my case i want to simulate the same thing. i want no other sdi form can not be drag out form my main sdi form's boundary. so just guide me how to do this.

i could guess that i have to work with form drag event and from there i have to check form top and left but need more suggestion.

private void Form1_Move(object sender, EventArgs e)
{
    this.Location = defaultLocation;
}

i try to do it this way but it is not working.

public partial class Form2 : Form
    {
        Form1 _parent = null;
        public Form2()
        {
            InitializeComponent();
        }

        public Form2(Form1 parent)
        {
            InitializeComponent();
            _parent = parent;
        }

        private void Form2_Move(object sender, EventArgs e)
        {
            if((this.Location.X+this.Width) > _parent.Width)
            {
                this.Location = new System.Drawing.Point(_parent.ClientRectangle.Width-this.Width,this.Location.Y);
            }

            if ((this.Location.Y + this.Height) > _parent.Height)
            {
                this.Location = new System.Drawing.Point(_parent.ClientRectangle.Height - this.Height, this.Location.X);
            }

            if (this.Location.Y < 0)
            {
                this.Location = new System.Drawing.Point(this.Location.X);
            }

            if (this.Location.X < 0)
            {
                this.Location = new System.Drawing.Point(this.Location.Y);
            }
        }
    }

please guide me where i made the mistake. thanks

UPDATE

private void Form2_Move(object sender, EventArgs e)
        {
            int left = this.Left;
            int top = this.Top;

            if (this.Left < _parent.Left)
            {
                left = _parent.Left;
            }
            if (this.Right > _parent.Right)
            {
                left = _parent.Right - this.Width;
            }
            if (this.Top < _parent.Top)
            {
                top = _parent.Top;
            }
            if (this.Bottom > _parent.Bottom)
            {
                top = _parent.Bottom - this.Height;
            }

            this.Location = new Point(left, top);
        }
Thomas
  • 33,544
  • 126
  • 357
  • 626
  • Sorry, you are trying to do it the hard way. If this is an exercise I understand but if not you are trying to recreate the MDI model and that's not practical. Just use an MDI parent and open child windows (you can setup an MDI frame to be pretty much the same (UI wise) as any other window. Hope it helps. – ProgrammerV5 Feb 10 '14 at 10:16
  • This seems like a bad idea. You want MDI behavior, but you don't use MDI. This is going to be difficult and create smelly code. – DonBoitnott Feb 10 '14 at 12:08
  • http://stackoverflow.com/questions/12001758/preventing-moving-of-a-control-out-of-its-container?rq=1 – Thomas Feb 10 '14 at 13:33
  • my issue solved and i update my answer with right code. – Thomas Feb 11 '14 at 07:06

1 Answers1

0

I recomend to you follow the @ProgrammerV5 recomendation. But if you realy need to control the form movement, please see the use of Cursor.Clip property

here are some information: http://www.codeproject.com/Tips/375046/The-Cursor-Clip-Property

Also you may need to do a MouseCapture.

mnieto
  • 3,744
  • 4
  • 21
  • 37
  • i am talking about cursor. rather i want my one of sdi form can not be move out of my main sdi form. – Thomas Feb 10 '14 at 11:20