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 |
![]() |
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 |
![]() |
> {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 |
![]() |
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 |
![]() |
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 |
![]() |