Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2003-03-10
8
Medium Priority
?
415 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
  • 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
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: 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses

572 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