Link to home
Start Free TrialLog in
Avatar of Iswandi
Iswandi

asked on

Highlighting in Edit Box

I want to highlight several words in my edit box, can anybody tell me how to do it ?
Some codes will be help ful
Avatar of migel
migel

Hi!
Use CEdit::SetSel(int nIindexStart,  int nIndexEnd)
use setsel to select a range as mentioned above else if you wanted to highlight individual words i.e. all 'the's in the
'the quick brown fox jumps over the lazy dog'
then, I think you will have to use an owner draw edit box
an edit box only has a single selection allowed.

you also cannot change the colours of part of the text within an edit box (all text (other than selected) is displayed in the same colours and fonts etc).

if you want to be able to highlight text etc, then use a rich edit control, which lets you contorl font, colour etc of inidividula characters/words etc.

Avatar of Iswandi

ASKER

Can I use the Rich Edit in dialog box ? I don't know why but my Visual C++ seems to shut the application if I put a rich edit inside the dialog box, and I would like to listen more about owner draw edit box... Can you give me more details Shawn, because I want to do exactly what you tell me? Like I want to highlight all "me" in this comments, and I want the highlight to stays permanently...
Avatar of Iswandi

ASKER

I'm a beginner programmer, but I have looked through the library of CEdit and Rich Edit, and I do pick up things quite fast and I don't think the question has quite straight forward answer, I would like to post more points, but this is all I got...
have a look at http://www.codeguru.com/editctrl/color_edit.shtml - it might even do what you want - it depends on whether you wish to roll your own or not - either way read the code and understand it if you are going to use it
Hi ,
    As mentioned in previous comment,
   1. use CEdit:SetSet() to select the text.
   2. Get the DC of the Edit box using CWnd:GetDC()
   3. Set the text color using pDC->SetTextColor().

You should be able to do in this way.

Iswandi .. You can use richedit control in a dialog.  See MS JB article to stop it from crashing... PRB: Dialog With RichEdit May Fail During Creation ID: Q166132

trraj .. SetSsel will only do a single selection .. not multiple as Isawndi requires.  SetTextColor won't help much unless you do in in an WM_CTLCOLOR handler

There is code at codeguru for richedit controls (including a color-highlighted text editor).

look at:
http://www.codeguru.com/richedit/syntax_hilight.shtml
http://www.codeguru.com/richedit/RichSyntaxView.shtml
http://www.codeguru.com/editctrl/syntax_coloring.shtml (CWnd-derived)

Avatar of Iswandi

ASKER

Adjusted points from 65 to 70
Avatar of Iswandi

ASKER

I'll try it out and tell you guys how it goes a long
Avatar of Iswandi

ASKER

trraj, it won't work for multiselection
sorry
ASKER CERTIFIED SOLUTION
Avatar of RONSLOW
RONSLOW

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Iswandi

ASKER

Can you give me a simple example ?
to highlight a word, select it (programmatically) and change character format.

is it something like syntax highlighting you want?  In that case, then you won't need the ctrl+click (say) to dynamically highlight words selected by the user).  Can you give a better indication of what it is you are trying to do?
Avatar of Iswandi

ASKER

I just want to highlight a certain words and let the highlighting stays...
let say there is a document containing as follow :

This is a car. This is a book and that is not a pen.

let's say I want is to be highlighted, I want every is in the document to somehow appear differently in color. In this example I'll make them in quotation mark.

Th"is" "is" a car. Th"is" "is" a book and that "is" not a pen.

I want the words in the quotation mark has different color.
in that case, search through the rich-text document looking for the letters "is" and select each one in turn (temporarily) and change the character formatting for the selection to highlight it.

This is the sort of thing syntax colour highlighting does.  Have a look at the examples at codeguru that use the richedit control for syntax colour highlighting and see how they change the colour of the individual words.
see also the MFC docs on CRichEdit for FindText and SetSelectionCharFormat