Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 437
  • Last Modified:

Using TMemo control, HELP!

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
skel
Asked:
skel
  • 10
  • 8
  • 4
  • +1
1 Solution
 
MatveyCommented:
Consola.Lines[Consola.Lines.Count-1] :=
   Consola.Lines[Consola.Lines.Count-1] + Letra

I think I remember it worked...
0
 
viktornetCommented:
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
 
skelAuthor Commented:
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
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.

 
viktornetCommented:
it works here.....just tried it on.... i have no idea what it could be. o O

did you try Matvey's code?????
0
 
skelAuthor Commented:
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
 
viktornetCommented:
Tried it again, to see if I've done something wrong, but no... Everything was successful....

Cheers,
Viktor
0
 
intheCommented:
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
 
skelAuthor Commented:
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
 
intheCommented:
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
 
intheCommented:
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
 
skelAuthor Commented:
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
 
intheCommented:
Hello Skel,

Anytime dude ,glad it's sorted i was running out of ideas :-)
¤¤¤¤¤¤¤InThe¤¤¤¤¤¤¤
0
 
skelAuthor Commented:
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
 
viktornetCommented:
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
 
viktornetCommented:
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
 
viktornetCommented:
btw- I hate those stars.. s*** :-||
0
 
skelAuthor Commented:
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
 
viktornetCommented:
Memo1.SelStart := Length(Memo1.Text) div 2; //would move the cursor to the middle of the containing text in the memo..

Cheers,
Viktor
0
 
viktornetCommented:
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
 
skelAuthor Commented:
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
 
viktornetCommented:
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
 
skelAuthor Commented:
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
 
viktornetCommented:
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
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.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

  • 10
  • 8
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now