472,958 Members | 2,289 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

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 3662
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
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
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
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
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
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
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
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
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...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.