C# PrintDocument Changed event
Asked Answered
O

2

8

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 :-)

Overbalance answered 9/12, 2009 at 1:35 Comment(3)
Time is of the issue so I went ahead and started implementing PrintDocumentEx. As I expected the WinForms print dialogs do not create new PageSettings, PrinterSettings, etc but modify the existing properties so I'll need to create "Ex" classes for all objects and bubble my "Changed" event for each leaf property back to the PrintDocumentEx. It's nasty but its progress. Still anxious to see if there is a more generic solution I'm missing.Overbalance
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. For now I went with the UpdatePrintDocument() method but am still looking for a better solution.Overbalance
Interesting that I can make a useless question (one that had a bounty with an unaccepted answer) a community wiki post.Overbalance
O
1

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.

Overbalance answered 9/12, 2009 at 1:35 Comment(1)
Yes, you did: Bounty creation of 100 rep on question id = 1871103 Dec 15 at 4:49Inch
H
1

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.

Hardcastle answered 20/12, 2009 at 20:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.