Solved

Underlining in RichEdit box

Posted on 1998-11-08
3
433 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
  • 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Unix / Linux grid computing 5 125
move a line in eclipse 3 60
Doc'in system (example?) BA 7 78
Fibonacci challenge 11 82
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

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

17 Experts available now in Live!

Get 1:1 Help Now