473,511 Members | 17,577 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to capture the OnPaint of a textbox

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've tried to replicate the functionality of a textbox, but I think it
would be easier to simply take over the painting of a plain textbox.
Then I wouldn't have to deal with several problem areas I'm currently
running into.

Thanks for any help.

Tom P.
Aug 25 '08 #1
6 3717
On Aug 25, 7: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.
Unless the Textbox class has a .Draw method associated with it, as
ImageList does for example, I would imagine it's hard to do this, but
I'm just guessing.

RL

Aug 25 '08 #2
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"
Aug 25 '08 #3
On Aug 25, 10:26*am, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac...@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"
Have you ever tried it? I did last night and the OnPaint method never
gets called (RTEdit as well). There is no OwnerDraw attribute for
TextBox and the best I can do is call InvokePaint and
InvokePaintBackground when the text changes. This may be what I'm left
with unless I can get this question answered. Or trap the WndPrc
message and parse it.

Thanks,
Tom P.
Aug 25 '08 #4
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
Aug 25 '08 #5
On Aug 25, 11:11*am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Mon, 25 Aug 2008 08:26:02 -0700, Ignacio Machin ( .NET/ C# MVP ) *

<ignacio.mac...@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
I had already started down the path to writing a custom control,
inheriting simply from the Control object, when a friend of mine
suggested the TextBox thing (i am having issues placing the carat but
it's just a matter of math and string length, separating text into
lines etc.).

I think you are correct that, oddly enough, the simplest thing to do
is skip the textbox altogether and write my own. This will also give
me space to do things I want like gradient backgrounds (I heard they
are easier on the eyes than flat white).

Tom P.
Aug 25 '08 #6
On Mon, 25 Aug 2008 08:56:34 -0700, Tom P. <pa***********@gmail.comwrote:
[...]
>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"

Have you ever tried it? I did last night and the OnPaint method never
gets called (RTEdit as well).
For many of the built-in Forms controls, even if OnPaint() is called (and
as you've seen, often it's not), that's not where the drawing actually
happens, because the control is just a wrapper for an unmanaged control.
You'd have to override WndProc and intercept WM_PAINT messages to actually
override the drawing.

Pete
Aug 25 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
2548
by: BB | last post by:
Hello all, I am trying to override OnPaint in a custom textbox control (so I can drawstring a caption, etc.). In the code below, I get the "painting the form" message as expected, but not the...
3
1583
by: Gabor | last post by:
I want to capture the textbox ongotfocus event, but it is missing in the event list. I found the Enter event, but it seems doesn't work. What I'm doing wrog? Thanks in advance Gabor
3
2642
by: Dave | last post by:
Hey all, Ok, here's another of my fun questions. I want to rewrite the textbox control in VB.NET. I need to implement superscript and subscript within the box. Don't ask why, but I can't use...
8
5062
by: Filipe Marcelino | last post by:
Hi, I'm trying to create a textbox inheriting from the standard textbox. I would like to: 1. repaint the textbox border; 2. define a color for that border; Till now I made this:
4
4590
by: H-S | last post by:
Please help. This is a real puzzler! Originally posted on microsoft.public.dotnet.framework.windowsforms but no answer found! I have a read-only textBox which shows the results of a selection...
4
5997
by: giddy | last post by:
hi when i run this class i made here , this is what it looks like without text - http://gidsfiles.googlepages.com/LinedTextBox_1.jpg WITH TEXT (heres the issue) -...
6
279
by: chenhong | last post by:
I have a aspx page which has a textbox control and a button control. I added some code in the button's OnClick event. When I add some text in the textbox and hit ENTER,the page postback and the...
8
2048
by: Freddy Coal | last post by:
Hi, I would like make a search and replace in a file, for that I need get the text in a textbox, but I need recognyze literally characters of the user like a commands, for example: The chain of...
0
7245
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7427
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7512
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5671
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3214
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1577
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
449
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.