Recent

Author Topic: Problem: How to mark two strings in a SynEdit at pressing a button?  (Read 7049 times)

carlejt

  • New Member
  • *
  • Posts: 39
Hello

In a SynEdit, how to mark (with a red frame) two string at the same time (after pressing a button). The first string on the line 1 between columns 2 and 5, and the second string between on the line 2 between columns 3 and 6.

Note: My application only consists of a form1 with a button1 and synedit1, the solution to this problem would be helpful.

Thanks

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11036
  • Debugger - SynEdit - and more
    • wiki
Re: Problem: How to mark two strings in a SynEdit at pressing a button?
« Reply #1 on: July 08, 2012, 09:13:07 pm »
Same as here: http://forum.lazarus.freepascal.org/index.php/topic,17415.msg95975.html#msg95975

Just create 2 markup modules (each with it's own blockselection)...


carlejt

  • New Member
  • *
  • Posts: 39
Re: Problem: How to mark two strings in a SynEdit at pressing a button?
« Reply #2 on: July 09, 2012, 12:33:07 am »
Hello Martin fr.

is similar, but this time in a SynEdit, I want to mark (with a red frame) two string at the same time (after pressing a button).

Note: The first string on the line 1 between columns 2 and 5, and the second string between on the line 2 between columns 3 and 6.
My application only consists of a form1 with a button1 and synedit1, the solution to this problem would be helpful.

I could not solve this problem.

Thanks
« Last Edit: July 09, 2012, 12:56:10 am by carlejt »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11036
  • Debugger - SynEdit - and more
    • wiki
Re: Problem: How to mark two strings in a SynEdit at pressing a button?
« Reply #3 on: July 09, 2012, 01:42:35 am »
Well I understand, what you want to do.

I gave you information, how to put independent red frames around 2, 3 or more parts of the text.

If the information is not clear to you, then you need to give me more info what you do not understand. Or send me the code you wrote, and that fails.

I wrote the code in the other thread, for 1 reasons:
- you read and understand it, and therefore learn how to do it
(You may also copy it as it is, but that wasn't why I wrote it)

If you have understood it, then you should be able to copy, paste and adapt that code for as many highlights as you want.

If you are in doubt of what the code does, then I will help you. Tell me what you did understand, and ask specific questions.
Also if you want more specific help, you need to tell, what your experience level with pascal, and OO-fameworks is.

--
Let me give you another hint:
- One pair (of instances) of TSynEditSelection and TSynEditMarkupSelection gives you one red frame.
- Two pairs of it, give 2 red frames.
- 3 pairs ....
« Last Edit: July 09, 2012, 01:46:08 am by Martin_fr »

carlejt

  • New Member
  • *
  • Posts: 39
Re: Problem: How to mark two strings in a SynEdit at pressing a button?
« Reply #4 on: July 09, 2012, 03:48:20 pm »
Hi Martin fr.

I resolved the problem, and it was thanks to you ...

Code: [Select]
unit news;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  SynEdit, SynEditMarkupSelection, SynEditTypes, SynEditMarkup,
  SynEditPointClasses;

type

  { TForm1 }


  TSynEdit1 = class(TSynEdit)
  private
    FMyBlock: TSynEditSelection;
    fMyMarkup: TSynEditMarkupSelection;
    FMyBlock_: TSynEditSelection;
    fMyMarkup_: TSynEditMarkupSelection;
  public
    constructor Create(AOwner: TComponent); override;
  end;

  TForm1 = class(TForm)
    Button1: TButton;

    SynEdit1: TSynEdit1;

  procedure Button1Click(Sender: TObject);
  procedure FormCreate(Sender: TObject);
  procedure SynEdit1StatusChange(Sender: TObject; Changes: TSynStatusChanges);

  private
    { private declarations }

  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{ TForm1 }


constructor TSynEdit1.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);

  FMyBlock := TSynEditSelection.Create(ViewedTextBuffer, FAlse);
  FMyBlock.InvalidateLinesMethod := @InvalidateLines;
  fMyMarkup := TSynEditMarkupSelection.Create(self, FMyBlock);
  TSynEditMarkupManager(MarkupMgr).AddMarkUp(fMyMarkup);

  FMyBlock_:= TSynEditSelection.Create(ViewedTextBuffer, FAlse);
  FMyBlock_.InvalidateLinesMethod := @InvalidateLines;
  fMyMarkup_ := TSynEditMarkupSelection.Create(self, FMyBlock_);
  TSynEditMarkupManager(MarkupMgr).AddMarkUp(fMyMarkup_);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  SynEdit1:=TSynEdit1.Create(self);
  SynEdit1.Parent:= self;

  SynEdit1.Left:=100;
  SynEdit1.Top:=0;

  with SynEdit1 do begin
    SynEdit1.text := 'ouaueuioeiuueiaoeuouoeauoeua' + LineEnding    +'ouaueuioeiuueiaoeuouoeauoeua' + LineEnding    +'ouaueuioeiuueiaoeuouoeauoeua' + LineEnding    ;

    fMyMarkup.Enabled := true;
    fMyMarkup.MarkupInfo.FrameColor := clRed;
    fMyMarkup.MarkupInfo.FrameEdges := sfeAround;
    fMyMarkup.MarkupInfo.FrameStyle := slsSolid;

    fMyMarkup_.Enabled := true;
    fMyMarkup_.MarkupInfo.FrameColor := clRed;
    fMyMarkup_.MarkupInfo.FrameEdges := sfeAround;
    fMyMarkup_.MarkupInfo.FrameStyle := slsSolid;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  with synedit1 do begin
  FMyBlock.StartLineBytePos := Point(2,1);
  FMyBlock.EndLineBytePos := Point(5,1);
  FMyBlock_.StartLineBytePos := Point(3,2);
  FMyBlock_.EndLineBytePos := Point(6,2);
  end;
end;

procedure TForm1.SynEdit1StatusChange(Sender: TObject;
  Changes: TSynStatusChanges);
begin
  if (synedit1.CaretX=2)and(synedit1.CaretY=2) then showmessage('hello');
end;

{$R *.lfm}

end.


However, there is something I have not been resolved:

Why SynEdit1StatusChange event does not work?

procedure TForm1.SynEdit1StatusChange(Sender: TObject;
  Changes: TSynStatusChanges);
begin
  if (synedit1.CaretX=2)and(synedit1.CaretY=3) then showmessage('hello');
end;


I only need this to complete.

Thanks

« Last Edit: July 09, 2012, 03:51:53 pm by carlejt »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11036
  • Debugger - SynEdit - and more
    • wiki
Re: Problem: How to mark two strings in a SynEdit at pressing a button?
« Reply #5 on: July 09, 2012, 05:31:16 pm »
Have you assigned the event to the SynEdit?

Normally, you do that in the ObjectInspector, whign lists all the events.


But if you create SynEdit in code, then you must do that in code to

Code: [Select]
  SynEdit1.OnStatusChange := @SynEdit1StatusChange;

The "@" is not needed in mode delphi. But default is fpc, and then you need it.

You can read up on method pointers and events....

The property SynEdit1.OnStatusChange  just points to a field (variable) that can contain the pointer to a method (must be a method of correct type / correct arguments)

If it isn't set, then it is nil. And then SynEdit has nothing to call.

The name of that method is not important at all...

carlejt

  • New Member
  • *
  • Posts: 39
Re: Problem: How to mark two strings in a SynEdit at pressing a button?
« Reply #6 on: July 09, 2012, 05:48:54 pm »
Hello Martin fr.

I put  SynEdit1.OnStatusChange := @SynEdit1StatusChange; into TForm1.FormCreate(Sender: TObject);  and work very good...

Martin fr. Thank you very much for all the help.

Now I can continue and finish my project... it was thanks to you.

Thank you very much.

 

TinyPortal © 2005-2018