Solved

Using TMemo control, HELP!

Posted on 1998-10-25
23
397 Views
Last Modified: 2010-08-05
Hi again!

I have problems adding characters one by one to a TMemo control. For example, I've got the following code:

Letra := 'A';
Consola.Text := Consola.Text + Letra;

where Consola is the TMemo Control... but it didn't work.... I tried with Consola.Text := 'WORD', and it didn't work either..... I set MaxLength property to a value greater than 0, but it continues not working...

How can I do it?
Thanks in advance,
Jaime
0
Comment
Question by:skel
  • 10
  • 8
  • 4
  • +1
23 Comments
 
LVL 3

Expert Comment

by:Matvey
ID: 1344350
Consola.Lines[Consola.Lines.Count-1] :=
   Consola.Lines[Consola.Lines.Count-1] + Letra

I think I remember it worked...
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1344351
Hello Jaime

I just created a new app dropped the TMemo and a TButton and here is the code I got....

In OnClick() of the button

procedure TForm1.Button1Click(Sender: TObject);
var
  Letter : char;
begin
  Letter := 'A';
  Memo1.Text := Memo1.Text + Letter;
end;

The code works perfectly....

Are you sure you haven't set the property to Read-Only ????

Cheers,
Viktor
0
 

Author Comment

by:skel
ID: 1344352
Hi again....

no.. the property isn't set to Read-Only..... and when I debug the code... the watch of Consola.Text shows the correct text... but it isn't displayed in the Memo. That Memo is in other Form called ConsolaES... so I'm using ConsolaES.Consola.Text := ConsolaES.Consola.Text + Letra.... and it doesn't display the character when they are being introduced :(

Any other suggestion?
Jaime
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1344353
it works here.....just tried it on.... i have no idea what it could be. o O

did you try Matvey's code?????
0
 

Author Comment

by:skel
ID: 1344354
Last News :

The Memo control works when I place it on the main form and set the Text property from there...... if I cut and paste that Memo into other form and set the Text property from the main form as before,  it stops working. The text isn't displayed even if the form is shown (modeless)... Why can this be?

Thanks
Jaime
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1344355
Tried it again, to see if I've done something wrong, but no... Everything was successful....

Cheers,
Viktor
0
 
LVL 17

Accepted Solution

by:
inthe earned 50 total points
ID: 1344356
The following code works perfectly ,when you click button 1
and form2(consolaES)shows move it so you can select form 1 again and click button 2  and watch form2 text update from hell to hello.  

procedure TForm1.Button1Click(Sender: TObject);
begin
  ConsolaES.show;
  ConsolaES.Consola.text:='hell';
end;

procedure TForm1.Button2Click(Sender: TObject);
var Letra:char;
begin
Letra:='o';
ConsolaES.Consola.text:=form2.Consola.text+Letra;
end;

Cheers ¤¤¤¤¤¤¤InThe¤¤¤¤¤¤¤
0
 

Author Comment

by:skel
ID: 1344357
To test... I've started a new Application.... I've created 2 Forms.... the main form has 2 buttons... one to open a new form, and the other to write a letter to the Memo control.

This is the main form:

****************************************************************
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Unit2;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
  Form2.Consola.Text := Form2.Consola.Text + 'A';
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  with TForm2.Create(Application) do
    Show
end;

end.

****************************************************************

and this is the form containing the Memo control:

****************************************************************
unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm2 = class(TForm)
    Consola: TMemo;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.DFM}

end.

****************************************************************

This doesn't work.... do you see the error? When I started designing the new application, I placed the Memo Control on the main form (TForm1) and it worked... so.. the problem is related to placing the control on other form.

Thanks
Jaime
0
 
LVL 17

Expert Comment

by:inthe
ID: 1344358
The only thing i have different is unit2 is not in the main uses declaration mine is added straight after  implementation
like so:

implementation
uses unit2;

would you like me to post both unit1 & unit2 of mine cause they work fine??

cheers ¤¤¤¤¤¤¤InThe¤¤¤¤¤¤¤
0
 
LVL 17

Expert Comment

by:inthe
ID: 1344359
Here it is anyway it works so hope it helps you out :-)

unit Unit1;
interface
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,StdCtrls, Buttons;
type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
  public
 end;

var
  Form1: TForm1;
implementation
uses Unit2;
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
 begin
  form2.show;
  form2.memo1.text:='hell';
 end;

procedure TForm1.Button2Click(Sender: TObject);
var a:char;
 begin
  a:='o';
  form2.memo1.text:=form2.memo1.text+a;
 end;
end.

      And unit 2 is as follows

unit Unit2;
interface
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,StdCtrls;

type
 TForm2 = class(TForm)
   Memo1: TMemo;
  private
 public
end;

var
  Form2: TForm2;
 implementation
 {$R *.DFM}
end.
   ¤¤¤¤¤¤¤InThe¤¤¤¤¤¤¤

0
 

