On Mon, 25 Aug 2008 08:26:02 -0700, Ignacio Machin ( .NET/ C# MVP )
<ig************@gmail.comwrote:
On Aug 25, 10:37Â*am, "Tom P." <padilla.he...@gmail.comwrote:
>I'm trying to make one of our perennial Â*favorites - The Syntax Color
Editor. (Mostly as a learning exercise). I'm wondering if there is a
way to capture the Paint event of a textbox so I can render the text
myself.
I think that the textbox not the best base class for this. A
RichTextbox would be more suited for the task.
But in anycase, you can derive your class from Textbox and handle the
onPaint event. There are eamples of how to do it. Do a search of
"owned draw controls"
Though, it begs the question: why derive from TextBox _or_ RichTextBox in
this case, if not to let the base class do the drawing? The drawing will
be closely dependent on other control state internals, such as current
selection, scroll position, and of course the actual formatted text.
Trying to do all the drawing oneself while coordinating with these other
things would be extremely complicated and prone to error. And besides, if
doing the drawing oneself, what's the point of inheriting RichTextBox?
The main difference between that and TextBox is the visual appearance of
the text, so if doing all the drawing oneself, why bother?
I think I would do one of three things:
-- inherit RichTextBox, but rather than overriding the drawing
behavior, add new text management members that automatically format the
incoming text. Unfortunately, the original text management members can't
be overridden, and this approach would be somewhat brittle (easily
bypassed by other code and user input).
-- compose RichTextBox into a custom control, delegating text
management and rendering to the RichTextBox control. This would address
the exposure to code in the first solution, but not necessarily the user
input issues (though maybe there's a way to intercept user input to
control/block things that might mess with the composed control).
-- write your own control. This is more complicated, obviously, but
it's not an impossible effort (in fact, I'd say that once you get the
basic concepts of custom controls down, it's not all that hard...just
time-consuming). The advantage is of course that with a completely custom
control, you have complete control over the behavior of the control.
Pete