Solved

How To Change the String in CRichEditCtrl

Posted on 1999-01-04
3
337 Views
Last Modified: 2013-11-20
RE is a CRichEditCtrl object. What I have is a spell checker. There are four choices for the user 1st Ignore,Ignore All, Change, and Change All.
so basically I have this options in a switch statement.
Since I already have the Change working how do I implement the ChangeAll. These are push buttons.If the spell checker detects a mispelled word. It list the suggestion in the list box.
switch (idAction)
                                          {
                                          case IDIGNORE:
                                                // skip this instance of pToken
                                                break;
                                          case IDIGNOREALL:
                                                // skip every instance of pToken - add to ignore list
                                                break;
                                          case IDCHANGE:
                                                // change this instance of pToken to dlg.m_strChange
                                                RE.ReplaceSel(LPCTSTR(dlg.m_strChange), true);
                                                nNew = dlg.m_strChange.GetLength();
                                                int dxChars += (nNew - nOld);
                                                
                                                break;
                                          case IDCHANGEALL:
                                                // change every instance of pToken to dlg.m_strChange
                                                break;
                                          default:
                                                break;
0
Comment
Question by:lucent2000
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
plaroche earned 100 total points
ID: 1327168
Use the FindText member of CRichEditCtrl and use a while loop to find every instance of the word by using the WHOLEWORD attribute for FindText.

Using the found position of the word execute a SetSel call to set the selection to the found word. Then use the same ReplaceSel call for your IDCHANGE code.


0
 

Author Comment

by:lucent2000
ID: 1327169
Can you show me how to do this?
0
 

Author Comment

by:lucent2000
ID: 1327170
void CNarrativeView::CheckSpelling(bool bAll)
{
      CString strText = _T("");
      if (bAll)
      {
            //check all word
            strText = GetNarrative();
            if(!strText.IsEmpty())
            {
                  int dxChars = 0;
                  int n = strText.GetLength();
                  PSTR pszBuf = new char[n+1];
                  if (!pszBuf)
                  {
                        return;
                  }
                  //copies string to the buffer
                  strcpy(pszBuf,LPCTSTR(strText));
                  char seps[] = " ,\t\r\n";
                  PSTR pToken = NULL;
                  
                  //check spelling suggestion
                  _ApplicationPtr pWord = reinterpret_cast<CNarrativeApp*>(AfxGetApp())->GetWordPtr();
                  if (pWord)
                  {
                        pToken = strtok(pszBuf,seps);
                        CStringArray strSugs;
                        bool bCheck = false;
                        while( pToken != NULL )
                        {
                              bCheck = true;
                              if (m_Options.m_bIgnoreNumbers)
                              {
                              CString strWord = pToken;

                                    if (strWord.FindOneOf("0123456789") != -1)
                                    {
                                          bCheck = false;
                                    }
                              }
                              if (bCheck && m_Options.m_bIgnoreUpper)
                              {
                              CString strWord = pToken;

                                    if (strWord.FindOneOf("abcedfghijklmnopqrstuvwxyz") == -1)
                                    {
                                          bCheck = false;
                                    }
                              }


                              if (bCheck)
                              {
                                    _bstr_t bstrWord(pToken);
                                    VARIANT_BOOL vtBool = pWord->CheckSpelling(bstrWord);

                                    if (vtBool == VARIANT_FALSE)
                                    {
                                          if (m_Options.m_bSuggestions)
                                          {
                                                SpellingSuggestionsPtr pSugs = NULL;
                                                pSugs = pWord->GetSpellingSuggestions(bstrWord);
                                                int iCount = pSugs->GetCount();
                                                strSugs.RemoveAll();
                                                for (int i =1; i <= iCount; i++)
                                                {
                                                      SpellingSuggestionPtr pSugWord = pSugs->Item(i);
                                                      
                                                      if (pSugWord)
                                                      {
                                                            // Build list
                                                            CString strSug = (LPCTSTR)pSugWord->GetName();
                                                            TRACE("Suggestion: <%s>\r\n", strSug);
                                                            strSugs.Add(strSug);
                                                      }
                                                }
                                          }

                                          CDlgSpelling dlg;
                                          // copy array elements
                                          int nSugs = strSugs.GetSize();
                                          for (int i = 0; i < nSugs; i++)
                                          {
                                                dlg.m_astrSugs.Add(strSugs[i]);
                                          }
                                          int nNew = 0;
                                          int nOld = strlen(pToken);
                                          int iStart = pToken - pszBuf;
                                          iStart += dxChars;
                                          int iEnd = iStart + nOld;
                                          CRichEditCtrl& RE = GetRichEditCtrl();
                                          RE.SetSel(iStart, iEnd);
                                                      
                                          dlg.m_strWrong = pToken;
                                          int idAction = dlg.DoModal();

                                          
                                          switch (idAction)
                                          {
                                          case IDIGNORE:
                                                // skip this instance of pToken
                                                break;
                                          case IDIGNOREALL:
                                                // skip every instance of pToken - add to ignore list
                                                break;
                                          case IDCHANGE:
                                                //change this instance of pToken to dlg.m_strChange
                                                RE.ReplaceSel(LPCTSTR(dlg.m_strChange), true);
                                                nNew = dlg.m_strChange.GetLength();
                                                dxChars += (nNew - nOld);
                                                
                                                break;
                                          case IDCHANGEALL:
                                                //change every instance of pToken to dlg.m_strChange
                                                
                                                /*Use the FindText member of CRichEditCtrl and
                                                use a while loop to find every instance of the word
                                                by using the WHOLEWORD attribute for FindText.
                                                Using the found position of the word execute a SetSel call
                                                to set the selection to the found word.
                                                Then use the same ReplaceSel in the IDCHANGE code.*/
                                                CHARRANGE chrg;      // range to search
                                                LPSTR lpstrText;     // null-terminated string to find
                                                CHARRANGE chrgText;  // range in which text is found
                                                FINDTEXTEX ft;
                                                long nFind = RE.FindText(FR_WHOLEWORD,FINDTEXTEX);
                                                long nMinPos = RE.SetSel(chrgText.cpMin);
                                                long nMaxPos = RE.SetSel(chrgText.cpMax);

                                                break;
                                          default:
                                                break;
                                          }
                                          
                                    }
                              }
                              pToken = strtok( NULL, seps );
                        }
                        
                  }
                  delete pszBuf;
            }
            
      }
      
      else
      {
            CRichEditCtrl& RE = GetRichEditCtrl();
            strText = RE.GetSelText();
            if (!strText.IsEmpty())
            {
                  _ApplicationPtr pWord = reinterpret_cast<CNarrativeApp*>(AfxGetApp())->GetWordPtr();
                  if (pWord)
                  {
                  _bstr_t bstrWord(strText);
                  VARIANT_BOOL vtBool = pWord->CheckSpelling(bstrWord);

                        if (vtBool == VARIANT_FALSE)
                        {
                        SpellingSuggestionsPtr pSugs = NULL;
                        pSugs = pWord->GetSpellingSuggestions(bstrWord);
                        int iCount = pSugs->GetCount();

                              for (int i = 1; i <= iCount; i++)
                              {
                                    SpellingSuggestionPtr pSugWord = pSugs->Item(i);
                                    if (pSugWord)
                                    {
                                    CString strWord = (LPCTSTR)pSugWord->GetName();

                                          TRACE("Suggestion: <%s>\r\n", strWord);
                                    
                                    }
                              }
                              //dlg
                              CDlgSpelling dlg;
                              dlg.DoModal();
                        }
                  }
            }
      }

}



0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
bigDiff challenge 17 105
sum13 challenge 24 106
wordappend challenge 8 172
"Black Box" Testing of Control System Software 2 49
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…
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

785 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