Question Drawing my own label

AthlonX4

New member
Joined
Jun 14, 2010
Messages
2
Programming Experience
Beginner
Hello everyone,
I create a class to draw a rectangle which contains a string. It is like a label.
Here is the class that I have created:
VB.NET:
Public Class DrawingBlock
    Private Draw As Graphics
    Public Sub New(ByVal point As Point, ByVal parentObj As Object, ByVal pen As Pen, ByVal brush As Brush, ByVal printString As String)
        Dim Rect As New Rectangle(point, RectSize)
        Draw = parentObj.CreateGraphics
        Draw.DrawRectangle(pen, Rect)
        Draw.DrawString(printString, New Font("Arial", 15), brush, New Point(point.X + 3, point.Y + 3))
    End Sub
End Class
I am wondering that if there is a way to add events to it. For example, if I clicked on certain rectangle, it will show a message box to tell me the color,location of the rectangle.
Thank you.
 
Personally, I would have your class inherit UserControl and do your drawing in the OnPaint method. Then you can handle the OnMouseClick (Or OnMouseDown) event and you'll have the mouse coord's already provided in the e variable.
 
Personally, I would have your class inherit UserControl and do your drawing in the OnPaint method. Then you can handle the OnMouseClick (Or OnMouseDown) event and you'll have the mouse coord's already provided in the e variable.

So you don't highly recommend me to create a totally new label, but inherit the Label control and add some new features? Thanks a lot!
 
So you don't highly recommend me to create a totally new label, but inherit the Label control and add some new features? Thanks a lot!
I suggested that you inherit UserControl, not inherit Label. Though you could inherit a Label, but I think you'd find yourself Shadowing and Overriding an awful lot for this, which is why I suggested UserControl which gives you a blank canvas to work with.
 
UserControl does have the overhead of being a container control, it also hides the Text property which a label usually needs. I'd just inherit Control class to have a 'blank' control.
 

Latest posts

Back
Top