1 Attachment(s)
[RESOLVED] [2008] Override OnPaint of a Textbox and a Button
Hi!!
In the Attachment I have a project very simple, just a form and a two classes: the TestTB and the TestB. The testTB class Inherits from the TextBox class and the TestB Inherits from the Button class. And the only sub that they have is this one:
Code:
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
e.Graphics.DrawString("1", Me.Font, New SolidBrush(Me.ForeColor), 3, 1)
e.Graphics.DrawString("2", Me.Font, New SolidBrush(Me.ForeColor), 13, 1)
e.Graphics.DrawString("3", Me.Font, New SolidBrush(Me.ForeColor), 23, 1)
e.Graphics.DrawString("4", Me.Font, New SolidBrush(Me.ForeColor), 3, 13)
e.Graphics.DrawString("5", Me.Font, New SolidBrush(Me.ForeColor), 13, 13)
e.Graphics.DrawString("6", Me.Font, New SolidBrush(Me.ForeColor), 23, 13)
e.Graphics.DrawString("7", Me.Font, New SolidBrush(Me.ForeColor), 3, 25)
e.Graphics.DrawString("8", Me.Font, New SolidBrush(Me.ForeColor), 13, 25)
e.Graphics.DrawString("9", Me.Font, New SolidBrush(Me.ForeColor), 23, 25)
End Sub
In the form i have a TestTB control and a TestB control. Now my question is why in hell doesn't the TestTB control shows the little numbers like the TestB control??
Re: [2008] Override OnPaint of a Textbox and a Button
Because by default the paint event is never fired for a textbox, as the painting is handled by the operating system directly. You will have to set owner drawn, but this will mean that you also have to manually draw the rest of the textbox as well.
Re: [2008] Override OnPaint of a Textbox and a Button
I cant find either OwnerDraw pr DrawMode in the textbox proprieties...
Re: [2008] Override OnPaint of a Textbox and a Button
You'll have to do something like this:
Code:
Public Class MyTextBox
Inherits TextBox
Public Sub New()
SetStyle(ControlStyles.UserPaint, True)
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
e.Graphics.DrawString("1", Me.Font, New SolidBrush(Me.ForeColor), 3, 1)
End Sub
End Class
luckily, it turns out that you don't have to manually draw the rest of the textbox, at least not in vb.
Re: [2008] Override OnPaint of a Textbox and a Button
Re: [RESOLVED] [2008] Override OnPaint of a Textbox and a Button
Just one thing I used ur code, and it works wonderfully, my problem is the following: I changed the size of the font to 20, but its like I havent changed it at all, I mean the fonts still stays with the 8.25 default value, how can I overcome this? Or why this happens? And where is the "real" value of the font he's using?
Re: [2008] Override OnPaint of a Textbox and a Button
JMC posted a nice post about that I think. He talked about how a textbox is text only, whereas a RTF (rich textbox) contains text + metadata about the text (ie, the "HTML" for it).
Try switching to a rich text control.
Re: [2008] Override OnPaint of a Textbox and a Button
Didnt work with the richtextbox, anyone got any ideias how to solve this?
Re: [2008] Override OnPaint of a Textbox and a Button
What exactly are you trying to accomplish here? Coloured text? If so, the richtextbox is the way to go.
Re: [2008] Override OnPaint of a Textbox and a Button
Quote:
anyone got any ideias how to solve this?
If also a stupid idea is valid...
I have not tried this, but I'm considering that a non multiline Textbox height is tied to its font height. I don't know if it could work in a bidirectional way. The first thing I'd try is to use a multiline textbox with a sufficient height to display the desired font. Only to begin to understand if the problem is in that direction. Anyway it should be an absolutely stupid suggestion!:o
Re: [2008] Override OnPaint of a Textbox and a Button
Nice idea. The problem is that I have from the start a multiline textbox with a sufficient height to display the desired font. So I cant be cause of that.
Re: [2008] Override OnPaint of a Textbox and a Button
Given how this thread got started (from another thread and a PM), I would suggest that what you're trying to force a textbox to do could be done with another control, of which you do mention the button control here too.
What you're trying to do is show small "available" numbers on the Textbox control and also have the TB accept the numeric input, however the TB simply wasn't designed to allow this type of interaction without a huge amount of re-coding of the Textbox. IE you're going to end up with a whole new control in the end and have put in who knows how many overrides to get this result. Now what I would suggest is for you to step back, look at how much work is involved here and take a look at how much work it would be to just use a different control for this, a button is an example.
I simply used a button for this since, a button has the physical space for custom drawing, it also has all of the click event stuff set up and it's easy to capture key presses and when a valid key is pressed all the "available" numbers don't need to be displayed anymore.
Just some things to think about.
Re: [2008] Override OnPaint of a Textbox and a Button
you could try looking at this, dont know if its what you need :-/ ?
a cstm textbox i made, not the final version but it shows how to draw on it ;)
http://www.vbforums.com/showthread.php?t=537317
Re: [2008] Override OnPaint of a Textbox and a Button
Quote:
Originally Posted by JuggaloBrotha
What you're trying to do is show small "available" numbers on the Textbox control and also have the TB accept the numeric input, however the TB simply wasn't designed to allow this type of interaction without a huge amount of re-coding of the Textbox. IE you're going to end up with a whole new control in the end and have put in who knows how many overrides to get this result.
Well, I dont give up, so I got what I wanted!! Yes I had to make a new control, but I only had to make 5 overrides, with very few lines of code each.
I read in the forum here to fix the bug I had to do add this lineI changed to
Code:
Me.Font = New Font("Verdana", 20, FontStyle.Regular, GraphicsUnit.Point)
and still work so my problem was solved.
This font bug was probably something microsoft didnt saw, cause if you Inherit a Textbox and Userpaint it then the Font property becomes useless and doesnt matter what value you give it in design mode cause it will keep default value (Microsoft Sans Sherif 8.25)
Re: [2008] Override OnPaint of a Textbox and a Button
Quote:
Originally Posted by Lasering
I read in the forum
here to fix the bug I had to do add this line
I changed to
Code:
Me.Font = New Font("Verdana", 20, FontStyle.Regular, GraphicsUnit.Point)
and still work so my problem was solved.
This font bug was probably something microsoft didnt saw, cause if you Inherit a Textbox and Userpaint it then the Font property becomes useless and doesnt matter what value you give it in design mode cause it will keep default value (Microsoft Sans Sherif 8.25)
If you're doing the custom drawing in the paint even, you should use the control's Font property anyways, unless you provide an additional font property for them. By doing it this way you give the developer that's using your control the ability to change the font in your control which provides additional flexibility.
I'm also curious to see this new control of yours, perhaps you could provide it in a new thread in the CodeBank part of the forum.