C# check if object is out of bounds (GUI) -


i'm making moving rectangle in c# wfa. it's moving without problems, can go away gui need check if rectangle out of bound. i've tried it, works in left-up corner. (my window 400x400)

    using system;     using system.collections.generic;     using system.componentmodel;     using system.data;     using system.drawing;     using system.linq;     using system.text;     using system.windows.forms;      namespace object     {         public partial class form1 : form         {             enum directions { left, right, up, down }             directions _direct;             private int _x;             private int _y;              public form1()             {                 initializecomponent();                 _x = 50;                 _y = 50;                 _direct = directions.down;             }          private void form1_load(object sender, eventargs e)         {             invalidate();         }          private void form_paint(object sender, painteventargs e)         {             e.graphics.fillrectangle(brushes.black, _x, _y, 70, 70);         }          private void timer1_tick(object sender, eventargs e)         {             if (_direct == directions.left)             {                 _x -= 10;                 checkposition(_x, _y);             }             else if (_direct == directions.right)             {                 _x += 10;                 checkposition(_x, _y);             }             else if (_direct == directions.down)             {                 _y += 10;                 checkposition(_x, _y);             }             else if (_direct == directions.up)             {                 _y -= 10;                 checkposition(_x, _y);             }              invalidate();             checkposition(_x, _y);         }          private void form1_keydown(object sender, keyeventargs e)         {             if (e.keycode == keys.left)             {                 _direct = directions.left;             }             else if (e.keycode == keys.right)             {                 _direct = directions.right;             }             else if (e.keycode == keys.up)             {                 _direct = directions.up;             }             else if (e.keycode == keys.down)             {                 _direct = directions.down;             }         }          private void checkposition(int x, int y) {             if (x < 0) {                 _x = 0;             }             else if (y < 0) {                 _y = 0;             }             else if ((x + 70) > 400) { _x = 330; }             else if ((y+70)>400) {_y=330;}         }     } } 

your rectangle contained within client rectangle if intersection between rectangle , client rectangle rectangle. use check:

// rectangle rectangle bool outofbounds=!(rectangle.intersect(clientrectangle, rectangle).equals(rectangle)) 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -