Solved

CRichEditView and last char removed from file

Posted on 2003-11-30
11
233 Views
Last Modified: 2013-11-20
I hav a CRichEditView and would like to get the last removed (or about to be removed) character when the user presses the backspace or delete key.

Any ideas?
0
Comment
Question by:boodabelly
  • 6
  • 5
11 Comments
 
LVL 23

Accepted Solution

by:
Roshan Davis earned 500 total points
ID: 9848191
Map WM_KEYDOWN do use the code

void CMyRichEditView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{

      long lStartPos, lEndPos;
      CString csTextToBeDeleted;

      GetRichEditCtrl().GetSel(lStartPos, lEndPos);

      if ( VK_BACK == nChar ||
             VK_DELETE == nChar )
      {
        if (lEndPos - lStartPos > 0)
        {
                  csTextToBeDeleted = GetRichEditCtrl().GetSelText();
        }
            else
            {
                  if ( VK_BACK == nChar )
                  {
                        GetRichEditCtrl().SetSel(lStartPos-1, lStartPos);
                  }
                  else
                  {
                        GetRichEditCtrl().SetSel(lStartPos, lStartPos+1);
                  }

                  csTextToBeDeleted = GetRichEditCtrl().GetSelText();

                  GetRichEditCtrl().SetSel(lStartPos, lEndPos);
            }

            csTextToBeDeleted;   <----------------------- THIS WILL BE THE TEXT THAT TO BE DELETED
      }
      
      

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

Good Luck
0
 
LVL 1

Author Comment

by:boodabelly
ID: 9848430
thanks man, works like a champ! =)
0
 
LVL 1

Author Comment

by:boodabelly
ID: 9848470
ugh, well, appearantly I accepted a little in haste and excitement.  I am trying to detect when a new line character is removed from the screen, can you answer that still or do I need to post another question?
0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9848480
I will check that....
0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9848495
In that case that text (csTextToBeDeleted) will give you the character 13.

char cChar = 13;

you can compare that text with cChar.

Rosh :)
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 1

Author Comment

by:boodabelly
ID: 9848665
ok, what am I doing wrong here?

// from constants file
#define BYY_NEW_LINE                  13

void Cbyy_view::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{

      long start_pos;
      long end_pos;

      if(!can_edit())
      {
            return;
      }

      GetRichEditCtrl().GetSel(start_pos, end_pos);

      if(nChar == VK_BACK || nChar == VK_DELETE)
      {
            if(end_pos - start_pos > 0)
            {
                  m_last_deleted_text = GetRichEditCtrl().GetSelText();
            }
            else
            {
                  if(nChar == VK_BACK)
                  {
                        GetRichEditCtrl().SetSel(start_pos - 1, start_pos);
                  }
                  else
                  {
                        GetRichEditCtrl().SetSel(start_pos, start_pos + 1);
                  }

                  m_last_deleted_text = GetRichEditCtrl().GetSelText();
                  GetRichEditCtrl().SetSel(start_pos, end_pos);
            }

            if(m_last_deleted_text == BYY_NEW_LINE)
            {
                  update_edit_areas(-1);
            }
      }

      CRichEditView::OnKeyDown(nChar, nRepCnt, nFlags);
}
0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9848704
This will work, I modified that string comparison like "GetAt" check the below code

// from constants file
#define BYY_NEW_LINE               13

void Cbyy_view::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{

    long start_pos;
    long end_pos;

    if(!can_edit())
    {
         return;
    }

    GetRichEditCtrl().GetSel(start_pos, end_pos);

    if(nChar == VK_BACK || nChar == VK_DELETE)
    {
         if(end_pos - start_pos > 0)
         {
              m_last_deleted_text = GetRichEditCtrl().GetSelText();
         }
         else
         {
              if(nChar == VK_BACK)
              {
                   GetRichEditCtrl().SetSel(start_pos - 1, start_pos);
              }
              else
              {
                   GetRichEditCtrl().SetSel(start_pos, start_pos + 1);
              }

              m_last_deleted_text = GetRichEditCtrl().GetSelText();
              GetRichEditCtrl().SetSel(start_pos, end_pos);
         }

         if(m_last_deleted_text.GetAt(0) == BYY_NEW_LINE)
         {
              update_edit_areas(-1);
         }
    }

    CRichEditView::OnKeyDown(nChar, nRepCnt, nFlags);
}
0
 
LVL 1

Author Comment

by:boodabelly
ID: 9848774
hmmm, well the .GetAt(0) crashes because the strlen is < 0.  m_last_deleted_text = "" when I remove a new line char.  I am thinking I might have to get the current line, move the cursor the appropriate direction and check the line number again.  I will try that and report back
0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9848779
Okay,
Good Luck
0
 
LVL 1

Author Comment

by:boodabelly
ID: 9848908
roshmon, cool man, this is what I got to work from the help you gave me, guess I need to get more used to this MFC stuff (I'm too old skool C), have to start thinking in MFC and getting comfortable with what functions do what, logic for MFC is pretty simple, just need to get more comfortable with it, ok, its 3am and I am rambling, BTW,this is what I got to work

void Cbyy_view::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{

      long start_pos;
      long end_pos;
      int line_num;


      line_num = m_curr_line;
      GetRichEditCtrl().GetSel(start_pos, end_pos);

      if(nChar == VK_BACK || nChar == VK_DELETE)
      {
            if(!can_edit())
            {
                  return;
            }

            if(nChar == VK_BACK)
            {
                  GetRichEditCtrl().SetSel(start_pos - 1, start_pos - 1);
            }
            else
            {
                  // increment two positions to go past the '\n'
                  GetRichEditCtrl().SetSel(start_pos + 2, start_pos + 2);
            }

            line_num = GetRichEditCtrl().LineFromChar(-1) + 1;

            if(line_num != m_curr_line)
            {
                  update_edit_areas(-1);
            }

            GetRichEditCtrl().SetSel(start_pos, end_pos);
      }

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

thanks again man!
0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9848928
Welcome :0)
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
Different colored text in ComboBox without Subclassing 8 55
WinWaitActive parameters 12 31
Message not shown 5 33
Capture logon name 13 42
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 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

17 Experts available now in Live!

Get 1:1 Help Now