Word Insert at Cursor

General TRichView support forum. Please post your questions here
Post Reply
pgkammath
Posts: 36
Joined: Fri Nov 24, 2017 6:16 am

Word Insert at Cursor

Post by pgkammath »

Hi,
I have a User Interface, where there is a SRichViewEdit and a StringGrid with a single column. When the user double clicks on an item in stringgird, the data in the column gets populated in the SRichViewEdit. The thing is , how to insert this word from stringgrid at the Cursor, How to to set the font ,color styles and, then move the cursor to next line.

I used modified \TRichView\ScaleRichView\Demos\ActionTest\ActionTest demo and built on that, I am not that familiar with other details.

I use the following code to place the text
InitialAssessment.RichViewEdit.InsertText(MainData.Cells[0,MainData.Row], False);

For my requirement I tried the following code from an earlier post. But I could not get that to work , since it was very old and the parameters are diferent now.

I know it simple. Early help is highly appreciated. Thanks in advance


This one

// returns an index of a text style having the same properties
// as the current text style, but the specified color
// (searching for an existing style; if not found, adding a new style)
function GetColoredTextStyleNo(rve: TCustomRichViewEdit; Color: TColor): Integer;
var TextStyle: TFontInfo;
begin
TextStyle := TFontInfo.Create(nil);
TextStyle.Assign(rve.Style.TextStyles[rve.CurTextStyleNo]);
TextStyle.Color := Color;
Result := rve.Style.FindTextStyle(TextStyle);
TextStyle.Free;
end;

// returns an index of a paragraph style having the same properties
// as the current paragraph style, but the specified alignment
// (searching for an existing style; if not found, adding a new style)
function GetAlignedParaNo(rve: TCustomRichViewEdit; Align: TRVAlignment): Integer;
var ParaStyle: TParaInfo;
begin
ParaStyle := TParaInfo.Create(nil);
ParaStyle.Assign(rve.Style.ParaStyles[rve.CurParaStyleNo]);
ParaStyle.Alignment := Align;
Result := rve.Style.FindParaStyle(ParaStyle);
ParaStyle.Free;
end;

// Using:
SRichViewEdit1.ActiveEditor.ApplyParaStyle(GetAlignedParaNo(SRichViewEdit1.ActiveEditor));
SRichViewEdit1.ActiveEditor.CurTextStyleNo := GetColoredTextStyleNo(SRichViewEdit1.ActiveEditor);
SRichViewEdit1.ActiveEditor.InsertText('Mario da Silva',false);
Sergey Tkachenko
Site Admin
Posts: 17569
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: Word Insert at Cursor

Post by Sergey Tkachenko »

The code looks correct.
With the obvious exception: GetAlignedParaNo/GetColoredTextStyleNo require the second parameter (Alignment/Color).

If you want to insert in the caret position, use SRichViewEdit1.ActiveEditor instead of SRichViewEdit1.RichViewEdit, like in the last example (because the caret may be not only in the main editor, but in a header, a footer, a note, or a text box).

What's exactly wrong with it?
pgkammath
Posts: 36
Joined: Fri Nov 24, 2017 6:16 am

Re: Word Insert at Cursor

Post by pgkammath »

Thanks Sergey.
Got it. Was bit lazy there. Modified the fuction GetColoredTextStyleNo got the result.

function GetColoredTextStyleNo(rve: TCustomRichViewEdit; Color: TColor;FontStyle:TFontStyles): Integer;
var TextStyle: TFontInfo;
begin
TextStyle := TFontInfo.Create(nil);
TextStyle.Assign(rve.Style.TextStyles[rve.CurTextStyleNo]);
TextStyle.Color := Color;
TextStyle.Style := FontStyle;

Result := rve.Style.FindTextStyle(TextStyle);
TextStyle.Free;
end;

function GetAlignedParaNo(rve: TCustomRichViewEdit; Align: TRVAlignment): Integer;
var ParaStyle: TParaInfo;
begin
ParaStyle := TParaInfo.Create(nil);
ParaStyle.Assign(rve.Style.ParaStyles[rve.CurParaStyleNo]);
ParaStyle.Alignment := Align;
Result := rve.Style.FindParaStyle(ParaStyle);
ParaStyle.Free;
end;


//Usage

SRichViewEdit1.ActiveEditor.ApplyParaStyle(GetAlignedParaNo(SRichViewEdit1.ActiveEditor,rvaLeft));
SRichViewEdit1.ActiveEditor.CurTextStyleNo := GetColoredTextStyleNo(SRichViewEdit1.ActiveEditor,clRed,[fsBold]);
SRichViewEdit1.ActiveEditor.InsertText(AdvStringGrid1.Cells[0,AdvStringGrid1.Row]+#13#10,false);
SRichViewEdit1.ActiveEditor.CurTextStyleNo := GetColoredTextStyleNo(SRichViewEdit1.ActiveEditor,clBlack,[]);
SRichViewEdit1.ActiveEditor.SetFocus;
Post Reply