TEdit. How to get number of chars after or before cursor

How to get number of chars after or before cursor in Edit component using SelStart. Can be this function modify:
{code}
void __fastcall TForm1::GetBeforeAfterChars( TEdit* Edit, String& BeforeCh, String& AfterCh )
{
    String txt = Edit->Text;
    int sel = Edit->SelStart;

   BeforeCh = txt.SubString( sel, sel ? 1 : 0 );
   AfterCh = txt.SubString( 1 + sel, 1 );
}{code}
0
duf
6/22/2012 8:23:52 AM
embarcadero.cppbuilder.using 1848 articles. 1 followers. Follow

4 Replies
617 Views

Similar Articles

[PageSpeed] 56

Hi duf

duf _ wrote:

> How to get number of chars after or before cursor in Edit component using SelStart. Can be this function modify:
> {code}
> void __fastcall TForm1::GetBeforeAfterChars( TEdit* Edit, String& BeforeCh, String& AfterCh )
> {
>     String txt = Edit->Text;
>     int sel = Edit->SelStart;
> 
>    BeforeCh = txt.SubString( sel, sel ? 1 : 0 );
>    AfterCh = txt.SubString( 1 + sel, 1 );
> }{code}


Maybe:

{code}
void __fastcall GetBeforeAfterChars( TEdit* Edit, String& BeforeCh, String& AfterCh )
{
   int sel = Edit->SelStart;
   BeforeCh = Edit->Text.SubString( 1, sel );
   AfterCh = Edit->Text.SubString( sel+1, Edit->Text.Length() - sel );
}
{code}

Best regards
Asger-P
http://Asger-P.dk/software
QLaunch, INI-Edit and Color Pick Pro.
0
Asger
6/22/2012 8:44:48 AM
> {quote:title=Asger Joergensen wrote:}{quote}
> Hi duf
> 
> duf _ wrote:
> 
> > How to get number of chars after or before cursor in Edit component using SelStart. Can be this function modify:
> > {code}
> > void __fastcall TForm1::GetBeforeAfterChars( TEdit* Edit, String& BeforeCh, String& AfterCh )
> > {
> >     String txt = Edit->Text;
> >     int sel = Edit->SelStart;
> > 
> >    BeforeCh = txt.SubString( sel, sel ? 1 : 0 );
> >    AfterCh = txt.SubString( 1 + sel, 1 );
> > }{code}
> 
> 
> Maybe:
> 
> {code}
> void __fastcall GetBeforeAfterChars( TEdit* Edit, String& BeforeCh, String& AfterCh )
> {
>    int sel = Edit->SelStart;
>    BeforeCh = Edit->Text.SubString( 1, sel );
>    AfterCh = Edit->Text.SubString( sel+1, Edit->Text.Length() - sel );
> }
> {code}
> 
> Best regards
> Asger-P
> http://Asger-P.dk/software
> QLaunch, INI-Edit and Color Pick Pro.

Thank You ;-)
0
duf
6/22/2012 8:59:39 AM
Asger wrote:

> int sel = Edit->SelStart;
> BeforeCh = Edit->Text.SubString( 1, sel );
> AfterCh = Edit->Text.SubString( sel+1, Edit->Text.Length() - sel );

Retreiving the value of the Text property multiple times like that is not 
efficient, especially if the text is long.  You should retreive it only once, 
eg:

{code:cpp}
void __fastcall TForm1::GetBeforeAfterChars( TEdit* Edit, String& BeforeCh, 
String& AfterCh )
{
    String txt = Edit->Text;
    int sel = Edit->SelStart;
    BeforeCh = txt.SubString( 1, sel );
    AfterCh = txt.SubString( sel+1, MaxInt );
}
{code}

Another way is to let the TEdit retreive the two substrings for you:

{code:cpp}
void __fastcall TForm1::GetBeforeAfterChars( TEdit* Edit, String& BeforeCh, 
String& AfterCh )
{
    int OldSelStart = Edit->SelStart;
    int OldSelLen = Edit->SelLength;
    try
    {
        Edit->SelStart = 0;
        Edit->SelLength = OldSelStart;
        BeforeCh = Edit->SelText;

        Edit->SelStart = OldSelStart;
        Edit->SelLength = Edit->GetTextLen() - OldSelStart;
        AfterCh = Edit->SelText;
    }
    __finally
    {
        Edit->SelStart = OldSelStart;
        Edit->SelLength = OldSelLen;
    }
}
{code}

--
Remy Lebeau (TeamB)
0
Remy
6/23/2012 5:13:13 AM
Hi Remy

Remy Lebeau (TeamB) wrote:

> Asger wrote:
> Retreiving the value of the Text property multiple times like that is not 
> efficient, especially if the text is long.  You should retreive it only once, 
> eg:
> 
> {code:cpp}
> void __fastcall TForm1::GetBeforeAfterChars( TEdit* Edit, String& BeforeCh, 
> String& AfterCh )
> {
>     String txt = Edit->Text;
>     int sel = Edit->SelStart;
>     BeforeCh = txt.SubString( 1, sel );
>     AfterCh = txt.SubString( sel+1, MaxInt );
> }
> {code}

You are right of course, your suggestion is twice as fast as mine, in
10000 runs on an edit containing 160 characters, you can save 20 milliseconds.

> Another way is to let the TEdit retreive the two substrings for you:
> 
> {code:cpp}
> void __fastcall TForm1::GetBeforeAfterChars( TEdit* Edit, String& BeforeCh, 
> String& AfterCh )
> {
>     int OldSelStart = Edit->SelStart;
>     int OldSelLen = Edit->SelLength;
>     try
>     {
>         Edit->SelStart = 0;
>         Edit->SelLength = OldSelStart;
>         BeforeCh = Edit->SelText;
> 
>         Edit->SelStart = OldSelStart;
>         Edit->SelLength = Edit->GetTextLen() - OldSelStart;
>         AfterCh = Edit->SelText;
>     }
>     __finally
>     {
>         Edit->SelStart = OldSelStart;
>         Edit->SelLength = OldSelLen;
>     }
> }
> {code}

This on the other hand is a very bad solution, it makes the edit flicker
and it is 297 times slower then your other suggestion.

Best regards
Asger-P
http://Asger-P.dk/software
QLaunch, INI-Edit and Color Pick Pro.
0
Asger
6/23/2012 9:02:19 AM
Reply: