Solved

CEdit - Highlight text

Posted on 2004-04-16
7
1,688 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
deburging in oracle form 12 88
Tab names are off by one 5 53
modThree challenge 4 77
firstChar challenge 13 107
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

911 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

21 Experts available now in Live!

Get 1:1 Help Now