Solved

CRichEditView and last char removed from file

Posted on 2003-11-30
11
231 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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: 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…
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.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

707 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

13 Experts available now in Live!

Get 1:1 Help Now