Home All Groups Group Topic Archive Search About

Rewriting the Textbox

Author
27 Jun 2006 8:01 AM
Dave
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 RTF as my superscript
and subscript's are always numbers and are defined as specific
characters.  ie. char(192) = superscript 1

Does anyone know how to inherit and change the onPaint method of the
textbox?  I have created my own controlt o try and do this but
apparently the onPaint method is never called:

Public Class SuperTextBox
    Inherits TextBox

    Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
        Dim sf As New StringFormat
        sf.LineAlignment = StringAlignment.Center

        e.Graphics.FillRectangle(New SolidBrush(Me.BackColor),
e.ClipRectangle)

        Dim strToPrint As String = Me.Text
        Dim boundLeft As Integer = e.ClipRectangle.Left

        For i As Integer = 0 To strToPrint.Length - 1
            If
clsSuperScriptHandler.isSubScript(CChar(strToPrint.Substring(i, 1)))
Then
                'subscript
                Dim f As New Font(me.Font.FontFamily, _
                                  me.Font.Size - 3, _
                                  Me.Font.Style, _
                                  Me.Font.Unit)

                e.Graphics.DrawString(strToPrint.Substring(i, 1), f, _
                             New SolidBrush(Me.ForeColor), New
RectangleF(boundLeft, e.ClipRectangle.Y + 5, e.ClipRectangle.Width,
e.ClipRectangle.Height), sf)
                boundLeft +=
CInt(e.Graphics.MeasureString(strToPrint.Substring(i, 1), f).Width *
0.7)
            ElseIf
clsSuperScriptHandler.isSuperScript(CChar(strToPrint.Substring(i, 1)))
Then
                'superscript
                Dim f As New Font(Me.Font.FontFamily, _
                                  Me.Font.Size - 3, _
                                  Me.Font.Style, _
                                  Me.Font.Unit)

                e.Graphics.DrawString(strToPrint.Substring(i, 1), f, _
                                      New SolidBrush(Me.ForeColor), New
RectangleF(boundLeft, e.ClipRectangle.Y - 5, e.ClipRectangle.Width,
e.ClipRectangle.Height), sf)
                boundLeft +=
CInt(e.Graphics.MeasureString(strToPrint.Substring(i, 1), f).Width *
0.7)
            Else
                'normal script
                e.Graphics.DrawString(strToPrint.Substring(i, 1),
Me.Font, _
                        New SolidBrush(Me.ForeColor), New
RectangleF(boundLeft, e.ClipRectangle.Y, e.ClipRectangle.Width,
e.ClipRectangle.Height), sf)
                boundLeft +=
CInt(e.Graphics.MeasureString(strToPrint.Substring(i, 1),
Me.Font).Width * 0.7)
            End If
        Next
    End Sub
End Class

Thanks

Dave

Author
27 Jun 2006 9:13 AM
Larry Lard
Dave wrote:
> 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 RTF as my superscript
> and subscript's are always numbers and are defined as specific
> characters.  ie. char(192) = superscript 1
>
> Does anyone know how to inherit and change the onPaint method of the
> textbox?  I have created my own controlt o try and do this but
> apparently the onPaint method is never called:

You don't say which VB you're using, but one clue that there's going to
be a problem is given in VB2005 when you type "overrides" in your new
class, and OnPaint isn't in the list. After a little wandering through
the docs, I found this:

<http://msdn2.microsoft.com/en-us/library/7h62478z.aspx>
"
Note

Overriding OnPaint will not allow you to modify the appearance of all
controls. Those controls that have all of their painting done by
Windows (for example, TextBox) never call their OnPaint method, and
thus will never use the custom code. Refer to the Help documentation
for the particular control you want to modify to see if the OnPaint
method is available. For a list of all the Windows Form Controls, see
Controls to Use on Windows Forms. If a control does not have OnPaint
listed as a member method, you cannot alter its appearance by
overriding this method. For more information about custom painting, see
Custom Control Painting and Rendering.
"

