User Controls

Posted on 2004-10-21
Medium Priority
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
  • 4
  • 2
LVL 18

Expert Comment

ID: 12378072
Right click the project -> Add -> Add Component

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

LVL 18

Expert Comment

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

Author Comment

ID: 12378128
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?
2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

LVL 18

Expert Comment

ID: 12378317
yes. Going good so far

Author Comment

ID: 12381911
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

armoghan earned 1500 total points
ID: 12397143
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.


Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
It’s quite interesting for me as I worked with Excel using vb.net 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 …
Hi, this video explains a free download that you can incorporate into your Access databases, or use stand-alone for contact management. Contacts -- Names, Addresses, Phone Numbers, eMail Addresses, Websites, Lists, Projects, Notes, Attachments…
Free Data Recovery software is an advanced solution from Kernel Tools to recover data and files such as documents, emails, database, media and pictures, etc. It supports recovery from physical & logical drive after a hard disk crash, accidental/inte…
Suggested Courses

588 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