Page 1 of 1

Question about gant chart, dragging bars

Posted: Fri Mar 17, 2006 2:35 pm
by 9640091
Hi,
I need to connect some bars in my gant chart and need to move them together. And also i want to prevent bars from overlapping. Is it possible? and also can connecting lines be used for this problem. if so, how?
thanks,

Bora.

Posted: Wed Mar 22, 2006 10:26 am
by narcis
Hi Bora,

To drag gantt bars as you requested you can use something as the code below. Regarding overlapping, what do you exactly want to achieve? For example, you could check if the bars dragged values fall into any other bar range in the same row and then perform the action you wish.

Code: Select all

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using Steema.TeeChart;

namespace GanttDrag
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
      gantt1.FillSampleValues();
    }

    private bool allowDrag = true;
    private int bar=-1;
		private Cursor cursorDrag=Cursors.Hand;
		private double xOriginal;
    private ArrayList barList = new ArrayList();

    private new void MouseMove(MouseEventArgs e,ref Cursor c) 
		{
			if (bar==-1) 
			{
				if (allowDrag && (bar==-1) && (gantt1.Clicked(e.X,e.Y)!=-1)) 
				{
					c=cursorDrag;
          tChart1.Chart.CancelMouse=true;
				}
			}
			else  // Dragging bar...
			{
        double Change = gantt1.GetHorizAxis.CalcPosPoint(e.X) - xOriginal;


				// drag bar
        gantt1.StartValues.Value[bar] += Change;
				gantt1.EndValues.Value[bar]+=Change;

        //Drag all bars in barList according to how bar is dragged
        for (int i = 0; i < barList.Count; ++i)
        {
          gantt1.StartValues.Value[Convert.ToInt16(barList[i])] += Change;
          gantt1.EndValues.Value[Convert.ToInt16(barList[i])] += Change;
				}

        xOriginal = gantt1.GetHorizAxis.CalcPosPoint(e.X);

        gantt1.Invalidate();
			}
		}

		private new void MouseDown(MouseEventArgs e,ref Cursor c) 
		{
			bar=-1;
			xOriginal=tChart1.Axes.Bottom.CalcPosPoint(e.X);

			// Check bar under mouse (to drag)
			if ((bar==-1) && allowDrag) 
			{
        bar = gantt1.Clicked(e.X, e.Y);
        if (bar != -1)
        {
          //Search for previous and next bars          
          SearchBars(bar, bar, barList, 1);          
        }
			}
		}

    private void SearchBars(int InitBar, int CurrBar, ArrayList barList, int mode)
    {
      //Look for next bars (mode==1)
      if ((gantt1.NextTasks[CurrBar] != -1.0) && (mode == 1))
      {
        barList.Add(gantt1.NextTasks[CurrBar]);
        SearchBars(InitBar, (int)Math.Round(gantt1.NextTasks[CurrBar]), barList, 1);
      }
      else
      {

        //Look for previous bars (mode==0)
        if ((gantt1.NextTasks[CurrBar] == -1.0) && (mode == 1))
        {
          SearchBars(InitBar, InitBar, barList, 0);
        }

        if (mode==0)
        {
          
          for (int i = 0; i < gantt1.Count; ++i)
          {
            if (gantt1.NextTasks[i]==CurrBar)
            {
              barList.Add(i);
              SearchBars(InitBar, i, barList, 0); 
            }            
          }

        }

      }
    }

    private void tChart1_MouseDown(object sender, MouseEventArgs e)
    {
      MouseDown(e, ref cursorDrag);
    }

    private void tChart1_MouseMove(object sender, MouseEventArgs e)
    {
      MouseMove(e, ref cursorDrag);
    }

    private void tChart1_MouseUp(object sender, MouseEventArgs e)
    {
      bar = -1;
      barList.Clear();
    }
	}  
}