Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Public Class superScriptComboBoxItem
Inherits System.Windows.Forms.ComboBox
''' <summary>Basic constructor</summary>
''' <remarks>Call the base class New method and set the draw mode to owner fixed</remarks>
Public Sub New()
MyBase.New()
Me.DrawMode = Windows.Forms.DrawMode.OwnerDrawFixed
End Sub
''' <summary>Handles the DrawItem event</summary>
''' <param name="sender">The control raising the event</param>
''' <param name="e">The event arguments</param>
''' <remarks>Calls the DrawText method to draw the text</remarks>
Private Sub m_ComboBox_DrawItem(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DrawItemEventArgs) _
Handles Me.DrawItem 'm_ComboBox.DrawItem
Me.DrawText(e.Graphics, Me.Items.Item(e.Index), e.Bounds)
End Sub
''' <summary>Draws text with superscript tags as superscript.</summary>
''' <param name="g">The graphics object wher the text will be drawn</param>
''' <param name="text">The text to draw, complete with html superscript tags</param>
''' <param name="bounds">The bounds of the graphic area</param>
''' <remarks>
''' Takes text containing normal and superscript text in html form, i.e. held within
''' sup tags (but no other tags included anywhere) and siaplys the text as normal and
''' superscript text.
''' </remarks>
Private Sub DrawText(ByVal g As Graphics, ByVal text As String, _
ByVal bounds As System.Drawing.Rectangle)
'Create a font for normal text
Dim normFont As Font = Me.Font
'Create a font for superscript text with a font hieght of 40%
Dim supFont As New Font(Me.Font.FontFamily, Me.Font.Height * 0.4, _
FontStyle.Regular, Me.Font.Unit)
'Calculate the difference in heights in single
Dim heightDiff As Single = normFont.Height - supFont.Height
'Loop through the items looking for <sup> and </sup> tags
Dim itemText As String = text
'Calculate the position of any superscript tags
Dim supStart, supEnd As Integer
supStart = itemText.IndexOf("<sup>")
supEnd = itemText.IndexOf("</sup>")
'Declare a variable to hold the text position
Dim textPos As Single = bounds.X
While supStart > 0
'Collect the normal and superscript text
Dim normText, supText As String
'Draw the normal text before the <sup> tag
normText = itemText.Substring(0, supStart)
'Draw the superscript text in the <sup> tag
supText = itemText.Substring(supStart + 5, supEnd - supStart - 5)
'Draw the normal text
g.DrawString(normText, Me.Font, Brushes.Black, textPos, bounds.Y)
'Calculate the next text position
textPos += g.MeasureString(normText, normFont).Width
'Draw the superscript text
g.DrawString(supText, supFont, Brushes.Black, textPos, bounds.Y)
'Calculate the next text position
textPos += g.MeasureString(supText, supFont).Width
'Trim the item text
itemText = itemText.Remove(0, supEnd + 6)
'Recalculate the position of any superscript tags
supStart = itemText.IndexOf("<sup>")
supEnd = itemText.IndexOf("</sup>")
End While
'Draw the any remaining text
g.DrawString(itemText, Me.Font, Brushes.Black, textPos, bounds.Y)
End Sub
End Class
Public Class superScriptComboBoxItem
Inherits System.Windows.Forms.ComboBox
''' <summary>Basic constructor</summary>
''' <remarks>
''' Call the base class New method and set the draw mode to owner fixed and the
''' drop down style to DropDownList.
''' </remarks>
Public Sub New()
MyBase.New()
Me.DrawMode = Windows.Forms.DrawMode.OwnerDrawFixed
Me.DropDownStyle = ComboBoxStyle.DropDownList
End Sub
''' <summary>Handles the DropDownStyleChanged event</summary>
''' <param name="sender">The object raising the event</param>
''' <param name="e">The event arguments</param>
''' <remarks>
''' Regardles of what the style has been set to it is always rest to DropDownList
''' which is required to maintain the style in which the items are drawn.
''' </remarks>
Private Sub superScriptComboBoxItem_DropDownStyleChanged( _
ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles Me.DropDownStyleChanged
'Remove this handler
RemoveHandler Me.DropDownStyleChanged, _
AddressOf Me.superScriptComboBoxItem_DropDownStyleChanged
'Set the drop down style to DropDownList
Me.DropDownStyle = ComboBoxStyle.DropDownList
'Restore this handler
AddHandler Me.DropDownStyleChanged, _
AddressOf Me.superScriptComboBoxItem_DropDownStyleChanged
End Sub
''' <summary>Handles the DrawItem event</summary>
''' <param name="sender">The control raising the event</param>
''' <param name="e">The event arguments</param>
''' <remarks>Calls the DrawText method to draw the text</remarks>
Private Sub m_ComboBox_DrawItem(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DrawItemEventArgs) _
Handles Me.DrawItem
If e.Index > -1 Then
Me.DrawText(e.Graphics, Me.Items.Item(e.Index), e.Bounds)
End If
End Sub
''' <summary>Draws text with superscript tags as superscript.</summary>
''' <param name="g">The graphics object wher the text will be drawn</param>
''' <param name="text">The text to draw, complete with html superscript tags</param>
''' <param name="bounds">The bounds of the graphic area</param>
''' <remarks>
''' Takes text containing normal and superscript text in html form, i.e. held within
''' sup tags (but no other tags included anywhere) and siaplys the text as normal and
''' superscript text.
''' </remarks>
Private Sub DrawText(ByVal g As Graphics, ByVal text As String, _
ByVal bounds As System.Drawing.Rectangle)
'Create a font for normal text
Dim normFont As Font = Me.Font
'Create a font for superscript text with a font hieght of 40%
Dim supFont As New Font(Me.Font.FontFamily, Me.Font.Height * 0.4, _
FontStyle.Regular, Me.Font.Unit)
'Calculate the difference in heights in single
Dim heightDiff As Single = normFont.Height - supFont.Height
'Loop through the items looking for <sup> and </sup> tags
Dim itemText As String = text
'Calculate the position of any superscript tags
Dim supStart, supEnd As Integer
supStart = itemText.IndexOf("<sup>")
supEnd = itemText.IndexOf("</sup>")
'Declare a variable to hold the text position
Dim textPos As Single = bounds.X
While supStart > 0
'Collect the normal and superscript text
Dim normText, supText As String
'Draw the normal text before the <sup> tag
normText = itemText.Substring(0, supStart)
'Draw the superscript text in the <sup> tag
supText = itemText.Substring(supStart + 5, supEnd - supStart - 5)
'Draw the normal text
g.DrawString(normText, Me.Font, Brushes.Black, textPos, bounds.Y)
'Calculate the next text position
textPos += g.MeasureString(normText, normFont).Width
'Draw the superscript text
g.DrawString(supText, supFont, Brushes.Black, textPos, bounds.Y)
'Calculate the next text position
textPos += g.MeasureString(supText, supFont).Width
'Trim the item text
itemText = itemText.Remove(0, supEnd + 6)
'Recalculate the position of any superscript tags
supStart = itemText.IndexOf("<sup>")
supEnd = itemText.IndexOf("</sup>")
End While
'Draw the any remaining text
g.DrawString(itemText, Me.Font, Brushes.Black, textPos, bounds.Y)
End Sub
End Class