Solved

Underlining in RichEdit box

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

914 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

18 Experts available now in Live!

Get 1:1 Help Now