Getting a TMemoField To A TRichEdit

I have Paradox table with a memo field called 'Results'.  I didn't realize until recently that Delphi considers this a TMemoField.  So I've been trying to find some way to get my 'Results' data from the Paradox memo field into my TRichEdit component named 'Editor'.  However, I have one slight twist to add to my dilema.  I want to display 3 sets 'Results' records to give a 3 column look.

My initial thought was to copy the text in each record to 3 temporary TMemos called p1, p2, and p3.  I would then be able to extract each line from p1, p2, and p3 and add them to a string.  Then take the string and add it to the 'Editor' via Editor.Lines.Add().

What I need is to either:
A) get the code to get the my text from the Paradox memo field to a TMemo component so that I can get the lines of text and go about what I started to do.

B) get the code that does everything, ie. gets the text from the Paradox memo field to the 'Editor' displayed in 3 columns.

I will gladly give upto 400 points for option B.  I will accept option A or B.
tecmasterAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
kretzschmarConnect With a Mentor Commented:
Hi Tecmaster,

here is my testunit, with creating TMemo at RunTime

unit memotest_u;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Db, ExtCtrls, DBCtrls, DBTables, ComCtrls;

type
  TForm1 = class(TForm)
    RichEdit1: TRichEdit;
    Table1: TTable;
    DBNavigator1: TDBNavigator;
    DataSource1: TDataSource;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

Var p1, p2, p3 : TMemo;


procedure TForm1.FormCreate(Sender: TObject);
begin
  { Create TMemo on the Fly and set some Parameters }
  p1 := TMemo.Create(Form1);
  p1.Parent := Form1;
  p1.Visible := False;
  p1.WordWrap := False;
  p1.ScrollBars := ssBoth;
  { Next }
  p2 := TMemo.Create(Form1);
  p2.Parent := Form1;
  p2.Visible := False;
  p2.WordWrap := False;
  p2.ScrollBars := ssBoth;
  { Next }
  p3 := TMemo.Create(Form1);
  p3.Parent := Form1;
  p3.Visible := False;
  p3.WordWrap := False;
  p3.ScrollBars := ssBoth;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  { Release TMemos }
  p1.Free;
  p2.Free;
  p3.Free;
end;

Function Filler(ExpS : String; Len : Integer) : String;
{ Fills a string to len with SpaceChars,
  Strings greater len will be ignored }
var s : String;
begin
  s := ExpS;
  while length(S) < Len do s := s + ' ';
  result := s;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  i : Integer;
  MaxLineCount : Integer;
  RReads : Integer;
begin
  MaxLineCount := 0;
  RReads := 0;
  { Get the MemoFields in the TMemo-Components }
  for i := 1 to 3 do
  begin
    if not(table1.EOF) then
    begin
      case i of
        1 : begin
              P1.Clear;
              P1.text := table1.Fieldbyname('Memo1').AsString;
              MaxLineCount := P1.Lines.Count;
            end;
        2 : begin
              P2.Clear;
              P2.text := table1.Fieldbyname('Memo1').AsString;
              if MaxLineCount < P2.Lines.Count then MaxLineCount := P2.Lines.Count;
            end;
        3 : begin
              P3.Clear;
              P3.text := table1.Fieldbyname('Memo1').AsString;
              if MaxLineCount < P3.Lines.Count then MaxLineCount := P3.Lines.Count;
            end;
      end;
      Inc(RReads);
      table1.next;
    end;
  end;
  { Make a Table in RichEdit (Tab-separated,
    Filled to a constant length, Font Courier New set in DesignMode)}
  richedit1.Clear;
  for i := 0 to maxlinecount - 1 do
    richedit1.Lines.Add(Filler(P1.lines.Strings[i],30) + #9 +
    Filler(P2.lines.Strings[i],30) + #9 +
    Filler(P3.lines.Strings[i],30));
  {Go Back to the Current Record}
  for i := 0 to RReads do Table1.Prior;
end;

end.

maybe you must a little bit experiment with the TMemos about Wordwrap and/or width and the FillLen (My TestMemos LineLen was < 30 Chars)

Meikl
0
 
kretzschmarCommented:
hi tecmaster,

why do not use a DBCtlGrid-Component, placing a DBMemo-Component in it?

Connectings

