Imports System.Windows.Forms

Imports System.Drawing

Public Class UserControl1

Public btn As New System.Windows.Forms.Label

Private Sub UserControl1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

'Drwaing Close Button

Dim sb As New SolidBrush(Color.Red)

With btn

.Parent = Me

.Name = "cButton"

.BorderStyle = Windows.Forms.BorderStyle.FixedSingle

.Location = New Point(.Width - 20, 0)

.Size = New Size(12, 9)

.CreateGraphics.FillRectangle(sb, 1, 1, btn.Width - 1, btn.Height - 1)

End With

End Sub

End Class

It did not fill the rectangle with red color.

Re: Windows Forms General FillRectangle


What is it doing instead It probably is drawing the red rectangle, but that's immediately being drawn over again by the labels own draw methods. If you're wanting custom drawing you'll need to put something in an OnDraw event for the control and possibly change its drawing mode. Although it looks like all you're trying to do is make the label red, in which case you could just change its color property.

Re: Windows Forms General FillRectangle


I use FillRectangle for test and to see if it work.

can you give me an example about OnDraw event

Re: Windows Forms General FillRectangle


Sorry I've gone and given you the wrong event, not sure where I've come up with "OnDraw", it's the Paint event you want to look for. The code you've already got should work just fine in a handler for the Control.Paint event. The PaintEventHandler's second param is a PaintEventArgs which contains the Graphics context for the control. Use that graphics context and paint away to your hearts content.

Something like:

private void MyPaintHandler(object sender, System.Windows.Forms.PaintEventArgs e)

I know that's in C#, but same sort of thing in VB.

Re: Windows Forms General FillRectangle


I cant convert your code to vb.net but the I use Graphics for the UserControl PaintEventArgs and CreateGraphics for the Label called 'btn'.

Re: Windows Forms General FillRectangle


You shouldn't be using CreateGraphics. The "Control" base class (and thus, pretty much any control you can put on a form) has an event called Paint, simply handle that event. The PaintEventArgs object has a property called Graphics, use this instance of Graphics to do your drawing.