Solved

Saving colored text to a MSAccess database from a RichEdit COntrol

Posted on 2004-09-24
12
319 Views
Last Modified: 2013-11-20
Hi all;

Got an ODBC VC++ ver 6.0 app. which connects to a MS Acces database. I have a Rich Edit control with text (CString) data. When I'm on a particular record, I click a button, and the text in the Rich Edit turns red...this works fine...but why is it the red text does not get saved to the data base ?? Do I have to change something in the actual Access database setup or in my ODBC CVIew based app? The code I use to turn the code red when I hit the ColorButton is this :

void CFormomaxView::OnColorButton()
{
     m_RichEdit.SetSel(0,1000);//color will affect 1000 characters in the edit control
     CHARFORMAT2 cf;
     cf.cbSize = sizeof(CHARFORMAT2) ;
     cf.dwMask = CFM_BOLD|CFM_COLOR;
     cf.dwEffects = CFE_BOLD ;
     cf.crTextColor = RGB(255,0, 0) ;//set to red
     m_RichEdit.SendMessage(EM_SETCHARFORMAT, (WPARAM)SCF_SELECTION, (LPARAM)&cf) ;            

}

Any takers on this one ? THanks!
0
Comment
Question by:pigeonbr
  • 7
  • 5
12 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 12143086
You need to save the colour to another field (a text field stores text, not formatting information + text) in the database (a long should be suitable)
0
 

Author Comment

by:pigeonbr
ID: 12143391
OK..thanks...so, if we apply this to the code I have given above, then that would mean that my CHARFORMAT2 cf would have to be saved to this new long field every time I change records, then read from when reading a new record, all done automatically by DoDataExchange() ...am I on the right track ? If so, could you give me an example of how this is done ? Not quite sure how to go about getting the cf in my example into a long type field...this cf contains several pieces of information such as bold, color...can all this be saved in same field ? Could you be a little more specific please :) Thanks...I'm beginning to get the picture :)
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 12143735
Ah, you just don't want the colour saved, you want all the formating information.
Three possible routes to go.
1) Store the complete contents of the rtf file in a BLOB field.
2) Store the complete contents of the rtf file in a MEMO field.
3) Store the rtf file to disk and just store the file name in the database.  (store in sub directories where the access file is stored? For performance have multiple sub dirs, max 512 files per directory).

For 1 and 2  by complete contents I don't mean just the text.  I mean the complete file as if it was saved to disk.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 12143758
A general point.  Memo fields in MS Access databases are known as a weak point that can result in a corrupt database requiring a repair.
0
 

Author Comment

by:pigeonbr
ID: 12145729
Andy,
Ok...Like I said the app is ODBC connected to MS ACcess databse...my RichEdit control is connected to a Memo field already in the database, and the DoDataExchange() doesn't save the formatting data...what is it I need to do more specifically ?
0
 

Author Comment

by:pigeonbr
ID: 12146042
Andy;
I took oout the code that makes the text bold..I don't need that ...so now I'm down to...

void CFormomaxView::OnColorButton()
{

    m_RichEdit.SetSel(0,1000);//color will affect 1000 characters in the edit control
     CHARFORMAT2 cf;
     cf.cbSize = sizeof(CHARFORMAT2) ;
     cf.dwMask = CFM_COLOR;
     cf.dwEffects = CFE_BOLD ;
     cf.crTextColor = RGB(255,0, 0) ;//set to red
     m_RichEdit.SendMessage(EM_SETCHARFORMAT, (WPARAM)SCF_SELECTION, (LPARAM)&cf) ;            

}


ok..Right now as I said I'm saving the text in the RichEdit control to a Memo field..obviously the color is not getting saved. You are saying i should save the data as an rtf file IN the same Memo field? If this is so, how do I go about this ?

I found this help in another related question:
"You use CRichEditCtrl::StreamIn() to save the text to a disk file, you can specify the format you need in there."
Is this the way to go or is there a way I can get all info into the same Memo field, so when I go from record to record with the controls on the toolbar, all the data gets loaded with DoDataExchange() automatically?

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:pigeonbr
ID: 12148739
Hi Guys,

Did some searching...almost there...this is what my Button that changes the color of RichEdit COntrol text to red does:

void CFormomaxView::OnColorButton()
{

       //******************************
      //Colors the text red       
       m_RichEdit.SetSel(0,1000);//color will affect 1000 characters in the edit control
     CHARFORMAT2 cf;
     cf.cbSize = sizeof(CHARFORMAT2) ;
     cf.dwMask = CFM_COLOR;
     cf.dwEffects = CFE_BOLD ;
     cf.crTextColor = RGB(255,0, 0) ;//set to red
     m_RichEdit.SendMessage(EM_SETCHARFORMAT,(WPARAM)SCF_SELECTION, (LPARAM)&cf) ;      
      //*************************************
      //Save RTF info for RichEdit COntrol
      CString strRichEdit;
      strRichEdit=m_RichEdit.GetRTF();//get the rtf string
      //save the rtf data into the memo field for that customer
      m_pSet->Edit();      //Put database in edit mode
      m_pSet->m_Note_sp_ciale=strRichEdit;   //stick rtf text into database field
      m_pSet->Update();   //Update recordset
      
      
}

ok..this actually works because I go into the database after I go thru this and the rtf data is in the field with the text! When I heit the Next record arrow to change records, the text that appears in the RichEdit COntrol is also red!!! Shouldn't be...then when I quit the program and restart, it reads the text as black again ...got some bugs to work out but I'm getting closer.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 12149383
What you need to decide is what is to be saved.
Do you just want to save the colour AND is all the text in the control the same colour?
Or do you have sections of the text as bold, red and other bits as blue, italic and other....

For the first all you would require is to save the colour (RGB format is 0xFFFFFF for white - so a long field should be suitable).  Now when you change the record you set the text in your control to the new text and at the same time set the colour in your CHARFORMAT2 structure and apply as you do already.

For the second you need to effectively store the file.  (The StreamIn, StreamOut you have found would be required for that. I would suggest either a BLOB field or save it to disk and just store the file name in the DB.)
0
 

Author Comment

by:pigeonbr
ID: 12150278
Andy,
All the text in the box is the same red color.

What I need some details on is exectly the best way to do what your are suggesting...when you say "Now when you change the record you set the text in your control to the new text and at the same time set the colour " ...When I change records, DDX takes care of this...What should I be doing exectly ?? Could you please clear this up for me :)
0
 

Author Comment

by:pigeonbr
ID: 12151295
On thinking this out some more, I think what I need to know is how to get the RGB info to save it...I know how to save it to another field in the databse...what is the function to get this data from the RichEdit COntrol ?
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 12153977
The DDX event fires when you update the data (change current record). At this point save/set the colour (just have another var in the recordset)

The colour in the richedit is
cf.crTextColor
Just cast it to long for saving.
0
 

Author Comment

by:pigeonbr
ID: 12168393
Andy
Finally got it working :) Thanks for your guidance Andy. It took a while for me to grasp exectly how to retrieve the color data from the database, but foound a way to do it. Thanks again
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

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