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
674 Views

Similar Articles

[PageSpeed] 55

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:

Similar Artilces:

Reset Page count, use distinctcount() for page number, or SPROC cursor to reset page number????????
I have a report that will come back as 20 pages.What the user wants is on the beginning of another unique ID, which causes a new page to be started, to have the page count restart to 1.So, if I have 3 UniqueIDs for all 20 pages, and the first, IDAA, is 10 pages long, the second, IDBB, is 4 pages long, and the third, IDCC, is 6 pages long, then the page numbering would be as:1-10 for IDAA1-4 for IDBB1-6 for IDCCI've been told to set a cursor in a SP that determines what page I'm on and then passses this to the result set as a column.  This is then set in a group to control...

How to get RANDOM numbers in a cursor?
I have the following stored procedure: drop procedure test; create procedure test as declare @test2 numeric(8) Declare test_cursor cursor for SELECT test2 FROM testy for UPDATE of test2 open test_cursor fetch test_cursor into @test2 while (@@sqlstatus = 0) begin update testy set test2 = ( select round( (rand(6) * 10000000),0) ) where test2 = @test2 fetch test_cursor into @test2 end; Why does the rand end up always putting the same value into the column in the entire table? How do I make it randomize the value and not repeat??????? Thank you i...

How to get port number used?
Currently using qpsmtpd 0.32 in forkserver mode. I would like to know if it is possible for a plugin to determine which port a connection came in on, so that it could be treated differently from connections on other ports. Thank you. On 14-Jul-07, at 9:37 PM, sub-qpsm-tpd@rope.net wrote: > Currently using qpsmtpd 0.32 in forkserver mode. > > I would like to know if it is possible for a plugin to determine which > port a connection came in on, so that it could be treated differently > from connections on other ports. $qp->connection->local_port tells you...

TEdit how to get cursor position?
How to find cursor position so that it was moving along with the appearing signs? I would like to use this function SendMessage( Edit1->Handle, EM_SETSEL, pos, Edit1->Text.Length()); but I have a problem with the variable 'pos'. because the position of the cursor can also change with the mouse and then I do not know what value to assign it. You can retrieve Edit1 character start position with {code} Edit1->SelStart; // or DWORD Pos; Edit1->Perform(EM_GETSEL, reinterpret_cast<unsigned>(&Pos), 0); {code} ...

Trying to get away from using a cursor
Hi, I have a situation in which I need to update one field in one table from concatenated data from multiple rows on a secondary table and I know I can do it with a cursor but I'm trying to fugure out if I can do it without using a cursor. Here's the basic scenario Atttendees table attendee_id int attendee_name varchar(80) tickets varchar(255) amount money Sample Data 1 Joe null null 2 Smith null null attendee_tickets table attendee_id int ticket_name varchar(16) amount money Sample Data 1 adult 10...

Get the row number when using gridview
Hi guys, I am using a gridview presently and have a checkbox in each row. If I select this and then delete the row, on page reload I want the next checkbox in line to be selected. This is not necessarily the first row in the gridview.What I need to know is how can I determine the row number of my grdivew where the checkbox is ticked. The workflow will be something like this. 'Get selected row number 'Set as a session variable'Delete row'On page reload go to the sessionvariable value - 1 rowEnable the checkbox for that given row.Any ideas?THanks in advancep.Smy ...

To Use Cursor or Not to Use, What is the answer ??
"Falling back on cursors because they remind you of datasets will result in poor performance " Hi, The above quote was from an article which advice to use store procedure and avoid T-sql in the body of a webpage. Anyway, To me, I always use cursor when I want to process one row at a time. Is there an alternative to cursor in MS sql server to have similar results.? thanks AlNew to Asp.net One approach I've seen is to instead use a #TEMP table, create a loop, and process the TOP 1 record in the table, deleting the record at the bottom of the loop. Like this: C...

Getting the most common used numbers from access database
Hey, Today has been a bummer because i am usually faily confident with sql statements but i just can't seem to figure out the format. Basically i want data to be drawn from the same database and same table. Although i want to find out the 6 most common numbers  in all 6 columns in my table. I've tried the count(*) method but it only seems to work on one column, not all of them and i can only serach for 1 number at the mo, not all the numbers.  Does anybody out there know how to count  the numbers which come from all 6 columns(fields) and then display only the 6 highest amoun...

