Link to home
Start Free TrialLog in
Avatar of jkeebler
jkeebler

asked on

Underlining in RichEdit box

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?
ASKER CERTIFIED SOLUTION
Avatar of mikeblas
mikeblas

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jkeebler
jkeebler

ASKER

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.

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