How to keep draggable lines(rectangle) on the panel/rectangle using C##??

Hi,

I was able to draw two vertical lines on the panel and made them draggable using EE's help.
I want to make it a little beter.
Can you help??

1) I want that rectangle(line, width of 1) to move from left to right direction.
when I move it downward, I see length of line gets reduced, meaning it is moving downward. How can I stop that??

2) Two lines come across when I move them, how can I stop two lines to pass each other??

3) I want two lines to stay on the graph( panel or rectangle). Just stay inside of x axis..

dkim18Asked:
Who is Participating?
 
Babycorn-StarfishConnect With a Mentor Commented:
okay, final comment for today. Never change the Y position of the rectangle this removes the need to check whether you're above or below rect1.

Err, i just read the posts above and my last couple of posts aren't there which is wierd. Here's what i posed last:

     private void panel3_MouseMove(object sender, MouseEventArgs e)
        {
            if (mouseDown && hit)
            {
                newPoint = e.Location;
                Point diff = GetDifference(newPoint, oldPoint);
                Rectangle currentRect = rects[currIdx];
                currentRect.X += diff.X;
                currentRect.Y += diff.Y;

                switch (currIdx)
                {
                    case LEFT://left most line
                        {
                            if (currentRect.X >= rects[RIGHT].X)
                            {
                                return;
                            }
                            else if(currentRect.X <= rect1.X)
                            {
                                return;
                            }    
                            break;
                        }
                    case RIGHT://right most line
                        {
                            if (currentRect.X <= rects[LEFT].X)
                            {
                                return;
                            }
                            else if (currentRect.X >= rect1.X + rect1.Width)
                            {
                                return;
                            }
                            break;
                        }
                    default:
                        {
                            break;
                        }
                }

                if (currentRect.Y <= rect1.Y)
                {
                    return;
                }
                else if (currentRect.Y + currentRect.Height >= rect1.Y + rect1.Height)
                {
                    return;
                }

                ShowCurrectRectCoordinates(currentRect.X, currentRect.Y);
                rects[currIdx] = currentRect;
                Invalidate(true);
                Update();
                oldPoint = newPoint;
            }
        }

Just remove this bit:

          if (currentRect.Y <= rect1.Y)
                {
                    return;
                }
                else if (currentRect.Y + currentRect.Height >= rect1.Y + rect1.Height)
                {
                    return;
                }

and this bit too:

currentRect.Y += diff.Y;
0
 
Babycorn-StarfishCommented:
Hi,

post the code you have that gives the behaviour in 1, also can you give a bit more detail about 2 & 3.

Thanks
0
 
dkim18Author Commented:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DragLineTest
{
  public partial class Form1 : Form
  {
    private List<Rectangle> rects = new List<Rectangle>();
    private int currIdx = -1;
    private Point oldPoint;
    private Point newPoint;
    private Boolean hit = false;
    private Boolean mouseDown = false;

    public Form1()
    {
      InitializeComponent();
      Rectangle rect1 = new Rectangle(1, 1, 190, 100);
      Rectangle leftBar = new Rectangle(21, 1, 1, 100);
      Rectangle rightBar = new Rectangle(170, 1, 1,100);
      rects.Add(rect1);
      rects.Add(leftBar);
      rects.Add(rightBar);
   
    }
    private void panel1_Paint(object sender, PaintEventArgs e)
    {
      e.Graphics.DrawRectangle(Pens.Black,rects[0]);
      e.Graphics.FillRectangle(Brushes.Red, rects[1]);
      e.Graphics.FillRectangle(Brushes.Red, rects[2]);
   
    }
    private void panel1_MouseDown(object sender, MouseEventArgs e)
    {
      currIdx = -1;
      for (int idx = 1; idx < rects.Count; idx++)
      {
        if (rects[idx].Contains(e.Location))
        {
          if (e.Button == MouseButtons.Left)
          {
            oldPoint = e.Location;
            currIdx = idx;
            mouseDown = true;
            hit = true;
          }
          return;
        }
      }
        hit = false;
        return;
    }
    private void panel1_MouseUp(object sender, MouseEventArgs e)
    {
      mouseDown = false;
      hit = false;
      Invalidate();
      Update();
    }
    private Point GetDifference(Point end, Point start)
    {
      return new Point(end.X - start.X, end.Y - start.Y);
    }
    private void panel1_MouseMove(object sender, MouseEventArgs e)
    {
      if (mouseDown && hit)
      {      
            newPoint = e.Location;
            Point diff = GetDifference(newPoint, oldPoint);
            Rectangle currentRect = rects[currIdx];
            currentRect.X += diff.X;
            currentRect.Y += diff.Y;
            ShowCurrectRectCoordinates(currentRect.X, currentRect.Y);
            rects[currIdx] = currentRect;
            Invalidate(true);
            Update();
            oldPoint = newPoint;      
      }
    }
    private void ShowCurrectRectCoordinates(int x, int y)
    {
      this.textBox1.Text = "x: " + x + " y: " + y;
   

    }
  }
}
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
dkim18Author Commented:
2) Two lines come across when I move them, how can I stop two lines to pass each other??

say  line1 is @x=10 and line2 @x=30, I don't want to line1 to move beyond x=29 vice versa line2 to move in beyond x=11..does this make sense??
I don't want them ever to pass each other

3) I want two lines to stay on the graph( panel or rectangle). Just stay inside of x axis..
In my example I draw rect1 which will be my graph panel in my real application.
I want my two lines to be inside of that rectangle and never go way or out of that rectangle.
Thanks again.


