views:

718

answers:

2

My issue is that I've created an "extended" RichTextBox control that uses native API to add many RichEdit features that are missing from the standard control (ie: modifying a single font property on a selection w/o changing the other font properties, word wrap to a printer [like WordPad], etc.). As part of the control I expose a PrintDocument that is used to print the formatted contents of the RichTextBox. When wordwrap is set to "Wrap To Printer" I SendMessage the EM_SETTARGETDEVICE message to the RichTextBox and cause it to wrap to the appropriate length.

This all works fine when something (user/code) changes the WordWrap property of my control. However if the PrintDocument is modified after that I have no way of knowing it. So even though the user may have changed the margins on the PrintDocument my RichTextBoxEx doesn't resend the EM_SETTARGETDEVICE for the new width until the WordWrap property is changed.

I see a few options to overcome this but I'm not a big fan of any of them. Here's what I have:

  1. Add a UpdatePrintDocument() method or similiar that would need to be called after something external from the control (ie: a PageSetupDialog on the parent form) updated the settings in the PrintDocument. Cons: I'll be distributing the control so I'd like to make it as friendly as possible. While I may remember to call the method anytime I successfully update the PrintDocument settings someone else might not. Pro: It's simple to implement.

  2. Create a new, PrintDocumentEx class that bases from PrintDocument and implements the needed "Changed" events. Cons: Might not be enough, might need to create PrintSettingsEx, PageSettingsEx, etc.. Pro: Implement once and no one has to worry about it again.

I really think #2 is the option I'm going to have to go with but it's not very reusable for the next instance I need some similiar functionality. I guess what I'm looking for is a way to attach a "generic PropertyChanged event" to any existing classes property since this would be applicable in future situations. Anxious to see what you guys have for me :-)

A: 

If I have understood your question correctly, the information that you require is sent when the WordWrap property is changed.

When other things are changed, no events updates the Print Document. The next time the WordWrap property is changed all information is sent.

The hack way to fix this is then to change the WordWrap property, whenever you change a property that you want to send to the Print Document. Change it to a temporary value, then change it back again.

Shiraz Bhaiji
+1  A: 

Just following up that my PrintDocumentEx (and associated) classed didn't work. Looking into the PrintDialog and related controls it's because they use native methods to acctually update the PrintDocument. So the events I attached to the properties in my "Ex" classes never fired because the set accessor was never invoked.

Cory Charlton
Hmmm did I open a bounty on this? I can't accept an answer and that sucks because my accept rate is low :-)
Cory Charlton
Yes, you did: `Bounty creation of 100 rep on question id = 1871103 Dec 15 at 4:49`
Marc Gravell