D
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
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