Click here to Skip to main content
15,896,557 members
Articles / Programming Languages / Visual Basic

Mathemathics Framework

Rate me:
Please Sign up or sign in to vote.
4.76/5 (56 votes)
16 Sep 2008CPOL6 min read 75.5K   6.2K   171  
.NET Mathematical Framework
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Drawing
Imports System.Data
Imports System.Windows.Forms


Namespace FontCombo
   
   Public Class FontComboBox
      Inherits ComboBox
      
      Public Sub New()
         MaxDropDownItems = 20
         IntegralHeight = False
         Sorted = False
         DropDownStyle = ComboBoxStyle.DropDownList
         DrawMode = DrawMode.OwnerDrawVariable
      End Sub 'New
      
      
      Public Sub Populate(b As Boolean)
         both = b
         Dim ff As FontFamily
         For Each ff In  FontFamily.Families
            If ff.IsStyleAvailable(FontStyle.Regular) Then
               Items.Add(ff.Name)
            End If
         Next ff 
         If Items.Count > 0 Then
            SelectedIndex = 0
         End If
         ttimg = New Bitmap([GetType](), "ttfbmp.bmp")
      End Sub 'Populate
      
      
      Protected Overrides Sub OnMeasureItem(e As System.Windows.Forms.MeasureItemEventArgs)
         If e.Index > - 1 Then
            Dim w As Integer = 0
            Dim fontstring As String = Items(e.Index).ToString()
            Dim g As Graphics = CreateGraphics()
            e.ItemHeight = CInt(g.MeasureString(fontstring, New Font(fontstring, 10)).Height)
            w = CInt(g.MeasureString(fontstring, New Font(fontstring, 10)).Width)
            If both Then
               Dim h1 As Integer = CInt(g.MeasureString(samplestr, New Font(fontstring, 10)).Height)
               Dim h2 As Integer = CInt(g.MeasureString(Items(e.Index).ToString(), New Font("Arial", 10)).Height)
               Dim w1 As Integer = CInt(g.MeasureString(samplestr, New Font(fontstring, 10)).Width)
               Dim w2 As Integer = CInt(g.MeasureString(Items(e.Index).ToString(), New Font("Arial", 10)).Width)
               If h1 > h2 Then
                  h2 = h1
               End If
               e.ItemHeight = h2
               w = w1 + w2
            End If
            w += ttimg.Width * 2
            If w > maxwid Then
               maxwid = w
            End If
            If e.ItemHeight > 20 Then
               e.ItemHeight = 20
            End If
         End If 
         
         
         MyBase.OnMeasureItem(e)
      End Sub 'OnMeasureItem
      
      
      Protected Overrides Sub OnDrawItem(e As System.Windows.Forms.DrawItemEventArgs)
         If e.Index > - 1 Then
            Dim fontstring As String = Items(e.Index).ToString()
            nfont = New Font(fontstring, 10)
            Dim afont As New Font("Arial", 10)
            
            If both Then
               Dim g As Graphics = CreateGraphics()
               Dim w As Integer = CInt(g.MeasureString(fontstring, afont).Width)
               
               If(e.State And DrawItemState.Focus) = 0 Then
                  e.Graphics.FillRectangle(New SolidBrush(SystemColors.Window), e.Bounds.X + ttimg.Width, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)
                  e.Graphics.DrawString(fontstring, afont, New SolidBrush(SystemColors.WindowText), e.Bounds.X + ttimg.Width * 2, e.Bounds.Y)
                  e.Graphics.DrawString(samplestr, nfont, New SolidBrush(SystemColors.WindowText), e.Bounds.X + w + ttimg.Width * 2, e.Bounds.Y)
               Else
                  e.Graphics.FillRectangle(New SolidBrush(SystemColors.Highlight), e.Bounds.X + ttimg.Width, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)
                  e.Graphics.DrawString(fontstring, afont, New SolidBrush(SystemColors.HighlightText), e.Bounds.X + ttimg.Width * 2, e.Bounds.Y)
                  e.Graphics.DrawString(samplestr, nfont, New SolidBrush(SystemColors.HighlightText), e.Bounds.X + w + ttimg.Width * 2, e.Bounds.Y)
               End If
            Else
               
               If(e.State And DrawItemState.Focus) = 0 Then
                  e.Graphics.FillRectangle(New SolidBrush(SystemColors.Window), e.Bounds.X + ttimg.Width, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)
                  e.Graphics.DrawString(fontstring, nfont, New SolidBrush(SystemColors.WindowText), e.Bounds.X + ttimg.Width * 2, e.Bounds.Y)
               
               Else
                  e.Graphics.FillRectangle(New SolidBrush(SystemColors.Highlight), e.Bounds.X + ttimg.Width, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)
                  e.Graphics.DrawString(fontstring, nfont, New SolidBrush(SystemColors.HighlightText), e.Bounds.X + ttimg.Width * 2, e.Bounds.Y)
               End If
            End If
            
            
            e.Graphics.DrawImage(ttimg, New Point(e.Bounds.X, e.Bounds.Y))
         End If
         MyBase.OnDrawItem(e)
      End Sub 'OnDrawItem
      
      Private nfont As Font
      Private both As Boolean = False
      Private maxwid As Integer = 0
      Private samplestr As String = " - Hello World"
      Private ttimg As Image
      
      
      Protected Overrides Sub OnDropDown(e As System.EventArgs)
         Me.DropDownWidth = maxwid + 30
      End Sub 'OnDropDown
   End Class 'FontComboBox 
End Namespace 'FontCombo

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer Universidad Tecnológica Nacional
Argentina Argentina
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions