JayBeeOH

Fonts ComboBox Demo

Aug 26th, 2016
451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.35 KB | None | 0 0
  1. '------------------------------------------------------------------------------------------
  2. '           Notice of My Copyright and Intellectual Property Rights
  3. '
  4. ' Any intellectual property contained within the program by Joseph L. Bolen remains the
  5. ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
  6. ' publish or provide such intellectual property to any other person or entity for any
  7. ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
  8. '
  9. '                 Copyright © 2016. All rights reserved.
  10. '        All trademarks remain the property of their respective owners.
  11. '-------------------------------------------------------------------------------------------
  12. ' Program Name:   Fonts ComboBox Demo
  13. '
  14. ' Author:         Joseph L. Bolen
  15. ' Date Created:   26 AUG 2016
  16. '
  17. ' Description:    Show installed fonts in combobox.
  18. '
  19. '                 Documentation is at:
  20. '                   App's screen image is at: http://imgur.com/tYymDwQ
  21. '                   App's Visual Basic .NET code is at http://pastebin.com/q2K1VGsy
  22. '                   Video tutorial at YouTube: http://www.youtube.com/user/bolenpresents
  23. '-------------------------------------------------------------------------------------------
  24.  
  25. Public Class MainForm
  26.  
  27.     Private Sub MainForm_Load(sender As Object, e As EventArgs) _
  28.         Handles MyBase.Load
  29.  
  30.         ' Get the installed fonts and load the combo box.
  31.         Dim installedFonts As New System.Drawing.Text.InstalledFontCollection
  32.         With FontsComboBox
  33.             ' Allow user to draw control.
  34.             .DrawMode = DrawMode.OwnerDrawFixed
  35.             ' If you wish to control MaxDropDownItems, set IntegralHeight property to false.
  36.             .IntegralHeight = False
  37.             .MaxDropDownItems = 8
  38.             .DataSource = (From fontFamiles In installedFonts.Families Select fontFamiles.Name).ToList
  39.             .SelectedItem = FontExampleLabel.Font.Name
  40.         End With
  41.         AddHandler FontsComboBox.SelectedIndexChanged, AddressOf FontsComboBox_SelectedIndexChanged
  42.     End Sub
  43.  
  44.     ' Change the font of label when FontsComboBox changes value.
  45.     Private Sub FontsComboBox_SelectedIndexChanged(sender As Object, e As EventArgs)
  46.  
  47.         If FontsComboBox.SelectedIndex <> -1 Then
  48.             FontExampleLabel.Font =
  49.                 New Font(FontsComboBox.SelectedItem.ToString, FontExampleLabel.Font.Size)
  50.         End If
  51.     End Sub
  52.  
  53.     ' Draw each item in the control's collection with the item's font.
  54.     Private Sub FontsComboBox_DrawItem(sender As Object, e As DrawItemEventArgs) _
  55.         Handles FontsComboBox.DrawItem
  56.  
  57.         Dim myComboBox As ComboBox = CType(sender, ComboBox)
  58.         Dim myFont As New Font(CType(myComboBox.Items(e.Index), String), myComboBox.Font.Size)
  59.  
  60.         ' Draw the background of the ComboBox control for each item.
  61.         e.DrawBackground()
  62.  
  63.         ' Define the default color of the brush.
  64.         Using mybrush As New SolidBrush(e.ForeColor)
  65.             ' Draw the current item text based on the current font and brush settings.
  66.             e.Graphics.DrawString(myFont.Name, myFont, mybrush, e.Bounds, StringFormat.GenericDefault)
  67.         End Using
  68.  
  69.         ' If the control has focus, draw a focus rectangle around the selected item.
  70.         e.DrawFocusRectangle()
  71.     End Sub
  72.  
  73. End Class
Advertisement
Add Comment
Please, Sign In to add comment