Solved

Getting a TMemoField To A TRichEdit

Posted on 1999-01-04
10
656 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now