Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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?
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

597 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