Combobox OnPaint method

C

Chris

1. I was trying to use a flatcombo box. I found code on codeproject
that did what I needed. They used the following code in the WndProc
overridden procedure.

Dim g As Graphics = Me.CreateGraphics
g.FillRectangle(BorderBrush, Me.ClientRectangle)

I thought using CreateGraphics was a no-no if you could avoid it. So I
changed it to

Dim g As Graphics = Graphics.FromHwnd(Me.Handle)
g.FillRectangle(Brushes.Black, Me.ClientRectangle)

Is my was a better way of doing it?

2. I thought it would be better to do this in the onpaint override of
the combobox, but it never gets called. Why not?

Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
Dim rect As Rectangle = New Rectangle(Me.Width - 15, 3, 12,
Me.Height - 6)
e.Graphics.FillRectangle(Brushes.Black, rect)
e.Graphics.FillRectangle(Brushes.Black, Me.ClientRectangle)
End Sub

Thanks for the info.
Chris
 
K

Ken Tucker [MVP]

Hi,

Add this in sub new for your class to get the onpaint event to
work.

Me.SetStyle(ControlStyles.UserPaint, True)

Ken
---------------------
1. I was trying to use a flatcombo box. I found code on codeproject
that did what I needed. They used the following code in the WndProc
overridden procedure.

Dim g As Graphics = Me.CreateGraphics
g.FillRectangle(BorderBrush, Me.ClientRectangle)

I thought using CreateGraphics was a no-no if you could avoid it. So I
changed it to

Dim g As Graphics = Graphics.FromHwnd(Me.Handle)
g.FillRectangle(Brushes.Black, Me.ClientRectangle)

Is my was a better way of doing it?

2. I thought it would be better to do this in the onpaint override of
the combobox, but it never gets called. Why not?

Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
Dim rect As Rectangle = New Rectangle(Me.Width - 15, 3, 12,
Me.Height - 6)
e.Graphics.FillRectangle(Brushes.Black, rect)
e.Graphics.FillRectangle(Brushes.Black, Me.ClientRectangle)
End Sub

Thanks for the info.
Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top