I'm not sure what the best way to proceed would be; ideally you want to
avoid having to do all the rendering yourself, but I think you may have
to. It's not that hard, just a bit fiddly.

--
Larry Lard
Replies to group please
Author
27 Jun 2006 9:54 AM
Dave
> I'm not sure what the best way to proceed would be; ideally you want to
> avoid having to do all the rendering yourself, but I think you may have
> to. It's not that hard, just a bit fiddly.

Dont suppose you have any examples do you?
Author
27 Jun 2006 3:05 PM
Claes Bergefall
Add this:

Public Sub New()
    MyBase.SetStyle(System.Windows.Forms.ControlStyles.AllPaintingInWmPaint,
True)
    MyBase.SetStyle(System.Windows.Forms.ControlStyles.DoubleBuffer, True)
    MyBase.SetStyle(System.Windows.Forms.ControlStyles.UserPaint, True)
End Sub

    /claes

Show quoteHide quote
"Dave" <davesa***@gmail.com> wrote in message
news:1151395270.629843.321290@i40g2000cwc.googlegroups.com...
> 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 RTF as my superscript
> and subscript's are always numbers and are defined as specific
> characters.  ie. char(192) = superscript 1
>
> Does anyone know how to inherit and change the onPaint method of the
> textbox?  I have created my own controlt o try and do this but
> apparently the onPaint method is never called:
>
> Public Class SuperTextBox
>    Inherits TextBox
>
>    Protected Overrides Sub OnPaint(ByVal e As
> System.Windows.Forms.PaintEventArgs)
>        Dim sf As New StringFormat
>        sf.LineAlignment = StringAlignment.Center
>
>        e.Graphics.FillRectangle(New SolidBrush(Me.BackColor),
> e.ClipRectangle)
>
>        Dim strToPrint As String = Me.Text
>        Dim boundLeft As Integer = e.ClipRectangle.Left
>
>        For i As Integer = 0 To strToPrint.Length - 1
>            If
> clsSuperScriptHandler.isSubScript(CChar(strToPrint.Substring(i, 1)))
> Then
>                'subscript
>                Dim f As New Font(me.Font.FontFamily, _
>                                  me.Font.Size - 3, _
>                                  Me.Font.Style, _
>                                  Me.Font.Unit)
>
>                e.Graphics.DrawString(strToPrint.Substring(i, 1), f, _
>                             New SolidBrush(Me.ForeColor), New
> RectangleF(boundLeft, e.ClipRectangle.Y + 5, e.ClipRectangle.Width,
> e.ClipRectangle.Height), sf)
>                boundLeft +=
> CInt(e.Graphics.MeasureString(strToPrint.Substring(i, 1), f).Width *
> 0.7)
>            ElseIf
> clsSuperScriptHandler.isSuperScript(CChar(strToPrint.Substring(i, 1)))
> Then
>                'superscript
>                Dim f As New Font(Me.Font.FontFamily, _
>                                  Me.Font.Size - 3, _
>                                  Me.Font.Style, _
>                                  Me.Font.Unit)
>
>                e.Graphics.DrawString(strToPrint.Substring(i, 1), f, _
>                                      New SolidBrush(Me.ForeColor), New
> RectangleF(boundLeft, e.ClipRectangle.Y - 5, e.ClipRectangle.Width,
> e.ClipRectangle.Height), sf)
>                boundLeft +=
> CInt(e.Graphics.MeasureString(strToPrint.Substring(i, 1), f).Width *
> 0.7)
>            Else
>                'normal script
>                e.Graphics.DrawString(strToPrint.Substring(i, 1),
> Me.Font, _
>                        New SolidBrush(Me.ForeColor), New
> RectangleF(boundLeft, e.ClipRectangle.Y, e.ClipRectangle.Width,
> e.ClipRectangle.Height), sf)
>                boundLeft +=
> CInt(e.Graphics.MeasureString(strToPrint.Substring(i, 1),
> Me.Font).Width * 0.7)
>            End If
>        Next
>    End Sub
> End Class
>
> Thanks
>
> Dave
>