Cannot highlight first word of sentence Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
TexasJimbo
Knows some wx things
Knows some wx things
Posts: 28
Joined: Sun May 04, 2008 6:41 pm

Cannot highlight first word of sentence

Post by TexasJimbo » Sat Feb 25, 2012 3:20 am

I have a text control with RICH_TEXT2 style enabled. In this control I want to write a sentence and within a loop highlight one word at a time...so for instance, write the sentence "One day at a time." to the text control, then on the first iteration of the loop highlight the word "One", then on the next iteration highlight "day", etc. The code works except for the first iteration, and the first word in the text control does not get highlighted. The code snippet that actually manipulates the text control is as follows:

Code: Select all

            SentenceTC -> Clear();
            SentenceTC -> AppendText(currentSentence -> sentence.GetData());
            startingPosition = currentSentence -> sentence.Find(currWord -> word.GetData());
            wordLength = currWord -> word.Length();
            endingPosition = startingPosition + wordLength;
            SentenceTC -> SetStyle(startingPosition, endingPosition, wordStyle);
I also tried to update the control (SentenceTC -> Update()), but that has no effect. I also tried setting RICH_TEXT style which didn't have any effect either.
I am running this under Windows XP.

Any ideas why this doesn't work for the first word or what I could do to make it work?
Thanks in advance for any advice.

User avatar
doublemax
Moderator
Moderator
Posts: 15170
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Cannot highlight first word of sentence

Post by doublemax » Sat Feb 25, 2012 3:55 am

I tested with the following code and it worked:

Code: Select all

wxTextCtrl *tc = new wxTextCtrl(this, wxID_ANY, "this is a stupid test", wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE|wxTE_RICH2);
wxTextAttr red;
red.SetTextColour( *wxRED );
red.SetFlags( wxTEXT_ATTR_TEXT_COLOUR );
tc->SetStyle( 0, 4, red );
Are you sure that startingPosition, endingPosition contain the correct values?

Although probably not related to this problem, but this line looks suspicious:

Code: Select all

startingPosition = currentSentence -> sentence.Find(currWord -> word.GetData());
Will this work if a word occurs multiple times in the sentence?
Use the source, Luke!

TexasJimbo
Knows some wx things
Knows some wx things
Posts: 28
Joined: Sun May 04, 2008 6:41 pm

Re: Cannot highlight first word of sentence

Post by TexasJimbo » Sat Feb 25, 2012 4:22 pm

I'm almost embarrassed to post this...I discovered my problem was a comparison of apples to oranges.
The Sentence I was displaying contained punctuation whereas the words making up the sentence were retrieved from a dictionary.
So the first word of every sentence was capitalized but the comparison to the dictionary version of the same word was always lowercase and so the "Find" method returned a wxNOT_FOUND value. Also, as doublemax pointed out, this would not work if I had the same word multiple times in the same sentence.

Thanks for the feedback.
Jim

Post Reply