Solved

CEdit - Highlight text

Posted on 2004-04-16
7
1,712 Views
Last Modified: 2013-11-20
I have an CEdit control and I would like to highliht the text after the user press return. My code delete the text and doesn't highlight it. Any suggestions?

Code snippet:

BOOL CMyRecView::MyFunction()
{
   ....
   myEditCtrl.SetFocus();
   myEditCtrl.SetSel(0,-1)
    ....
}

I have tried :
  ((CEdit*)GetDlgItem(IDC_MY_EDIT))->SetSel(0, 2);
which remove the first two characters of the text instead of highlight.

Thanks inadvance.
0
Comment
Question by:DBOTMA
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 4

Accepted Solution

by:
caner_elci earned 200 total points
ID: 10840533
myEditCtrl.SetSel( 0, -1 )  or myEditCtrl.SetSel( 0, 65535 ) would perfectly work.. But there may be a problem with focus or keyboard handling.. you say you wanna select the text when user presses Enter key, for example, when the user presses Enter, you get the message, select the text, then message bubbling continues and deselects the text.. so, check it out, clear virtual key state in WM_KEYDOWN and WM_KEYUP message handlers.. that would stop sending Enter key to MFC's CEdit and stuff..
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10840569
Try this:

  myEditCtrl.SetFocus();
  myEditCtrl.PostMessageEM_SETSEL, 0, -1);
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10840573
Correction:

 myEditCtrl.SetFocus();
 myEditCtrl.PostMessage(EM_SETSEL, 0, -1);
0
Industry Leaders: 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!

 
LVL 23

Expert Comment

by:Roshan Davis
ID: 10841155
Hi Alex,
SendMessage right?

Rosh :)
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10841525
PostMessage. I don't see how MyFunction is called and my guess is that some keyboard message is not handled yet and it clears editbox.
0
 

Expert Comment

by:shetlandbob
ID: 10842312
Have you set the "Want Return" to true in the editbox properties? this may have an effect?

I have a dialog control with control variable "editBoxControl", the following code will select the entire string.

  CEdit* edit;
  edit = &editBoxControl;
  edit->SetSel(0,-1,false);


I wonder if the problem may be that when you press enter the text box is being cleared, and so there is nothing to highlight? thats the first thing I would check?

How are you capturing the return?
0
 

Author Comment

by:DBOTMA
ID: 10856089
BOOL CMyRecView::PreTranslateMessage(MSG* pMsg)
{
    if (pMsg->message == WM_KEYDOWN &&
         pMsg->wParam == VK_RETURN )
    {
       
         ...some code......
       
         return TRUE;                   //I ADD THIS RETURN WHICH FIXED MY PROBLEM
    }

    return CRecordView::PreTranslateMessage(pMsg);
}
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Add content to output file 4 72
no14 challenge 14 76
abstract class with all non abstract mentods 6 87
c++  placing data into a form and an editbox 5 46
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question