Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2007-11-20
13
Medium Priority
?
351 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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

927 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