1
votes

I have a vb.net application that uses system.drawing to show multiple data fields in combobox dropdown.But it crashes every time i open it and get the error message unhandled exception (out of memory)

enter image description here

System.OutOfMemoryException: Out of memory. at System.Drawing.Graphics.FromHdcInternal(IntPtr hdc) 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.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Draw Code for dropdown

Private Sub NamesComboBox_DrawItem2(sender As Object, g As System.Windows.Forms.DrawItemEventArgs) Handles Grower.DrawItem

        Grower.DrawMode = DrawMode.OwnerDrawVariable
        Grower.Size = New System.Drawing.Size(75, 120)
        Grower.DropDownStyle = ComboBoxStyle.DropDown
        Grower.DropDownWidth = 500
        g.DrawBackground()

        Dim drv As DataRowView = CType(Grower.Items(g.Index), DataRowView)

        ' Retrieve the value of each column.
        Dim Grow As String = drv("Grower").ToString()
        Dim Ptype As String = drv("Poultry Type").ToString()
        Dim GrowName As String = drv("Grower Name").ToString()
        Dim BoardAccount As String = drv("Board Account").ToString()
        Dim ChickAccount As String = drv("Chick Account").ToString()
        Dim CatchingAccount As String = drv("Catching Account").ToString()


        ' Get the bounds for the first column
        Dim a1 As Rectangle = g.Bounds
        a1.Width = 50


        ' Draw the text on the first column
        Using sb As SolidBrush = New SolidBrush(g.ForeColor)
            g.Graphics.DrawString(Grow, g.Font, sb, a1)
        End Using

        ' Draw a line to isolate the columns 
        Using p As Pen = New Pen(Color.Black)
            g.Graphics.DrawLine(p, a1.Right, 0, a1.Right, a1.Bottom)
        End Using



        ' Get the bounds for the second column
        Dim a2 As Rectangle = g.Bounds
        a2.X = a1.Width + 1
        a2.Width = 15

        ' Draw the text on the second column
        Using sb As SolidBrush = New SolidBrush(g.ForeColor)
            g.Graphics.DrawString(Ptype, g.Font, sb, a2)
        End Using

        Using p As Pen = New Pen(Color.Black)
            g.Graphics.DrawLine(p, a2.Right, 0, a2.Right, a2.Bottom)
        End Using

        ' Get the bounds for the third column
        Dim a3 As Rectangle = g.Bounds
        a3.X = 65
        a3.Width = 180

        ' Draw the text on the third column
        Using sb As SolidBrush = New SolidBrush(g.ForeColor)
            g.Graphics.DrawString(GrowName, g.Font, sb, a3)
        End Using


        Using p As Pen = New Pen(Color.Black)
            g.Graphics.DrawLine(p, a3.Right, 0, a3.Right, a3.Bottom)
        End Using

        ' Get the bounds for the third column
        Dim a4 As Rectangle = g.Bounds
        a4.X = 245
        a4.Width = 50

        ' Draw the text on the third column
        Using sb As SolidBrush = New SolidBrush(g.ForeColor)
            g.Graphics.DrawString(BoardAccount, g.Font, sb, a4)
        End Using

        Using p As Pen = New Pen(Color.Black)
            g.Graphics.DrawLine(p, a4.Right, 0, a4.Right, a4.Bottom)
        End Using

        ' Get the bounds for the third column
        Dim a5 As Rectangle = g.Bounds
        a5.X = 295
        a5.Width = 50

        ' Draw the text on the third column
        Using sb As SolidBrush = New SolidBrush(g.ForeColor)
            g.Graphics.DrawString(ChickAccount, g.Font, sb, a5)
        End Using


        Using p As Pen = New Pen(Color.Black)
            g.Graphics.DrawLine(p, a5.Right, 0, a5.Right, a5.Bottom)
        End Using

        ' Get the bounds for the third column
        Dim a6 As Rectangle = g.Bounds
        a6.X = 345
        a6.Width = 50

        ' Draw the text on the third column
        Using sb As SolidBrush = New SolidBrush(g.ForeColor)
            g.Graphics.DrawString(CatchingAccount, g.Font, sb, a6)
        End Using


    End Sub
1
Showing us your drawing code would help.LarsTech
@LarsTech Added the code in question. ThanksManny Balboa
thanks @LarsTech that worked now its running, can you post it as answer so i can accept it as answer. ThanksManny Balboa

1 Answers

1
votes

The paint method shouldn't change any of the control's properties, so move these lines to the form's constructor or the OnLoad override:

Grower.DrawMode = DrawMode.OwnerDrawVariable
Grower.Size = New System.Drawing.Size(75, 120)
Grower.DropDownStyle = ComboBoxStyle.DropDown
Grower.DropDownWidth = 500

It's not clear from your code if OwnerDrawVariable is needed. Try using OwnerDrawFixed and set the ItemHeight property to your liking. Also, a ComboBox does not have an adjustable height, so you can only set the width.