Solved

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

Posted on 2007-11-20
13
340 Views
Last Modified: 2008-02-01
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..

0
Comment
Question by:dkim18
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 4
13 Comments
 
LVL 11

Expert Comment

by:Babycorn-Starfish
ID: 20323756
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
 

Author Comment

by:dkim18
ID: 20323800
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
 

Author Comment

by:dkim18
ID: 20323829
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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 11

Expert Comment

by:Babycorn-Starfish
ID: 20323937
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
 
LVL 11

Expert Comment

by:Babycorn-Starfish
ID: 20324000
sorry, that was actually for 2) :)

I don't seem to get the problem you mentioned in one.
0
 
LVL 11

Expert Comment

by:Babycorn-Starfish
ID: 20324013
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
 
LVL 11

Expert Comment

by:Babycorn-Starfish
ID: 20324043
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
 
LVL 11

Expert Comment

by:Babycorn-Starfish
ID: 20324058
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
 

Author Comment

by:dkim18
ID: 20324463
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
 
LVL 11

Accepted Solution

by:
Babycorn-Starfish earned 500 total points
ID: 20324509
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
 
LVL 11

Expert Comment

by:Babycorn-Starfish
ID: 20324513
oh and this too:

  private const int LEFT = 0;
        private const int RIGHT = 1;
0
 

Author Comment

by:dkim18
ID: 20327078
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
 
LVL 11

Expert Comment

by:Babycorn-Starfish
ID: 20327186
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

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question