Current location: Hot Scripts Forums » Programming Languages » Windows .NET Programming » [SOLVED] [2005] ToolStripComboBox with Font Styles?


[SOLVED] [2005] ToolStripComboBox with Font Styles?

Reply
  #1 (permalink)  
Old 04-18-08, 05:42 PM
tim8w tim8w is offline
Wannabe Coder
 
Join Date: Nov 2004
Posts: 183
Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy [SOLVED] [2005] ToolStripComboBox with Font Styles?

I have most of the basic ideas. I'm just having problems with defining the EventHandler for ComboBox_DrawItem... Any ideas?

Code:
Public Class ctlTSFontCombo
    Inherits System.Windows.Forms.ToolStripComboBox

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        Me.DropDownStyle = ComboBoxStyle.DropDownList
        Me.DropDownWidth = 150
        Me.ComboBox.DrawMode = DrawMode.OwnerDrawVariable
        AddHandler ComboBox.DrawItem, AddressOf MyComboBox_DrawItem

        'Actually shows 8 items because ItemHeight is increade
        Me.MaxDropDownItems = 14
    End Sub

    'UserControl overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        '
        'ctlTSFontCombo
        '
        Me.Font = New System.Drawing.Font("Arial", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

    End Sub

#End Region

    Private Sub MyComboBox_DrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles MyComboBox.DrawItem
        If e.Index >= 0 Then
            Dim txt$ = Me.Items(e.Index).ToString
            Dim fnt As Font = New Font(e.Font.Name, 14, FontStyle.Bold) 'Default = legible text

            'Selected line will draw in the default font
            'All others will draw in the individual line's named font
            If e.State And DrawItemState.ComboBoxEdit Then
                fnt = e.Font    'Combobox's edit text always draws in the box's own font

            ElseIf (e.State And DrawItemState.Selected) = 0 Then
                'If not the selected line, create a custom font
                'Try all permutations of regular/bold/italic/underline/strikeout
                'If it can't do any of them, you still have the default
                Try
                    Dim ff As FontFamily = New FontFamily(txt)
                    For tStyle As FontStyle = 0 To 15
                        If (ff.IsStyleAvailable(tStyle)) Then
                            fnt = New Font(ff.Name, 14, tStyle)
                            Exit For
                        End If
                    Next
                Catch ex As Exception
                    'Gets here if font has been deleted while app is running, or other error
                End Try
            End If

            'Draw the drop-down item - you could easily adapt this to display a font icon, etc
            e.DrawBackground()
            e.DrawFocusRectangle()
            e.Graphics.DrawString(txt, fnt, New SolidBrush(e.ForeColor), e.Bounds.X, e.Bounds.Y)
        End If
    End Sub


    'Go through installed font list and add to ComboBox
    Public Sub ReloadFonts()
        Me.Items.Clear()
        For Each ff As FontFamily In System.Drawing.FontFamily.Families
            Me.Items.Add(ff.Name)
        Next
    End Sub
End Class
Reply With Quote
  #2 (permalink)  
Old 04-21-08, 11:10 AM
tim8w tim8w is offline
Wannabe Coder
 
Join Date: Nov 2004
Posts: 183
Thanks: 0
Thanked 0 Times in 0 Posts
Smile Resolved: [2005] ToolStripComboBox with Font Styles?

All I was missing was the Sender parameter...

Code:
    Private Sub MyComboBox_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles MyComboBox.DrawItem
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Looking for a script that will allow users to change the font size AND family AshleyQuick Script Requests 1 03-18-07 01:35 PM
how to install font when the page is getting accessed raji20 PHP 1 10-07-06 11:09 AM
Convert TTF Font file to AFM Font File raji20 PHP 0 08-14-06 12:23 AM
Font detection tjs Script Requests 1 07-27-06 04:54 PM
Font Script Needed Granat Networks Script Requests 0 10-28-04 08:02 PM


All times are GMT -5. The time now is 02:16 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.