Link to home
Start Free TrialLog in
Avatar of florisb
florisb

asked on

stringgrid code / component

hello there,

I'm using a tstringgrid and I would like to make it look exactly like the one in MS Outlook 2000, I would like the sort functionality (with the visible arrow up / down), and the right mouse pop-up on the upper columns.

I did check Torry's pages and I allready have the components of the 'dreamcompagny'. Has someone experience with a free alternative, that works perfect for this?

Thanx,
Floris.


Avatar of florisb
florisb

ASKER

Hmmm, trying to use the standard stringgrid now, some problems: points for any solutions I can use:

Shitty thing now; two pop-up's like in Outlook, no problemo, making the row active that's right-clicked in; problem:

Should I take the linewidth in account?

I'm really still hoping someone has done all this, I can increase points.

procedure TForm1.grdResultContextPopup(Sender: TObject; MousePos: TPoint;
  var Handled: Boolean);
var
  rect : TGridRect;
begin
//right clicked in upper column?
//if grdResult.Selection.
if MousePos.Y <= grdResult.DefaultRowHeight then
  grdResult.popupMenu := popupTop
else
  begin
  grdResult.popupMenu := popupGrid;
//  showMessage(inttostr(MousePos.Y));
  if not ( (((MousePos.Y)-1) div grdResult.DefaultRowHeight) > grdResult.rowCount-1) then
     grdResult.row := ((MousePos.Y)-1) div grdResult.DefaultRowHeight;
  end;
end;
ASKER CERTIFIED SOLUTION
Avatar of kretzschmar
kretzschmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of florisb

ASKER

He thanx! I made my own MouseToCell...(with a + 6 in the proc I posted it did work perfect (upper column is 6 higher than others)).

I used some code I found to made an up and down sort possible by code. I can do the things I want now (sorting goes fast!)

Code here:

procedure TResultaten.GetSort(var GenStrGrid : TStringGrid; ThatCol : Integer;omgekeerdResult : boolean);
{ Description : sort stringlist, by copying to stringlist
                (column that has to be sorted is move to beginning line, sort
                is done automatically and column is placed back)
  Pre         : rowcount has to be set correctly!
  Post        : genstrgrid sorted on Column:ThatCol
  Input       : -
  Returns     : -
  Creator     : Floris
  Date        : 5-2}
const
 TheSeparator = '@'; // Define the Separator
var
 CountItem, I, J, K, ThePosition : Integer;
 MyList : TStringList;
 MyString, TempString : String;
begin
  CountItem := GenStrGrid.RowCount; // Give the number of rows in the StringGrid
  MyList := TStringList.Create;//Create the List
  MyList.Sorted := False;
  try
    begin
    for I := 1 to (CountItem - 1) do
      begin
      MyList.Add(GenStrGrid.Rows[I].Strings[ThatCol] + TheSeparator + GenStrGrid.Rows[I].Text);
      end;
    Mylist.Sort;//Sort the List
    for K := 1 to Mylist.Count do
      begin
      MyString := MyList.Strings[(K - 1)];//Take the String of the line (K – 1)
      ThePosition := Pos(TheSeparator, MyString);//Find the position of the Separator in the String
      TempString := '';
      //element waarop gesorteerd is kan nu verwijderd worden van eerste positie
      TempString := Copy(MyString, (ThePosition + 1), Length(MyString));
      MyList.Strings[(K - 1)] := '';
      MyList.Strings[(K - 1)] := TempString;
      end;
   if not omgekeerdResult then
     begin
     for J := 1 to (CountItem - 1) do
       GenStrGrid.Rows[J].Text := MyList.Strings[(J - 1)];//Refill the StringGrid
     end
   else
     begin
     for J := 1 to (CountItem - 1) do
       GenStrGrid.Rows[J].Text := MyList.Strings[((CountItem-2)-(J - 1))];//Refill the StringGrid
     end;
  end;
 finally
  MyList.Free;//Free the List
 end;
end;


procedure TResultaten.grdResultContextPopup(Sender: TObject;
  MousePos: TPoint; var Handled: Boolean);
var
  Col,Row : integer;
begin
end;

procedure TResultaten.grdResultMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
{ Description : right and leftmouse handling, sort if column click.
  Pre         :
  Post        :
  Input       : -
  Returns     : -
  Creator     : Floris
  Date        : 3-2, update 4-2}

var
  Col,Row : integer;
begin
//right mouseclick
  if Button = mbright then
    begin
    if activeResultSet = false then //no activerestul, no pop-up at all.
      grdResult.popupMenu := nil
    else
      begin
      grdResult.MouseToCell(x,y,Col,Row); //HALELUJA! (wist niet dat deze functie bestond)
        If Row = 0 then
          grdResult.popupMenu := popupTop //show popup for upper columns
        else
          begin
          grdResult.row := row; //show normal popup and set current row here
          grdResult.popupMenu := popupGrid;
          end;
      end;
    end
//left mouseclick
  else if (not (activeResultSet = false)) and (ssLeft in Shift) then
    begin
    grdResult.MouseToCell(x,y,Col,Row); //get row
    if row = 0 then
      begin
      //bepaal of er up of down gesort wordt.

      if (col = sortedOnColumn) then
        begin
        if not omgekeerdResult then //switch after third clicks on same column
          omgekeerdResult := true
        else
          omgekeerdResult := false;
        end
      else
        omgekeerdResult := false;

      GetSort(grdResult,col, omgekeerdResult);
      grdResult.refresh; //how to refresh that move column line?
      sortedOnColumn := col;

//      if omgekeerdResult then //na twee maar klikken op zelde kolm; reset
//        omgekeerdResult := false;
      end;
    end;
end;
Avatar of florisb

ASKER

thanks for the help meikl, what do you think about the points? Perhaps with another tip from you I can just give them?

Greetings,
Floris.
what kind of tip do you need, floris ?
Avatar of florisb

ASKER

Hi Meikl

Maybe tip isn;t the correct English word.

You didn't answer my question, but gave me a good hint... ..errr something to use with tstringgrids? Or some answer on my questoin?




Avatar of florisb

ASKER

I do have two more small questions:

After my sort routine, there's a fat black line in the grid (like the one you get if you resize a column), how to I cancel this line?

I would like to have an event when a column is resized, can't find it.

Floris.
Avatar of florisb

ASKER

Okee....;-(
Avatar of florisb

ASKER

No answer to question, but mouseToCell made things much easier...