DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2006

    User Control inherited from TextBox with Label

    I want to create a user control inherited from a TextBox with an
    associated label (let's call it a LabelTextBox). The trick is that I
    want the control to inherit from TextBox and have all of its properties
    available rather than the default properties of the user control. I
    also do not want to have to expose the Textbox properties one by one in
    a user control.

    I have already previously created this control using Delphi and I have
    gotten pretty far along with it using VB.Net. Basically, whenever the
    "LabelCaption" property is set, a label is either created on the same
    parent as the TextBox and the label caption is set or if the label has
    already been created, the label caption is simply reset.

    Now the hard part. In Delphi, I overrode (and then inherited from) the
    setbounds procedure and there I added a call to my UpdateLabel
    procedure which forces the label keep its position relative to the
    Textbox whenever it is moved. So, at designtime, whenever the TextBox
    is moved, the label follows.

    I have not been able to do the equivilent with VB.Net as I cannot
    figure out how to override setbounds and/or inherit from it too. My
    label always stays whereever it was created unless I change the caption
    (which also includes a call to UpdateLabel).

    Any help with this would be greatly appreciated!



  2. #2
    Join Date
    May 2010

    User Control inherited from TextBox with Label

    Hello Markosoft.

    About three years from your post, I am also doing an UserControl inherited from TextBox with an optional Label,

    Reasons of inherited from TextBox are the same as you told in your post.

    I added a new property to the control called "Label". If this label is not "" I crete a new Object Label and I add it to the parent of the TextBox.

    This is working fine in RunTime, but in DesignTime, there's no Parent of textbox available, so I can't assign it, the result is that I can't see the label in Dessign time.

    I think you know how to make this, because your question in your post is later than my problem.

    I looked for ideas in forums, but everyone tell me I make a control inherited from UserControl and then putting inside a Label and a TextBox, but this is that I don't want to do.

    Please, I'll be very grateful if you can give me any idea.


    PD. I add the minimum code for if you want to look what I am doing wrong.
    Public Class LabTex
        Inherits System.Windows.Forms.TextBox
        Private objLabel as Label = Nothing
        Private sLabel as string = ""
        Property Label() as String
                return sLabel
            End Get
            Set(ByVal Value As String)
                sLabel = Value
                If sLabel = "" Then
                    If objLabel IsNot Nothing Then
                        objLabel.Visible = False
                        objLabel = Nothing
                    End If
                    If objLabel Is Nothing Then
                        objLabel = New Label()
                        objLabel.AutoSize = True
                        objLabel.Visible = True
                        objLabel.Text = sLabel
                        objLabel.Left = Me.Left - 3 - objLabel.Width
                        objLabel.Top = Me.Top + (Me.Height * 2 / 3) - 8
                        objLabel.ForeColor = Color.Navy
                        If Me.DesignMode Then
                        End If
                    End If
                End If
            End Set
        End Property
        Protected Overrides Sub OnHandleCreated(e As EventArgs)
            If sLabel <> "" Then
                objLabel.Left = Me.Left - 3 - objLabel.Width
                objLabel.Top = Me.Top + (Me.Height * 2 / 3) - 8
            End If
        End Sub
    End Class
    Last edited by Hack; 05-31-2010 at 08:56 AM. Reason: Added Code Tags

  3. #3
    Join Date
    Feb 2004
    Longueuil, Québec
    Lets define a few things first, because what you say makes no sense, probably because you are misled by your experience in Delphi. Although Delphi and .NET are both object oriented, some features that have the same names are often handled differently.


    In .NET, you cannot have a user control that inherits from a TextBox.

    A user control is a control that inherits from the UserControl class.

    VB, like most object oriented languages, does not support multiple inheritance, so you cannot have something that inherits from both UserControl and TextBox. You either have a user control, or you have a class that inherits from TextBox. You cannot have both at the same time.

    A UserControl is not really a control. It is a surface/container on which you put other controls. It's kind of a form without a border, that can be inserted inside a regular form. In some environments, it could be called a sub-form.

    When you want to create a UserControl, you go to the Project menu and select Add User Control, which creates a surface on which you put controls as if you were in a Form.

    You can put a TextBox and a Label inside a UserControl, and code any interaction between those 2. You can then use the UserControl inside a regular form as if it was but one control, bringing with it the all controls it contains and the code that makes them work together.

    From what you say, I think that this is what you are aiming at. But it has nothing to do with inherithing from a TextBox.


    When you inherits from a TextBox, you are not creating a UserControl, but simply a Control (some call it a custom control) that has all the properties and methods of a TextBox, to which you can add new properties or methods, or overwrite the existing ones to makes them behave in a different way.

    Just as a TextBox, it is an individual control. If you want a Label to be associated with the TextBox and to move with it you have to code the creation and manipulation of the Label inside your class, which is possible and give you a lot of latitude. From the little you tell us, this looks as if might be what you were doing in Delphi.

    But is a lot harder to code than putting a TextBox and a Label inside a UserControl. Move the UserControl, which is a surface, and whatever is on that surface (the TextBox and Label) moves while keeping the same relative position, with no code on your side.


    So, when you say "a user control inherited from a TextBox" it makes no sense. Either you have a UserControl, either you have something inherited from a TextBox. You cannot have both.

    If I am reading your thoughts well, what you want to do is Project... Add User Control. Add a TextBox and a Label to the surface (the UserControl) that is created, and position them as you want. Then reuse the UserControl thus created inside forms. You will see that when you move the UserControl, both controls move together.

    The UserControl has the standard properties and methods of all controls, but also has properties that references the controls in contains.

    UserControl1.BackColor = Color.Red
    UserControl1.TextBox1.Text = "I am the TextBox on the UserControl"
    UserControl1.Label1.Visible = False
    UserControl1.Label1.Text = "I am the Label on the UserControl, but nobody sees me"

    Try to forget Delphi. Although both Delphi and .NET are object oriented, they have their differences, and some terms do not have the same meaning in Delphi and .NET.

    I tell the same thing to my students who are VB6 programmers: try to forget VB6 when you move to .NET.

    Although the language is Visual Basic in both, a few things, including user controls, have changed in such a way that you would lose a lot of time trying to do things in .NET the way you did in VB6. If this is true when moving from Microsoft's Visual Basic to another Microsoft's Visual Basic, imagine what it is when you move from Borland's Pascal to Microsoft's Visual Basic .NET.

    User controls are not elements of standard object programming. They have been added in some environements to help you work faster. Since they are not standard, every vendor has its own implementation.

    There are new ways of doing things, and they are usually better and easier to use. VB6 and Delphi where created at the beginning of the 90's. I am starting to meet professional programmers that were not even born then. Those are old things, and things have changed a lot in computing since then.


    Posting your code along your questions could help us understand what you are trying to achieve and guide you with more precision.
    Jacques Bourgeois

  4. #4
    Join Date
    May 2010
    Hello JBourgeois

    First of all many thanks for your extensive response, I am very grateful.

    I want to emphasize that this thread is from Markosoft in 2006-October. and I re-opened it yesterday in 2010-May

    I reopened it making a question to him. He said in 2006 that he developed an (Usercontrol) inherited form TextBox. Yes, he and I (both) were wrong with this word (UserControl). We had to say "Control inherited" from TextBox.

    After that, he said that his problem was only when he moved the Texbox
    in designTime, Label stayed in previous ubication.

    So he was telling that he was able to create an TextBox and putting also a label in DesignTime owned from the parent of TextBox.

    My question was exactly to ask how he made it. Never more. And my problem is that I can see the label in RunTime, because TextBox alrady have a Parent, but in DesignTime, I don't know how to add the label to the textbox.Parent.

    In my previous post, I wrote the minimum code lines to try my Control.

    And I Know perfectly what is an UserControl, I have maid several. What I am looking making the control of that manner is that I wanted vertical alignement from TextBox and not from Label.

    After that I want to tell that I have already made it from an UserControl and now thanks to "Nick Thissen", I can Align my UserControl From the TextBox (And not from the Label).

    So, Although I have it solutioned with my UserControl, I like to know how "Markosoft" made his Control (or everyone who can tell me how acceding to the parent of the TextBox in DesignTime).


  5. #5
    Join Date
    Apr 2007
    Sterling Heights, Michigan
    Quote Originally Posted by mmustaros View Post
    I reopened it making a question to him.
    When you post a question in a thread, you are posting a question to everyone, not just one person.

    In this case, it is unlikely you will get a response from Markosoft as he has not logged onto this site since May of 2007.

  6. #6
    Join Date
    Feb 2004
    Longueuil, Québec
    Interesting little challenge. I went for it.

    Nothing prevents you from making the thing work in Design mode, but when you want to do so, you sometimes have to consider Design mode and runtime differently.

    Since a control is typically not moved in runtime, you probably did not react to the Move event. But since you want the thing to work when you move the control in Design, you should react to that event to move the Label.

    Here is the code. Feel free to ask questions if you want precisions.

    Public Class LabelledTextBox
        Inherits System.Windows.Forms.TextBox
        Private _Label As Label
        Protected Overrides Sub OnMove(ByVal e As System.EventArgs)
            'Do not react on first move, the control is simply being initialized
            '   and is not yet included in the form, so it has no parent
            If Me.Parent IsNot Nothing Then
            End If
        End Sub
        Protected Overrides Sub OnVisibleChanged(ByVal e As System.EventArgs)
            'Create the Label when the TextBox becomes visible
            Static FirstPass As Boolean = True
            'See comment in OnMove + We create the Label only on the first pass
            If Me.Parent IsNot Nothing Then
                If FirstPass Then
                    _Label = New Label
                    _Label.Text = "I am the Label"
                    _Label.Visible = True
                    FirstPass = False
                    'Label visibility must follow TextBox visibility
                    _Label.Visible = Me.Visible
                End If
            End If
        End Sub
        Private Sub SetLocation()
            _Label.Location = New Point(Me.Location.X, Me.Location.Y - 20)
        End Sub
    End Class
    Jacques Bourgeois

  7. #7
    Join Date
    May 2010
    Thanks again JBourgeois, for your time and for your excellent work.

    Yes, that is what I was looking. Your Class works fine, and your teachs are crystal clear.

    Miquel (mmustaros)

Similar Threads

  1. User Control behaves differently in Excel User Forms
    By f_kandalaft in forum VB Classic
    Replies: 0
    Last Post: 01-21-2003, 06:59 PM
  2. Replies: 1
    Last Post: 08-27-2002, 02:28 AM
  3. Renaming a user control
    By Jeff Pipes in forum .NET
    Replies: 2
    Last Post: 02-25-2002, 07:27 AM
  4. pass a form to a web user control (pagelet)
    By Annie Wang in forum ASP.NET
    Replies: 0
    Last Post: 05-24-2001, 04:07 PM
  5. Tabbing within a user control
    By Jamie Harsevoort in forum VB Classic
    Replies: 4
    Last Post: 06-12-2000, 06:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center