Get Column Number Using Column Name
This ones burning me because, I am sure i've done it before and It's simply slipping my mind. I need to be able to get a column number using the column name. i.e. Column name = 'effective_date'. Using effective date - get the column number that belongs to effective date. Any help is appreciated!! Thanks!! William dw.Describe("effective_date.ID") Good Luck Michael Walker Cascadia Software William_Shiers wrote: > This ones burning me because, I am sure i've done it before and It's simply > slipping my mind. > ...

Which to use to get results of number of rows in a query?
kind of confused on which command to use like sqldatareader or sqldataadaptor, etc to get query.. i understand datareader is faster? but it does not return me the results of number of rows it query right? Unless i do a while loop and query thru the whole thing? Or what should i use to simply just get the total number of records from a sql query? ::Or what should i use to simply just get the total number of records from a sql query? Read the data. This is the same thing other solutions do. SQL Servers nver know the exact number of rows of a query until they are finished. Did th...

Getting error message using error number
This probably is a silly question - Can anybody tell me how to trap an error message that is returned from the database inside a stored procedure? I am able to trap the error using the global var @@error, but dont seem to find any such thing for the text of the error message. I also am able to get the text of the corresponding error number from master..sysmessages, but then, the placeholders are empty. Any help is appreciated Thanks Arviind Rajan wrote: > > This probably is a silly question - > Can anybody tell me how to trap an error message that is returned from ...

get number of rows in a table and use it as a variable: help
hello, I need to get the number of rows in a mySQL table and use the resulting number as a variable to set the MAX range value ofDim id As Int32Dim rand As New Random() id = rand.Next(1, 10).ToString() to get the rows i use Dim strQueryROWS As String = "select count(*) from uploads"  how do i return the result of this as an int i can then use? thanks for any help-luke"with every fix comes a new bug" Try this:  // 1. Instantiate a new command SqlCommand cmd = new SqlCommand("select count(*) from Categories", conn);  // 2. Call E...

How to get the Sequence number without using temp table
Here is my problem: I have a table with following columns: PersonID    FirstName    LastName 102                  John             Ben 103                  Josh              Parker 104                  Mark            Ben Now if I type SELECT * FROM Person WHERE LastName = 'Ben' these two records will be displayed PersonI...

using cursor to get a row count for all user tables...
Please help me troubleshoot the below code. I'm trying to convert it over from MS SQL Server to Sybase. The MS SQL Server code is very similar to Sybase ASE, but different in places. I'm hoping that the below code is correct and will work on ASE, but was hoping to get your feedback. Is there a better way to do this? ------------------------------------------------ declare table_cursor cursor for select name from sysobjects where type='U' for read only declare @table_name sysname set @total_recs = 0 open table_cursor fetch table_curso...

Web resources about - TEdit. How to get number of chars after or before cursor - embarcadero.cppbuilder.using

Number - Wikipedia, the free encyclopedia
A number is a mathematical object used to count , label, and measure . In mathematics , the definition of number has been extended over the years ...

Number - Wikipedia, the free encyclopedia
A number is a mathematical object used to count , label, and measure . In mathematics , the definition of number has been extended over the years ...

Closing the Gap: Five numbers that should shame Australia
Indigenous Australians continue to die younger, be out of work more and have less education than other Australians, the annual Closing the Gap ...

Double take: black Maserati with Perth City Council number plates turns heads
A black Maserati featuring the number plates and crest of the Perth City Council does have a direct connection to the City of Perth, WAtoday ...

Double take: black Maserati with Perth City Council number plates turns heads
... driver did a double take on Tuesday when he saw a black four-door Maserati saloon pull into a Perth carpark - with Perth City Council number ...

NDIS ACT rollout improves but numbers underestimated: quarterly report
The rollout of the National Disability Insurance Scheme in the ACT is on track with complaints slowing down, but the number of Canberrans deemed ...

Double take: black Maserati with Perth City Council number plates turns heads
... driver did a double take on Tuesday when he saw a black four-door Maserati saloon pull into a Perth carpark - with Perth City Council number ...

Latest U.S. smartphone market numbers show Apple in the lead, but Samsung is catching up
According to the latest U.S. smartphone market share numbers from Parks Associates, Apple is still well in the lead compared to competing manufacturers, ...

Samsung is catching up to the iPhone, by the numbers
... of the smartphone market, but its competitors are starting to catch up. Looking at the latest United States smartphone market share numbers, ...

Madeleine Albright says you might think she knows better than "to tell a large number of women to go ...
"But last Saturday, in the excitement of a campaign event for Hillary Clinton in New Hampshire, that is essentially what I did, when I delivered ...

Resources last updated: 2/14/2016 12:20:07 AM