ComboBox - Override OnPaint Problem

  • Thread starter Thread starter ChrisP1980
  • Start date Start date
C

ChrisP1980

Guest
Hello!

I am trying to override the OnPaint event for a custom ComboBox. Border and background work just fine, however, the text box is the problem. It remains white and has an ugly black underline. I could not find anything via Google. I used red and blue colors just for trying. Here is the code:

Option Strict On

Public Class clsCombo

Inherits ComboBox

Private _Margin As Integer = 10
Private _State As VisualStyles.ComboBoxState = VisualStyles.ComboBoxState.Normal
Private _Font As New Font("Arial", 11.0F, FontStyle.Regular)

Private rectBorder As Rectangle

Protected Overrides Sub OnCreateControl()

MyBase.OnCreateControl()

SetStyle(ControlStyles.UserPaint, True)
SetStyle(ControlStyles.ResizeRedraw, True)

End Sub

Protected Overrides Sub OnSizeChanged(e As EventArgs)

MyBase.OnSizeChanged(e)

rectBorder = New Rectangle(0, 0, Me.Width - 1, Me.Height - 1)

End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)

MyBase.OnPaint(e)

e.Graphics.FillRectangle(New SolidBrush(Color.Blue), rectBorder)
e.Graphics.DrawRectangle(New Pen(Color.Red), rectBorder)


End Sub

End Class

Here is how it looks like:

1577407.gif

Furthermore, I would like to add padding to the ComboBox (i.e. the margin between textbox and the border). Have not found a way either because the size is always based on the text.

Any resource/help would be much appreciated. Thank you!

Best regards,

Christian

Continue reading...
 
Back
Top