Author Comment

by:skel
ID: 1344360
I've found finally the problem!!!

I had:
with TForm2.Create(Application) do
  Show

and you had only: form2.show..... I used your way to show the form, and it worked, thanks inthe


0
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.

 
LVL 17

Expert Comment

by:inthe
ID: 1344361
Hello Skel,

Anytime dude ,glad it's sorted i was running out of ideas :-)
¤¤¤¤¤¤¤InThe¤¤¤¤¤¤¤
0
 

Author Comment

by:skel
ID: 1344362
I take this opportunity to ask you what is the correct way to show a form? as you saw.. I used Create method before showing it which was incorrect in this case.... if I want to display an aboutbox... can I use only the show method? I use always Create before Show (as I saw in an demo)

Thanks
Jaime
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1344363
Hello Jaime!

If it's your own Form and not created in the IDE then you need to create your form and then show it... In your case you created your form in the IDE and you don't have to create it again, just use Form2.Show; here is wn example if you created it dynamically at run-time and not in the IDE

var
  MyForm : TForm;
begin
  with MyForm.Create(self) do
  try
    ShowModal;
  finally
    Free;
 end;
end;

This is a simple example that frees the memoery of the form right after the form is closed..

if you need more info and have other questions just ask...

btw- That's the exact thing I did, but don'r know you tried doing it that way... I thought you use Form2.Show; That's why I was runnin out of ideas as Barry(Inthe) said ;->

Cheers,
Viktor
end;
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1344364
Oh, just remember that if you create your own form at run-time(dynamically) then you need to free the memory you've taken up by that form :-)

Cheers,
Viktor
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1344365
btw- I hate those stars.. s*** :-||
0
 

Author Comment

by:skel
ID: 1344366
Well.. I understood.. thanks.. now continuing with the title of the question... how can I move the cursor inside the memo control when characters are added to the control?

Thanks
Jaime
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1344367
Memo1.SelStart := Length(Memo1.Text) div 2; //would move the cursor to the middle of the containing text in the memo..

Cheers,
Viktor
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1344368
Oh one more thing... If you add the chars by a button you need to use this...

//add the char
Memo1.SetFocus;
Memo1.SelStart := Length(Memo1.Text) div 2;

btw- Any other questions you got??

Regards,
Viktor Ivanov
0
 

Author Comment

by:skel
ID: 1344369
Viktornet..

I've tried       Entrada.SelStart := Entrada.SelStart + 1;    where Entrada is the TMemo control....  it didn't work... the cursor only moved to the second char and it didn't continue moving.... I want that conforming I'm pressing keys, after the key is introduced in the control using Entrada.Text := Entrada.Text + Key in the KeyPress event, the cursor must be moved one character to the right and when I press the Enter key, the cursor must make a line feed and a carriage return. How can I do it?

Thanks in advance
Jaime
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1344370
Let me see the code you are using and the event where you are using and tell me exactly what you want... Here is a thought... try putting this in OnKeyPress()

if key = #13 then
  Entrada.SelStart := Entrada.SelStart - 1;

Is this what you want?? If not please supply the code I asked you above..

Cheers,
Viktor

0
 

Author Comment

by:skel
ID: 1344371
Viktornet:

Here's the code for OnKeyPress event:

procedure TConsola.EntradaKeyPress(Sender: TObject; var Key: Char);
begin
  if Conectado and not Entrada.ReadOnly then
    begin
      Entrada.Text := Entrada.Text + Key;
      Entrada.SelStart := Entrada.SelStart + 1;
      Emulador.Serial.Write(Key,1);
      Entrada.ReadOnly := True;
    end;
end;

where Entrada is the TMemo object, Conectado is a boolean variable which is TRUE if a circuit attached to the serial port is powered on... Serial is a serial port control and Write is the method to send 1 character over the serial link.

I want the following....

I have the TMemo control, in which, each character I type, is displayed in the control and sent to the serial port. When that character is Enter (#13), it has to be sent too and the cursor must perform a carriage return followed by a line feed (similar to a simple text editor).

Any suggestion?
Thanks

Jaime
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1344372
Ok, here is how the text is suppose to look like...

procedure TConsola.EntradaKeyPress(Sender: TObject; var Key: Char);
     begin
       if key = #13 then begin
          Entrada.Text := Entrada.Text + #13; //If this one doesn't work just say so...
            //Send your info whereever you want to,...
       if Conectado and not Entrada.ReadOnly then
         begin
           Entrada.Text := Entrada.Text + Key;
//           Entrada.SelStart := Entrada.SelStart + 1;
You might want to change the previous line to the following...
        Entrada.SelStart ;= Length(Entrada.Text);//since you and a char to the end of the text...
            Emulador.Serial.Write(Key,1);
            Entrada.ReadOnly := True;
         end;
     end;

Tell me how it goes...

Cheers,
Viktor
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 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

10 Experts available now in Live!

Get 1:1 Help Now