Solved

Underlining in RichEdit box

Posted on 1998-11-08
3
444 Views
Last Modified: 2013-11-20
I have a form that the user fills out and, upon completion, the form is printed in a RichEdit box in a set format (as a works cited entry).  I do this by attaching the variable m_entry to the RichEdit box and once the user clicks a button, the variable m_entry is filled using a simple (eg. m_entry = m_field1 + ", " + m_field2) method.  I would like to make one of the fields, when put into the m_entry variable, underlined.  How do I do this?
0
Comment
Question by:jkeebler
[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
  • 2
3 Comments
 
LVL 11

Accepted Solution

by:
mikeblas earned 50 total points
ID: 1324368
Select the text you want to format with SetSel(), then format it with GetSelectionFormat() and SetSelectionFormat().  It would end up looking something like this:

void CYourDlg::OnOK()
{
   // get first string
   CWnd* pEditOne = GetDlgItem(IDC_EDIT1);
   ASSERT(pEditOne != NULL);
   CString strOne;
   pEditOne->GetWindowText(strOne);

   // get second string
   CWnd* pEditTwo = GetDlgItem(IDC_EDIT2);
   ASSERT(pEditTwo != NULL);
   CString strTwo;
   pEditTwo->GetWindowText(strTwo);

   // set at the end of the control
   int n = m_richEdit.GetTextLength();
   m_richEdit.SetSel(n+1, n+1);
   
   // build a string to add
   CString strAdd;
   strAdd = strOne + " to " + strTwo + "\r\n";

   // add it to the control
   m_richEdit.ReplaceSel(strAdd, FALSE);

   // get formatting of string
   CHARRANGE range;
   m_richEdit.GetSel(range);
   
   // select first part of the string we just added
   range.cpMin -= strAdd.GetLength();
   range.cpMax = range.cpMin + strOne.GetLength();
   m_richEdit.SetSel(range);

   // get the format of that selection
   CHARFORMAT format;
   format.cbSize = sizeof(format);
   m_richEdit.GetSelectionCharFormat(format);
   
   // modify format to include underlining
   format.dwMask |= CFM_UNDERLINE;
   format.dwEffects |= CFE_UNDERLINE;

   // set formatting back
   m_richEdit.SetSelectionCharFormat(format);

   // select the end again
   n = m_richEdit.GetTextLength();
   m_richEdit.SetSel(n+1, n+1);
}

B ekiM

0
 

Author Comment

by:jkeebler
ID: 1324369
I got many errors when I tried this:

1) SetSel is not a member of CString
2) ReplaceSel is not a member of CString
3) GetSel is not a member of CString
4) GetSelectionCharFormat is not a member of CString
5) GetTextLength is not a member of CString

Do I need to add a library file or something?  I'm a beginner so I'd appreciate if someone could give me further instructions.  Thanks a lot.

0
 
LVL 11

Expert Comment

by:mikeblas
ID: 1324370
Your m_richEdit member is a CString bound by value, not a control bound by reference.

void CYourDlg::OnOK()
{
   // get first string
   CWnd* pEditOne = GetDlgItem(IDC_EDIT1);
   ASSERT(pEditOne != NULL);
   CString strOne;
   pEditOne->GetWindowText(strOne);

   // get second string
   CWnd* pEditTwo = GetDlgItem(IDC_EDIT2);
   ASSERT(pEditTwo != NULL);
   CString strTwo;
   pEditTwo->GetWindowText(strTwo);

   // get edit control itself
   CRichEditCtrl* pRich = (CRichEditCtrl*) GetDlgItem(IDC_RICHEDIT1);
   ASSERT(pRich != NULL)

   // set at the end of the control
   int n = pRich->GetTextLength();
   pRich->SetSel(n+1, n+1);
   
   // build a string to add
   CString strAdd;
   strAdd = strOne + " to " + strTwo + "\r\n";

   // add it to the control
   pRich->ReplaceSel(strAdd, FALSE);

   // get formatting of string
   CHARRANGE range;
   pRich->GetSel(range);
   
   // select first part of the string we just added
   range.cpMin -= strAdd.GetLength();
   range.cpMax = range.cpMin + strOne.GetLength();
   pRich->SetSel(range);

   // get the format of that selection
   CHARFORMAT format;
   format.cbSize = sizeof(format);
   pRich->GetSelectionCharFormat(format);
   
   // modify format to include underlining
   format.dwMask |= CFM_UNDERLINE;
   format.dwEffects |= CFE_UNDERLINE;

   // set formatting back
   pRich->SetSelectionCharFormat(format);

   // select the end again
   n = pRich->GetTextLength();
   pRich->.SetSel(n+1, n+1);
}

You should have been able to diagnose and correct the problem by yourself. To get a better understanding of data exchange in dialog boxes and the difference between exchanging the control reference and exchanging the data itself, grab a good MFC book or reread the documentation about DDX.

B ekiM


0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

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: 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.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

696 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