Solved

CEdit - Highlight text

Posted on 2004-04-16
7
1,682 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
7 Comments
 
LVL 4

Accepted Solution

by:
caner_elci earned 200 total points
Comment Utility
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
Comment Utility
Try this:

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

Expert Comment

by:AlexFM
Comment Utility
Correction:

 myEditCtrl.SetFocus();
 myEditCtrl.PostMessage(EM_SETSEL, 0, -1);
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 23

Expert Comment

by:Roshan Davis
Comment Utility
Hi Alex,
SendMessage right?

Rosh :)
0
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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 …
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.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now