Solved

Underlining in RichEdit box

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error on moodle after upgrade 3 155
Best tools for Backup and restore of Wordpress site 6 58
dog bark java program 15 130
mapBully challenge 6 197
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

732 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