Table->DataSource->DBCtlGrid
DBMemo->TableField

meikl
0
 
ZifNabCommented:
Hi tecmaster,

this is A :

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Db, DBTables, ComCtrls, Grids, DBGrids, DBCtrls;

type
  TForm1 = class(TForm)
    DataSource1: TDataSource;
    Table1: TTable;
    Memo1: TMemo;
    procedure Table1AfterScroll(DataSet: TDataSet);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Table1AfterScroll(DataSet: TDataSet);
begin
 Memo1.Lines.Text := table1.FieldbyName('Notes').asstring;
end;

end.

For B, I look on that when I find some time.

Regards, Zif.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
kretzschmarCommented:
hi tecmaster,

a method

procedure TForm1.Button1Click(Sender: TObject);
var
  i : Integer;
  MaxLineCount : Integer;
begin
  MaxLineCount := 0;
  { Get the MemoFields in the TMemo-Components }
  for i := 1 to 3 do
  begin
    if not(table1.EOF) then
    begin
      case i of
        1 : begin
              Memo1.Clear;
              Memo1.text := table1.Fieldbyname('Memo').AsString;
              MaxLineCount := Memo1.Lines.Count;
            end;
        2 : begin
              Memo2.Clear;
              Memo2.text := table1.Fieldbyname('Memo').AsString;
              if MaxLineCount < Memo2.Lines.Count then MaxLineCount := Memo2.Lines.Count;
            end;
        3 : begin
              Memo3.Clear;
              Memo3.text := table1.Fieldbyname('Memo').AsString;
              if MaxLineCount < Memo3.Lines.Count then MaxLineCount := Memo3.Lines.Count;
            end;
      end;
      table1.next;
    end;
  end;
  { Make a Table in RichEdit (Tab-separated)}
  richedit1.Clear;
  for i := 0 to maxlinecount - 1 do
    richedit1.Lines.Add(memo1.lines.Strings[i] + #9 +
                        memo2.lines.Strings[i] + #9 +
                        memo3.lines.Strings[i]);


end;

maybe you must fill the strings to a constantlength, the layout depends on the font you use.

meikl


0
 
tecmasterAuthor Commented:
I tried using the

taResults.FieldByName('MatchResults').AsString;

as suggested, however I got an EAccessViolation error, when it attempted to assign the text.

I also tried doing a p1.Clear, p1.Lines.Clear, and p1.Lines.Text, but I got the EAccessViolation error in all cases.  Evidentally something is not getting declared or set correctly in order for me to use p1, p2, and p3 (see code below).

Here is the code I'm using.

var p1, p2, p3: TMemo;

  taResults.First;
  while not taResults.eof do begin
    case taResultsPeriodId.Value of
      0: p1.Text := taResults.FieldByName('MatchResults').AsString;
      1: p2.Text := taResults.FieldByName('MatchResults').AsString;
      2: p3.Text := taResults.FieldByName('MatchResults').AsString;
    end; { case }
    taResults.Next;
  end; { not taResults.EOF }

0
 
kretzschmarCommented:
hi tecmaster,

you have defined your tmemos as var bur not created.

add in your code

p1.create(self);
p2.create(self);
p3.create(self);

dont forget to free it after use

or drop in your form three TMemos components with visible = False and name it to your names

meikl


0
 
ZifNabCommented:
are you sure that field 'matchresults' is of type text, or string? What type is it?

wait a minute :

have you placed these three memo-fields on your form during design-time? Other wise you've to create them at runtime!! and destroy them after you've used them.

p1 := TMemo.Create ... //Create

.

p1.Free                //Destroy
0
 
kretzschmarCommented:
hi tecmaster,

zif is right about creation-statement. i am a bit confused today.

meikl
0
 
tecmasterAuthor Commented:
kretzchmar, please post your comment pertaining to adding the 3 TMemos to the current form and I will grade.  I never did get the other solutions to work.  Kept getting a similar error.  It seems as though more needs to be set when creating a component from scratch rather than WYSIWYG.

Thanks for your help.
tecmaster
0
 
tecmasterAuthor Commented:
Ah I see what I was missing before.  Thanks for your assistance.  I didn't want to add the TMemos to the form because they are not used for anything except for one specific task.  I could not see taking up additional resources just to use a TMemo.

Again, thanks.

tecmaster
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.