Solved

CEdit - Highlight text

Posted on 2004-04-16
7
1,708 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
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!

 
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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Strange date stored 22 171
Question regarding Copy/Paste 16 97
conditional code and condition difference 9 84
how to split multiple lines delimiter : 8 119
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

761 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