Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Building a custom combobox 1

Status
Not open for further replies.

fawkes

Technical User
Sep 12, 2003
343
GB
Using vb.net 2005

Has anyone ever built a custom combobox?

I want to have html text displayed in a windowforms combobox as if it were displayed in a web browser.

Has anyone ever had a go at this?

Has anyone ever heard of anything like this?
 
Hello fawkes,

There are several examples on Perhaps one of them would help you. Here is a link to one which does some pretty intensive repainting: think you will need to override the paint methods to do what you want to do.

Here is a link about getting at the inner textbox in a combobox:
Have a great day!

j2consulting@yahoo.com
 
Star for you, definitely in the right direction.

I'm trying to display measurement symbols (e.g. m^3) in a combo box. I could do most using utf characters but not superscript 4 so I needed to use the html <sup> tag.

Here's what I've got so far

Code:
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

What I need to figure out now is how to draw the text after it has been selected, I can't catch any event that will allow me to draw the text or I'm trying to draw it using the wrong code.

Any more ideas would be graetfully received.

Incidentally, I used your two links and
(Always best to give credit where credit's due)
 
Think I've got it

Code:
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

The last problem comes down to the drop down style.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top