[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 489
  • Last Modified:

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
0
Iswandi
Asked:
Iswandi
  • 7
  • 6
  • 2
  • +2
1 Solution
 
migelCommented:
Hi!
Use CEdit::SetSel(int nIindexStart,  int nIndexEnd)
0
 
ShaunWildeCommented:
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
0
 
RONSLOWCommented:
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.

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
IswandiAuthor Commented:
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...
0
 
IswandiAuthor Commented:
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...
0
 
ShaunWildeCommented:
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
0
 
trrajCommented:
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.

0
 
RONSLOWCommented:
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)

0
 
IswandiAuthor Commented:
Adjusted points from 65 to 70
0
 
IswandiAuthor Commented:
I'll try it out and tell you guys how it goes a long
0
 
IswandiAuthor Commented:
trraj, it won't work for multiselection
sorry
0
 
RONSLOWCommented:
You can display any words highlighted using a rich-edit control.

What you cannot do (easily at least) is multply-select the words with control+click etc.  To do something like that, you'd need to handle the clicking yourself, remember what was selected and change the text colour (and background) appropriately.
0
 
IswandiAuthor Commented:
Can you give me a simple example ?
0
 
RONSLOWCommented:
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?
0
 
IswandiAuthor Commented:
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.
0
 
RONSLOWCommented:
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.
0
 
RONSLOWCommented:
see also the MFC docs on CRichEdit for FindText and SetSelectionCharFormat
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 6
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now