User Controls

Posted on 2004-10-21
Last Modified: 2010-04-24
I am using this code below for a custom label effect and someone once told me that "If you don't want to duplicate that code for EVERY single label on your form then you can create a UserControl that derives from the base Label and place that code in it. Then you can add that new Label control to your toolbox and drag/drop it onto your forms just like other controls.

Two part question: How can I do this in my VB.NET 2002 (I'm new and need lots of details) and how can I change the  e.Graphics.DrawRectangle(Pens.Blue, r) to use a custom brush color like I have done with the  Dim c As Color = Color.FromArgb(255, &HC1, &HD2, &HEE) section.

Private inLabel1 As Boolean
    Private Sub Label1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label1.MouseEnter
        Dim c As Color = Color.FromArgb(255, &HC1, &HD2, &HEE)
        Label1.BackColor = c
        inLabel1 = True
    End Sub

    Private Sub Label1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Label1.Paint
        If inLabel1 Then
            Dim r As Rectangle = New Rectangle(New Point(0, 0), Label1.Size)
            r.Width = r.Width - 1
            r.Height = r.Height - 1
            e.Graphics.DrawRectangle(Pens.MediumBlue, r)
        End If
    End Sub

    Private Sub Label1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label1.MouseLeave
        Label1.BackColor = Color.FromKnownColor(KnownColor.Control)
        inLabel1 = False
End Sub
Question by:teamdad
    LVL 18

    Expert Comment

    Right click the project -> Add -> Add Component

    see this example for extend a control
    it has extended the combo box

    LVL 18

    Expert Comment

    sorry the above link is for C#
    see this for VB.NET

    Author Comment

    So if I just want a label I am going to paint diffrent I just add a label to the form it brings up, in the code I change what I want it to be, I rename it to whatever I want the name of the control to show up as and then what..... it's sitting there as part of my form I am working on and according to the article it says Now, compile your control into a .DLL file and it's ready for use.  Am I right so far?
    LVL 18

    Expert Comment

    yes. Going good so far

    Author Comment

    If I compile it like this won't it be compiled with my program too? And in my VB.NET 2002 it doesn't have an option that I see to compile to a DLL.
    LVL 18

    Accepted Solution

    Ok leave it,
    Another easy way is to make a simple class and extend it from Label (lets say the name of the class is MYLabel, then use designer to create the GUI and use its Labels, then replace the Label class name in your code by MYLabel, there you have your inherited control on your form.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Article by: jpaulino
    XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String ( Literal, only instead of starting and ending with w…
    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    846 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now