我有一个vb.net应用程序,它使用system.drawing在combobox下拉列表中显示多个数据字段。但是每次打开它时它都会崩溃并获取错误消息未处理的异常(内存不足)
System.OutOfMemoryException:内存不足。在 System.Drawing.Graphics.FromHdcInternal(IntPtr hdc)at System.Windows.Forms.ComboBox.WmReflectDrawItem(Message& m)at at System.Windows.Forms.ComboBox.WndProc(Message& m)at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)在System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd,Int32) msg,IntPtr wparam,IntPtr lparam)
为下拉列表绘制代码
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
答案 0 :(得分:1)
paint方法不应该更改任何控件的属性,所以将这些行移动到窗体的构造函数或OnLoad覆盖:
Grower.DrawMode = DrawMode.OwnerDrawVariable
Grower.Size = New System.Drawing.Size(75, 120)
Grower.DropDownStyle = ComboBoxStyle.DropDown
Grower.DropDownWidth = 500
如果需要OwnerDrawVariable,您的代码中并不清楚。尝试使用OwnerDrawFixed并根据自己的喜好设置ItemHeight属性。此外,ComboBox没有可调高度,因此您只能设置宽度。