Solved

Getting a TMemoField To A TRichEdit

Posted on 1999-01-04
10
662 Views
Last Modified: 2012-06-21
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.
0
Comment
Question by:tecmaster
  • 5
  • 3
  • 2
10 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1354585
hi tecmaster,

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

Connectings

Table->DataSource->DBCtlGrid
DBMemo->TableField

meikl
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1354586
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1354587
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Comment

by:tecmaster
ID: 1354588
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1354589
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
 
LVL 8

Expert Comment

by:ZifNab
ID: 1354590
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1354591
hi tecmaster,

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

meikl
0
 

Author Comment

by:tecmaster
ID: 1354592
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
 
LVL 27

Accepted Solution

by:
kretzschmar earned 100 total points
ID: 1354593
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
 

Author Comment

by:tecmaster
ID: 1354594
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question