?
Solved

Edit.SetSel(iStart,iEnd); make illegality char

Posted on 2003-03-10
8
Medium Priority
?
388 Views
Last Modified: 2013-11-20
here is my function for get what char had been deleted or been backspaced.
void CNoteView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
     CData *myData;    
     int iRet;
     CString sSel;

     //delete
     if (nChar == 46)
     {
       CEdit& mEdit = GetEditCtrl();
       mEdit.GetSel(iStart,iEnd);
       if (iStart != iEnd)
       {
          CEditView::OnKeyDown(nChar, nRepCnt, nFlags);
         return;
       }
       iEnd ++;
       mEdit.SetSel(iStart,iEnd);
       GetSelectedText(sSel);
     }
     else //backspace
     if (nChar == 8)
     {
       CEdit& mEdit = GetEditCtrl();
       mEdit.GetSel(iStart,iEnd);
         
       if ((iStart != iEnd)||(iStart == 0))
       {
         CEditView::OnKeyDown(nChar, nRepCnt, nFlags);
         return;
       }
       iStart --;
           
       mEdit.SetSel(iStart,iEnd);
       GetSelectedText(sSel);
     }

     CEditView::OnKeyDown(nChar, nRepCnt, nFlags);
}
everything is ok, if not delete a enter or press backspace in front of a line.
for example: text is :
abcd
1234
efg

now if stop the cursor at the end of abcd, and then press delete,the text will changed to:
abcd|1234
efg

why?
how can I get it if a enter been delete?
0
Comment
Question by:dead_fish
[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
  • 4
  • 2
  • 2
8 Comments
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 8108541
Please comment the line

//mEdit.SetSel(iStart,iEnd);

and try again.

Good Luck
0
 
LVL 22

Accepted Solution

by:
ambience earned 750 total points
ID: 8108706
what is the purpose of

if (iStart != iEnd)
{
    CEditView::OnKeyDown(nChar, nRepCnt, nFlags);
   return;
}
 
if you just want to get the text that will be deleted you only need to do the following,

 if (nChar == 46)
    {
      CEdit& mEdit = GetEditCtrl();
      mEdit.GetSel(iStart,iEnd);
      if (iStart == iEnd)
      {
         iEnd ++;
      }
      mEdit.SetSel(iStart,iEnd);
      GetSelectedText(sSel);
      CEditView::OnKeyDown(nChar, nRepCnt, nFlags);
    }

at the end of this sSel contains the text that is going to be deleted, if it was "ENTER" sSel would contain "\r\n".
0
 

Author Comment

by:dead_fish
ID: 8109029
hi roshmon :
if comment the line
//mEdit.SetSel(iStart,iEnd);
then how can I get w ath char had been deleted?

the purpose of
if (iStart != iEnd)
{
   CEditView::OnKeyDown(nChar, nRepCnt, nFlags);
  return;
}
is if delete not any char then ignore it,
I just care about one char been deleted.

my question is if it was "ENTER", sSel would contain "\r\n".But now why it can not contain "\r\n"
and text been changed to "|"

thanks

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: 8109249
You can identify the char by using CharFromPos GetCaretPos.
If it is ENTER ignore the SetSet and increase count.

Good Luck
0
 

Author Comment

by:dead_fish
ID: 8109254
now I changed function:

void CNoteView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
     int iRet;
     CString sSel=13;
     int iLength;

     //delete
     if (nChar == 46)
     {
          CEdit& mEdit = GetEditCtrl();
         mEdit.GetSel(iStart,iEnd);
         iLength = GetBufferLength();
         if ((iStart != iEnd) || (iLength == iStart))
         {
             CEditView::OnKeyDown(nChar, nRepCnt, nFlags);
             return;
         }
         iEnd += 2;
         mEdit.SetSel(iStart,iEnd);              
         GetSelectedText(sSel);
         if (sSel.GetLength() == 0)
            {
             sSel = 13;
         }
         if (sSel.GetLength() == 2)
         {
             iEnd --;
             mEdit.SetSel(iStart,iEnd);          
             GetSelectedText(sSel);
         }
     }
     else //backspace
     if (nChar == 8)
     {
         CEdit& mEdit = GetEditCtrl();
         mEdit.GetSel(iStart,iEnd);
         if ((iStart != iEnd)||(iStart == 0))
         {
          CEditView::OnKeyDown(nChar, nRepCnt, nFlags);
          return;
         }
         iStart -= 2;
            mEdit.SetSel(iStart,iEnd);
         GetSelectedText(sSel);
         if (sSel.GetLength() == 2)
         {
          iStart ++;
          mEdit.SetSel(iStart,iEnd);                       GetSelectedText(sSel);
         }
         if (sSel.GetLength() == 0)
            {
          sSel = 13;
         }
     }

     CEditView::OnKeyDown(nChar, nRepCnt, nFlags);
}

everything is ok now! :)

thanks!!!
0
 
LVL 22

Expert Comment

by:ambience
ID: 8109375
>> text been changed to "|"


do you mean when you debug or view the text you actualy see a symbol like that, well that is quite understandable, Enter is non-printable character so to say, if the edit box is multi-line, "\r\n" would break the line and begin start of a new line, however inside a single-line box it appears as a non-printable char which is something like "|".

you can compare the text programaticaly to see if it is the enter sequence or not, like

if(sSel == "\r\n")
   AfxMessageBox("Enter is selected");
else
   AfxMessageBox("Error garbage selected");
0
 

Author Comment

by:dead_fish
ID: 8109470
where "/r/n" changed to "/r" or "/n", it will show "|".
so in my last function it select "/r/n", in my first function it only select "/r" or "/n", that why "/r/n" changed to "/n" or "/r".

if between iStart and iEnd is "/r/n"
GetSelectedText(sSel);
can not get this string,it will be null.
0
 

Author Comment

by:dead_fish
ID: 8134424
his answer remind me that "ENTER" have two char not one.

thank you!
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

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…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month14 days, 1 hour left to enroll

800 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