Recent

Author Topic: Delphi To Pascal  (Read 5738 times)

Deepaak

  • Sr. Member
  • ****
  • Posts: 454
Delphi To Pascal
« on: October 24, 2017, 12:15:09 pm »
Working in Delphi but not in Laz.
Plz. Covert it.
CopyRect is not working.

Code: Pascal  [Select][+][-]
  1. procedure TfrmMain.ComboBox1Change(Sender: TObject);
  2. begin
  3.   with StringGrid1 do
  4.       Cells[Col, Row] := ComboBox1.Text;
  5. end;
  6.  
  7. procedure TfrmMain.FormCreate(Sender: TObject);
  8. begin
  9.  StringGrid1.DefaultRowHeight := ComboBox1.Height;
  10.  StringGrid1.DefaultColWidth :=ComboBox1.Width;
  11. end;
  12.  
  13. procedure TfrmMain.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
  14. var
  15.    R: TRect;
  16. begin
  17.   if (ACol >= StringGrid1.FixedCols) and
  18.      (ARow >= StringGrid1.FixedRows) and
  19.      (gdFocused in State) then
  20.      with ComboBox1 do
  21.      begin
  22.        BringToFront;
  23.        CopyRect(R,Rect);
  24.        R.TopLeft :=     frmMain.ScreenToClient(
  25.                         StringGrid1.ClientToScreen(R.TopLeft));
  26.        R.BottomRight := frmMain.ScreenToClient(
  27.                         StringGrid1.ClientToScreen(R.BottomRight));
  28.        SetBounds(R.Left, R.Top, R.Right-R.Left, R.Bottom-R.Top);
  29.      end;
  30. end;
  31.  
  32. procedure TfrmMain.StringGrid1TopLeftChanged(Sender: TObject);
  33. var
  34. R: TRect;
  35. begin
  36.       with StringGrid1 do
  37.         CopyRect(R, CellRect(Col, Row));
  38.  
  39.     with ComboBox1 do
  40.     begin
  41.       Visible := False;
  42.       R.TopLeft :=     frmMain.ScreenToClient(
  43.                        StringGrid1.ClientToScreen(R.TopLeft));
  44.       R.BottomRight := frmMain.ScreenToClient(
  45.                        StringGrid1.ClientToScreen(R.BottomRight));
  46.       SetBounds(R.Left, R.Top, R.Right - R.Left, R.Bottom - R.Top);
  47.     end;
  48.  
  49.     with StringGrid1 do
  50.       if (TopRow <= Row) and (TopRow + VisibleRowCount > Row) then
  51.          ComboBox1.Show;
  52. end;
« Last Edit: October 24, 2017, 12:35:09 pm by Deepaak »
Holiday season is online now. :-)

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: Delphi To Pascal
« Reply #1 on: October 24, 2017, 12:30:55 pm »
Could you give us a hint as to what is not working?

tr_escape

  • Sr. Member
  • ****
  • Posts: 432
  • sector name toys | respect to spectre
    • Github:
Re: Delphi To Pascal
« Reply #2 on: October 24, 2017, 12:48:42 pm »
As I understand you trying to when you click on the a cell of grid you want to show a combobox on the cell isn't you?


Deepaak

  • Sr. Member
  • ****
  • Posts: 454
Re: Delphi To Pascal
« Reply #3 on: October 24, 2017, 12:52:40 pm »
u Got It Right.....
Holiday season is online now. :-)

tr_escape

  • Sr. Member
  • ****
  • Posts: 432
  • sector name toys | respect to spectre
    • Github:
Re: Delphi To Pascal
« Reply #4 on: October 24, 2017, 12:54:59 pm »
Did you try to StringGrid1PrepareCanvas instead of DrawCell?

Also you using col and row varaible as global (I think) change to like as : g_col , g_row :Integer;


« Last Edit: October 24, 2017, 01:19:49 pm by tr_escape »

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: Delphi To Pascal
« Reply #5 on: October 24, 2017, 01:24:34 pm »
Also you using col and row varaible as global I think change to like as : g_col , g_row :Integer;
No, the Col and Row are surrounded by "with StringGrid1 do" so they are properties of StringGrid1.
Ah, you already corrected that :)

