Page 1 of 1

TChartListBox and Drag & Drop

Posted: Tue May 12, 2009 7:47 am
by 10545590
Hi !

I have a problem using TChartListBox with Drag & Drop. I use the DropMaster component from www.raize.com for Drag & Drop.

My ListBox has Multiselect and ExtendedSelect enabled. So the user can select any series he wants. Lets say we have 3 series and the user selects 2 of them. This works fine.
Now I use the following code:

Code: Select all

procedure TForm1.SourceListBoxMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var Stream  : TMemoryStream;
    i       : SmallInt;
    Ergi    : String;
begin
  if {(ssShift in Shift) and} (ssLeft in Shift)  then begin

    if Windows.DragDetect(SourceListBox.Handle, POINT(X,Y)) then begin  //  Panel2.Handle
      with DMTextSource1.CustomFormatData do begin
        // First clear the format data...
        Clear;

        Ergi := '';
        for i := 0 to SourceListBox.Items.Count - 1 do
          if SourceListBox.Selected[i] = True then
            Ergi := Ergi + IntToStr(i) + '#';
        Ergi := ergi + 'ListBoxSelectionEnd';
       // ShowMessage(ergi);

        Stream := TMemoryStream.Create;
        SaveChartToStream(SourceChart, Stream, True, False);
        Stream.Position := 0;
        addFormat(DD_Name, Ergi + StreamToString(Stream));        //   EncodeDoubleToString(aDouble)
        Stream.Free;
      end;

      DMTextSource1.Execute; // Start the OLE drag
    end;
  end;
end;
Basically this code handles the Drag & Drop function. This code works fine, too.

But the problem is clicking the ListBox. If 2 or more series are selected and the user clicks into the listbox and holds the left mouse button down (in order to start the drag & Drop operation), the selection could be resetted.

Example:
The Listbox has the following entries:
Series 1
Series 2
Series 3
Series 4

The underlined series are the selected series.

Now the user clicks on Series 3 in order to start the Drag & Drop. Now Series 2 and 4 are deselected and only Series 3 is selected.

How can we prevent this behaviour?

And some notes about the drag & Drop. The normal Drag operation of the listbox is disabled (EnableDragSeries = False).

Posted: Tue May 12, 2009 3:05 pm
by yeray
Hi Dominik,

I think that you should use an array to maintain a list of the selected series, maintain it at OnChartListBoxMouseDown event. Then you'll have the series you need in Drag&Drop or where you wish. Here is an approximation:

Code: Select all

uses series;

var selectedSeries: array of TChartSeries;

procedure TForm1.FormCreate(Sender: TObject);
var i: Integer;
begin
  for i:=0 to 3 do
  begin
    Chart1.AddSeries(TLineSeries.Create(self));
    Chart1[i].FillSampleValues(25);
    Chart1[i].Title := 'Series' + IntToStr(i);
  end;
  Chart1.View3D := false;

  setLength(selectedSeries,0);
end;

procedure TForm1.ChartListBox1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if (ssCtrl in Shift) then
  begin
    SetLength(selectedSeries,length(selectedSeries)+1);
    selectedSeries[Length(selectedSeries)-1] := ChartListBox1.SelectedSeries;
  end
  else
  begin
    SetLength(selectedSeries,1);
    selectedSeries[Length(selectedSeries)-1] := ChartListBox1.SelectedSeries;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var i: Integer;
begin
  Caption := IntToStr(length(selectedSeries));
  for i:=0 to length(selectedSeries)-1 do
  begin
    Caption := Caption + '  ' + selectedSeries[i].Title;
  end;
end;