Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2007-11-20
13
Medium Priority
?
342 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

730 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