Page 1 of 1

Moving Cursor with StringGrid Entry...

Posted: Thu Feb 01, 2007 11:49 am
by 9242408
Ok, this should be neat.

I basically want to move my cursor to the point with which an entry of a number in a cell has occurred. I have all the events in place.

Do I set the YValue of the cursor to move or the XValue? Id assume the XValue but how do I calculate the position. And of course the value entered may not be the exact number available in the series, so how does that play out to get the nearest point?


Thanks,

Tom

Posted: Thu Feb 01, 2007 12:01 pm
by narcis
Hi Tom,

You may be interested in using Locate method as shown here:

Code: Select all

procedure TForm1.Button1Click(Sender: TObject);
var Index: Integer;
begin
  Series1.YValues[5]:=10;

  Index:=Series1.YValues.Locate(Series1.YValues[5]);

  ChartTool1.YValue:=Series1.YValues[Index];
  ChartTool1.XValue:=Series1.XValues[Index];
end;

Posted: Thu Feb 01, 2007 12:37 pm
by 9242408
Narcis,

Works as expected. The only thing is the numbers are somewhat long, even 1000ths of a place after the decimal. Trying to locate a number has to be exact, as far as I can tell.

Any ideas on how to locate the approximate number rather than the exact?


Thanks.

Tom

Posted: Thu Feb 01, 2007 1:04 pm
by narcis
Hi Tom,

Then the only way I can think of is doing something like this:

Code: Select all

procedure TForm1.Button1Click(Sender: TObject);
var
  i, SeriesIndex: Integer;
  Val, ApproxVal, Diff, tmpDiff: Double;

begin
  Val:=500;
  ApproxVal:=499.5;
  Series1.YValues[5]:=Val;
  Diff:=Series1.MaxYValue;
  SeriesIndex:=-1;

  for i:=0 to Series1.Count do
  begin
    tmpDiff := Series1.YValue[i] - ApproxVal;
    if Abs(tmpDiff) < Diff then
    begin
      Diff:=tmpDiff;
      SeriesIndex:=i;
    end;
  end;

  ChartTool1.YValue:=Series1.YValues[SeriesIndex];
  ChartTool1.XValue:=Series1.XValues[SeriesIndex];
end;