🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
2 messages
2 total messages Started by Sky Mon, 07 Apr 2003 15:38
How to get assuring value of SelStart
#17140
Author: Sky
Date: Mon, 07 Apr 2003 15:38
29 lines
1959 bytes
Hi all, I have a strange question,
the following function has been used.
void __fastcall TMainForm::Input_Symbol(AnsiString szSymbol)
{
    if (szSymbol.IsEmpty()) return;
    if (!ActiveControl->ClassNameIs("TEdit")) return;

    TEdit *edit = dynamic_cast<TEdit *>(ActiveControl);
    if (edit != NULL)
    {
        int t = edit->SelStart;
        AnsiString szText = edit->Text;
        szText.Insert(szSymbol, t + 1);
        edit->Text = szText;
        edit->SelStart = t + szSymbol.Length();
    }
}
Input_Symbol() has been used in my two project,
but one is correct and another is incorrect.
it always insert the symbol in front of cursor.
(some multi-byte characters has been used in TEdit)
the multi-byte character has been treated as one character in one project,
but in another project it has been treated as two character.
Does anyone can tell me what happened. How can i insert the appointed
symbol into cursor if there is multi-byte characters and standard character
farraginous.
Any hint?
TIA
Sky.
Re: How to get assuring value of SelStart
#17150
Author: Remy Lebeau (TeamB)
Date: Mon, 07 Apr 2003 17:27
35 lines
2100 bytes
"Sky" <akeehuang@163.net> wrote in message
news:3e919b72$1@newsgroups.borland.com...

>     if (!ActiveControl->ClassNameIs("TEdit")) return;
>
>     TEdit *edit = dynamic_cast<TEdit *>(ActiveControl);

Since you're doing the dynamic_cast anyway, then the call to ClassNameIs()
is redundant and aonly adds extra runtime overhead.  Do this instead:

    void __fastcall TMainForm::Input_Symbol(AnsiString szSymbol)
    {
        TEdit *edit = dynamic_cast<TEdit *>(ActiveControl);
        if( !edit ) return;

>         int t = edit->SelStart;
>         AnsiString szText = edit->Text;
>         szText.Insert(szSymbol, t + 1);
>         edit->Text = szText;
>         edit->SelStart = t + szSymbol.Length();

You could use just the SelStart, SelLength, and SelText properties to
simplify that, then you don't need the temporary AnsiString at all:

    int t = edit->SelStart;
    edit->SelLength = 0;
    edit->SelText = szSymbol;
    edit->SelStart = t + szSymbol.Length();

When SelLength is 0, SelText inserts the new text into the edit control at
the position where SelStart is.


Gambit
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads