Results 1 to 4 of 4

Thread: Using DrawString in ComboBox onDrawItem

  1. #1

    Thread Starter
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Using DrawString in ComboBox onDrawItem

    I'm confused on this because this works fine in VS 2008 on winXP sp2/sp3 but in VS 2008 on Vista sp1/sp2 I'm getting an Invalid Parameter exception on this line:
    Code:
    e.Graphics.DrawString(aColor.Name, Me.Font, br, e.Bounds.Height + 5I, ((e.Bounds.Height - Me.Font.Height) \ 2I) + e.Bounds.Top)
    Here' the whole sub:
    Code:
        Protected Overrides Sub onDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
            e.DrawBackground()
            e.DrawFocusRectangle()
            If e.Index >= 0I Then
                ' Get the Color object from the Items list
                Dim aColor As Color = CType(Me.Items(e.Index), Color)
                ' get a square using the bounds height
                Dim rect As Rectangle = New Rectangle(5I, e.Bounds.Top + 2I, e.Bounds.Height - 3I, e.Bounds.Height - 5I)
                Dim br As Brush = If((e.State And DrawItemState.Selected) = DrawItemState.Selected, Brushes.White, Brushes.Black)
                ' draw a rectangle and fill it
                Using pn As New Pen(aColor)
                    e.Graphics.DrawRectangle(pn, rect)
                End Using
                Using b As New SolidBrush(aColor)
                    e.Graphics.FillRectangle(b, rect)
                End Using
                ' draw a border
                rect.Inflate(1I, 1I)
                e.Graphics.DrawRectangle(Pens.Black, rect)
                ' draw the Color name
                e.Graphics.DrawString(aColor.Name, Me.Font, br, e.Bounds.Height + 5I, ((e.Bounds.Height - Me.Font.Height) \ 2I) + e.Bounds.Top)
                br.Dispose()
            End If
        End Sub
    The combobox is filled with the names of colors from the colors enum.

    Anyone know why?
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  2. #2

    Thread Starter
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Using DrawString in ComboBox onDrawItem

    I forgot to post the stack trace:
    Code:
    System.ArgumentException was unhandled
      Message="Parameter is not valid."
      Source="System.Drawing"
      StackTrace:
           at System.Drawing.Graphics.CheckErrorStatus(Int32 status)
           at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, RectangleF layoutRectangle, StringFormat format)
           at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, Single x, Single y)
           at Controls.ColorComboBox.OnDrawItem(DrawItemEventArgs e) in C:\Users\JeffLT\Programming\Controls\Controls\ColorComboBox.vb:line 260
           at System.Windows.Forms.ComboBox.WmReflectDrawItem(Message& m)
           at System.Windows.Forms.ComboBox.WndProc(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
           at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr lparam)
           at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, Message& m)
           at System.Windows.Forms.Control.WmOwnerDraw(Message& m)
           at System.Windows.Forms.Control.WmDrawItem(Message& m)
           at System.Windows.Forms.Control.WndProc(Message& m)
           at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
           at System.Windows.Forms.ContainerControl.WndProc(Message& m)
           at System.Windows.Forms.Form.WndProc(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
           at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
           at System.Windows.Forms.Control.DefWndProc(Message& m)
           at System.Windows.Forms.Control.WmOwnerDraw(Message& m)
           at System.Windows.Forms.Control.WmDrawItem(Message& m)
           at System.Windows.Forms.Control.WndProc(Message& m)
           at System.Windows.Forms.ComboBox.WndProc(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
           at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
           at System.Windows.Forms.Control.DefWndProc(Message& m)
           at System.Windows.Forms.Control.WmMouseDown(Message& m, MouseButtons button, Int32 clicks)
           at System.Windows.Forms.Control.WndProc(Message& m)
           at System.Windows.Forms.ComboBox.WndProc(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
           at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.Run(ApplicationContext context)
           at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
           at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
           at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
           at Controls.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
      InnerException:
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Using DrawString in ComboBox onDrawItem

    I just tested your code on Vista x64 SP2 and I can confirm that behaviour, although I can't offer an explanation at this point.

  4. #4

    Thread Starter
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Using DrawString in ComboBox onDrawItem

    What about Win7 (x86 and x64) ?
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width