I still haven't heard what's actually the issue. It works fine here.

Besides that the definition of
Code: Pascal  [Select][+][-]
  1. procedure TfrmMain.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
is different when you create the event in Lazarus (Rect is aRect and State is aState).

Did you just copy in the source and hoped it worked or did you really connect the procedures StringGrid1DrawCell() etc to the event-handlers in the object inspector ??

« Last Edit: October 24, 2017, 01:28:37 pm by rvk »

Deepaak

  • Sr. Member
  • ****
  • Posts: 454
Re: Delphi To Pascal
« Reply #6 on: October 24, 2017, 01:50:52 pm »
What about CopyRect()
Holiday season is online now. :-)

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: Delphi To Pascal
« Reply #7 on: October 24, 2017, 01:56:42 pm »
What about CopyRect()
Code: Pascal  [Select][+][-]
  1. uses Windows;

Deepaak

  • Sr. Member
  • ****
  • Posts: 454
Re: Delphi To Pascal
« Reply #8 on: October 24, 2017, 02:40:42 pm »
Thanks Its Working, I was missing Windows unit.

I want to Fix ComboBox1 to a particular Cell.
What changes i have to do.
Holiday season is online now. :-)

Deepaak

  • Sr. Member
  • ****
  • Posts: 454
Re: Delphi To Pascal
« Reply #9 on: October 24, 2017, 02:42:40 pm »
if Edit1 in place of ComboBox1 than also OK.

But Should be Fixed to a particular Cell.
Holiday season is online now. :-)

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: Delphi To Pascal
« Reply #10 on: October 24, 2017, 02:43:50 pm »
Thanks Its Working, I was missing Windows unit.

I want to Fix ComboBox1 to a particular Cell.
What changes i have to do.
Something like this?
http://forum.lazarus.freepascal.org/index.php/topic,10357.msg51768.html#msg51768

Set goEditing in the option of the StringGrid and add that code.
When you double click a cell to edit it you will get a combobox.

(no need for any other code)
« Last Edit: October 24, 2017, 02:47:08 pm by rvk »

tr_escape

  • Sr. Member
  • ****
  • Posts: 432
  • sector name toys | respect to spectre
    • Github:
