Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

The need to use DrawMode.OwnerDrawVariable comes along so infrequently that I thought I'd whip up a little code for what divil's talking about.

 

   Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
'set the drawmode, so the DrawItem and MeasureItem events will both fire...
       ComboBox1.DrawMode = DrawMode.OwnerDrawVariable

       FillComboBox()
   End Sub

   Private Sub FillComboBox()
       Dim item1, item2, item3, item4 As String
       item1 = "This is the first item."
       item2 = "This is the second item.  Hopefully it will be " & _
       "too long to fit on a single line, so it will word-wrap " & _
       "within a single item in our ComboBox."
       item3 = "This is the third item.  It is very lengthly " & _
       "indeed.  The Recording Industry Association of America will " & _
       "extend an amnesty program to some individuals involved in " & _
       "illegal sharing of copyrighted music files online, according " & _
       "to numerous published reports."
       item4 = "And this is the fourth item"
       Dim items() As String = {item1, item2, item3, item4}

       ComboBox1.Items.AddRange(items)

   End Sub

   Private Sub ComboBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) _
Handles ComboBox1.DrawItem

       Try
           If (e.State And DrawItemState.Selected) Then
               e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds)
               e.Graphics.DrawString(ComboBox1.Items(e.Index), Me.Font, SystemBrushes.HighlightText, _
New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))   

           Else
               e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds)

               e.Graphics.DrawString(ComboBox1.Items(e.Index), Me.Font, SystemBrushes.ControlText, _
New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)) 


           End If
       Catch ex As Exception
           Debug.WriteLine("Index:  " & e.Index & "   " & ex.Message)

       End Try
   End Sub

   Private Sub ComboBox1_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) _
Handles ComboBox1.MeasureItem
       e.ItemHeight = e.Graphics.MeasureString(ComboBox1.Items(e.Index), Me.Font, ComboBox1.Width).Height

   End Sub

Edited by ballisticnylon

"It may be roundly asserted that human ingenuity cannot concoct a cipher which human ingenuity cannot resolve."

 

- Edgar Allan Poe, 1841

 

I long to accomplish great and noble tasks, but it is my chief duty to accomplish humble tasks as though they were great and noble. The world is moved along, not only by the mighty shoves of its heroes, but also by the aggregate of the tiny pushes of each honest worker.

 

- Helen Keller

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...