0
 
Babycorn-StarfishCommented:
for 1)

use this code instead


 if (mouseDown && hit)
            {
                newPoint = e.Location;
                Point diff = GetDifference(newPoint, oldPoint);
                //create a copy of the rectangle under the mouse
                Rectangle curr = rects[currIdx];
                //update it to its new position
                curr.X += diff.X;
                curr.Y += diff.Y;
 
                //see whether it would intersect with any other
                for (int idx = 0; idx < rects.Count; idx++)
                {
                    if (rects[idx].IntersectsWith(curr))
                    {
                        return;
                    }
                }
                ShowCurrRectCoordinates(curr.X, curr.Y);
                rects[currIdx] = curr;
                //invalidate and repaint
                Invalidate(true);
                Update();
                oldPoint = newPoint;
            }

Open in new window

0
 
Babycorn-StarfishCommented:
sorry, that was actually for 2) :)

I don't seem to get the problem you mentioned in one.
0
 
Babycorn-StarfishCommented:
Scratch that, use this code:


public partial class Form1 : Form
    {
        private List<Rectangle> rects = new List<Rectangle>();
        private int currIdx = -1;
        private Point oldPoint;
        private Point newPoint;
        private Boolean hit = false;
        private Boolean mouseDown = false;
 
        //NEW
        private Rectangle rect1 = new Rectangle(1, 1, 190, 100);
 
        public Form1()
        {
            InitializeComponent();
            
            Rectangle leftBar = new Rectangle(21, 1, 1, 100);
            Rectangle rightBar = new Rectangle(170, 1, 1, 100); 
            rects.Add(leftBar);
            rects.Add(rightBar);
 
        }
        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            //NEW
            e.Graphics.DrawRectangle(Pens.Black, this.rect1);
            for (int idx = 0; idx < rects.Count; idx++)
            {
                e.Graphics.FillRectangle(Brushes.Red, rects[idx]);
            }  
        }
        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            //NEW
            currIdx = -1;
            for (int idx = 0; idx < rects.Count; idx++)
            {
                if (rects[idx].Contains(e.Location))
                {
                    if (e.Button == MouseButtons.Left)
                    {
                        oldPoint = e.Location;
                        currIdx = idx;
                        mouseDown = true;
                        hit = true;
                    }
                    return;
                }
            }
            hit = false;
            return;
        }
        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            mouseDown = false;
            hit = false;
            Invalidate();
            Update();
        }
        private Point GetDifference(Point end, Point start)
        {
            return new Point(end.X - start.X, end.Y - start.Y);
        }
        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            //NEW
            if (mouseDown && hit)
            {
                newPoint = e.Location;
                Point diff = GetDifference(newPoint, oldPoint);
                Rectangle currentRect = rects[currIdx];
                currentRect.X += diff.X;
                currentRect.Y += diff.Y;
 
                //see whether it would intersect with any other
                for (int idx = 0; idx < rects.Count; idx++)
                {
                    if (rects[idx].IntersectsWith(currentRect))
                    {
                        return;
                    }
                }
 
                ShowCurrectRectCoordinates(currentRect.X, currentRect.Y);
                rects[currIdx] = currentRect;
                Invalidate(true);
                Update();
                oldPoint = newPoint;
            }
        }
        private void ShowCurrectRectCoordinates(int x, int y)
        {
            this.textBox1.Text = "x: " + x + " y: " + y;
 
 
        }
    }
}

Open in new window

0
 
Babycorn-StarfishCommented:
I marked the stuff i changed as best i could. Essentially i moved the frame rectangle outside of the rects array (nothing major) and changed the mouse move method to check whether the updated rectangle would intersect with any other currently stored in rect.

To check for 3) i would suggest storing the x coordinate of the start and finish of rect1 (or its equivalent) and in the mouse move event perform and additional check to ensure that the x coordinate of current rect is greater than the left most edge of rect1 and also less than the the right most edge of rect1, basically an additional hit test.
0
 
Babycorn-StarfishCommented:
Sorry submitted too soon. You should also check that the y position is greater than the top most edge of rect1 and that the y position + the height of the line is less than the the lower edge of rect1 (rect1.Y + rect1.Height)
0
 
dkim18Author Commented:
for (int idx = 0; idx < rects.Count; idx++)
                {
                    if (rects[idx].IntersectsWith(currentRect))
                    {
                        return;
                    }
                }
 

Didn't work but I think I understand what you are trying to do.
I will put more thought on it later when I go home.

For #1, I think your last comment might solve it.
rectangle was able to move downward and you can hide the bottom part of the rectangle by dragging it downward.
Thanks much  and hopefully I can make this work and apply it on my ZedGraph.

I am working with the ZedGraph and draw several line graphs and I want to apply two vertical lines to give users to select the range of the line graph to do some calculations.
Thanks so much and I will try this further when I get home.

 
0
 
Babycorn-StarfishCommented:
oh and this too:

  private const int LEFT = 0;
        private const int RIGHT = 1;
0
 
dkim18Author Commented:
Babycorn-Starfish,

Thanks so much for all your help.
I think this is what I can use.
This part of the code didn't quite work as would like.
It did stop my lines insert but it also didn't move aferward.
I will play around with it a little more.
0
 
Babycorn-StarfishCommented:
Hi again,

i had trouble actual being able to select the lines, i don't think i was clicking on precisely where they were, if i made the a bit wider it worked fine.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.