Re: Delphi To Pascal
« Reply #11 on: October 24, 2017, 02:50:02 pm »
Try this code but not excellent of course:


Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs
  9.  
  10.   ,LCLIntf
  11.   ,LCLType, Grids, StdCtrls
  12.  
  13.   , Types;
  14.  
  15. type
  16.  
  17.   { TForm1 }
  18.  
  19.   TForm1 = class(TForm)
  20.     ComboBox1: TComboBox;
  21.     StringGrid1: TStringGrid;
  22.     procedure ComboBox1Change(Sender: TObject);
  23.     procedure ComboBox1Exit(Sender: TObject);
  24.     procedure FormCreate(Sender: TObject);
  25.     procedure StringGrid1SelectCell(Sender: TObject; aCol, aRow: Integer;
  26.       var CanSelect: Boolean);
  27.     procedure StringGrid1TopLeftChanged(Sender: TObject);
  28.   private
  29.  
  30.   public
  31.  
  32.   end;
  33.  
  34. var
  35.   Form1: TForm1;
  36.  
  37. implementation
  38.  
  39. {$R *.lfm}
  40.  
  41. { TForm1 }
  42.  
  43. procedure TForm1.StringGrid1SelectCell(Sender: TObject; aCol, aRow: Integer;
  44.   var CanSelect: Boolean);
  45. var
  46.   R: TRect;
  47. begin
  48.   if (ACol > 0) and (ARow <> 0) then
  49.   begin
  50.     R := StringGrid1.CellRect(ACol, ARow);
  51.     R.Left := R.Left + StringGrid1.Left;
  52.     R.Right := R.Right + StringGrid1.Left;
  53.     R.Top := R.Top + StringGrid1.Top;
  54.     R.Bottom := R.Bottom + StringGrid1.Top;
  55.     with Combobox1 do
  56.     begin
  57.       Left := R.Left + 1;
  58.       Top := R.Top + 1;
  59.       Width := (R.Right + 1) - R.Left;
  60.       Height := (R.Bottom + 1) - R.Top;
  61.       Visible := True;
  62.       Text := 'Select an item';
  63.       //SetFocus;
  64.     end;
  65.   end;
  66.   CanSelect := True;
  67. end;
  68.  
  69. procedure TForm1.StringGrid1TopLeftChanged(Sender: TObject);
  70. begin
  71.   ComboBox1.Visible := False;
  72. end;
  73.  
  74. procedure TForm1.FormCreate(Sender: TObject);
  75. begin
  76.   with Combobox1 do
  77.   begin
  78.     StringGrid1.DefaultRowHeight := Height;
  79.     Visible := False;
  80.     Items.Add('Item1');
  81.     Items.Add('Item2');
  82.     Items.Add('Item3');
  83.     Items.Add('Item4');
  84.     Text := 'Select an item';
  85.   end;
  86. end;
  87.  
  88. procedure TForm1.ComboBox1Change(Sender: TObject);
  89. begin
  90.   with StringGrid1 do
  91.     Cells[Col, Row] :=ComboBox1.Items[ComboBox1.ItemIndex];
  92.   ComboBox1.Visible := False;
  93.   StringGrid1.SetFocus;
  94. end;
  95.  
  96. procedure TForm1.ComboBox1Exit(Sender: TObject);
  97. begin
  98.   ComboBox1Change(nil);
  99. end;
  100.  
  101. end.
  102.  

Deepaak

  • Sr. Member
  • ****
  • Posts: 454
Re: Delphi To Pascal
« Reply #12 on: October 24, 2017, 03:14:06 pm »
Error in ur Code.

Error : Out of Bound.

Plz check it.
Holiday season is online now. :-)

tr_escape

  • Sr. Member
  • ****
  • Posts: 432
  • sector name toys | respect to spectre
    • Github:
Re: Delphi To Pascal
« Reply #13 on: October 24, 2017, 03:19:21 pm »
Error in ur Code.

Error : Out of Bound.

Plz check it.

My grid is 10*50 sized maybe yours is different. In my computer it is working.

My form:
Code: Pascal  [Select][+][-]
  1. object Form1: TForm1
  2.   Left = 279
  3.   Height = 470
  4.   Top = 118
  5.   Width = 704
  6.   Caption = 'Form1'
  7.   ClientHeight = 470
  8.   ClientWidth = 704
  9.   OnCreate = FormCreate
  10.   LCLVersion = '1.9.0.0'
  11.   object StringGrid1: TStringGrid
  12.     Left = 0
  13.     Height = 470
  14.     Top = 0
  15.     Width = 704
  16.     Align = alClient
  17.     ColCount = 10
  18.     DefaultColWidth = 100
  19.     RowCount = 50
  20.     TabOrder = 0
  21.     OnSelectCell = StringGrid1SelectCell
  22.     OnTopLeftChanged = StringGrid1TopLeftChanged
  23.   end
  24.   object ComboBox1: TComboBox
  25.     Left = 68
  26.     Height = 23
  27.     Top = 60
  28.     Width = 100
  29.     ItemHeight = 15
  30.     OnChange = ComboBox1Change
  31.     TabOrder = 1
  32.   end
  33. end


Deepaak

  • Sr. Member
  • ****
  • Posts: 454
Re: Delphi To Pascal
« Reply #14 on: October 24, 2017, 03:35:11 pm »
Worked!!!!!!!!!!!!

Thanks...................

But i Want ComboBox1 to be Fixed to a Particular Cell.
Not Moveable.

For Eg, Col1, Row1, ---------------- Fixed ComboBox. It should not move on to other Cells.
Holiday season is online now. :-)

 

TinyPortal © 2005-2018