Results 1 to 18 of 18

Thread: [RESOLVED] Labeled TextBox. I Can't see the label in design

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    7

    Resolved [RESOLVED] Labeled TextBox. I Can't see the label in design

    I have made a control inherited from TextBox, but I want an optional Label in front of the textBox.

    The soure

  2. #2

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    7

    Labeled TextBox. I Can't see the label in design

    I have made a control inherited from TextBox with an optional Label in front of the textBox.

    The problem is that I can't see the Label in Design Time, but in execution it goes fine.

    Please any help will be welcome.
    Thanks.
    Miquel

    The source:

    Public Class LabTex
    Inherits System.Windows.Forms.TextBox

    Private ObjLabel as Label = Nothing
    Private sLabel as string = ""
    Property Label() as String
    Get
    return sLabel
    End Get
    Set(ByVal Value As String)
    sLabel = Value
    If sLabel = "" Then
    If ObjLabel IsNot Nothing Then
    ObjLabel = Nothing
    End If
    else
    If ObjLabel Is Nothing Then
    ObjLabel = New Label()
    if Me.DesignMode then
    Me.Controls.Add(Me.ObjLabel)
    'else
    ' Me.Parent.Controls.Add(Me.ObjLabel)
    End If
    End If
    ObjLabel.Text = sLabel
    ObjLabel.AutoSize = True
    ObjLabel.Top = Me.Top + (Me.Height / 3)
    ObjLabel.Left = Me.Left - 2 - ObjLabel.Width
    End If
    End Set
    End Property

    Protected Overrides Sub OnHandleCreated(e As EventArgs)
    MyBase.OnHandleCreated(e)
    If ObjLabel IsNot Nothing Then
    if Me.DesignMode then
    Me.Controls.Add(Me.ObjLabel)
    else
    Me.Parent.Controls.Add(Me.ObjLabel)
    End If
    ObjLabel.Top = Me.Top + (Me.Height / 3)
    ObjLabel.Left = Me.Left - 2 - ObjLabel.Width
    End If
    End Sub

    End Class

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    7

    Re: Labeled TextBox. I Can't see the label in design

    Thanks.

    I pressed Enter-Key before finished.

    I Have posted a new Thread.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Labeled TextBox. I Can't see the label in design

    as NickThissen told you in your duplicate thread you need to use a usercontrol

  6. #6

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    7

    Re: Labeled TextBox. I Can't see the label in design

    Thanks, but I want it inherited from a TextBox because I want to have all TextBox Properties and not the default properties of an UserControl.

    The code I post works fine in execution, the only fail is that I can't see the label in design.

  7. #7
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: Labeled TextBox. I Can't see the label in design

    In design mode what is the text property for the label ?

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Labeled TextBox. I Can't see the label in design

    Duplicate threads merged - please create only one thread for each question.

  9. #9

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    7

    Re: Labeled TextBox. I Can't see the label in design

    In Design I put a value for the property "Label" in te Properties-Editor such as "Code".

    In execution I see first Code and then the TextBox, but in design I only see the TextBox.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Labeled TextBox. I Can't see the label in design

    As has been suggested, a UserControl is the appropriate option here. If you want your control to have the same members as the TextBox class then add them yourself, e.g.
    vb.net Code:
    1. Public Class LabelledTextBox
    2.     Inherits UserControl
    3.  
    4.     '...
    5.  
    6.     Public Sub AppendText(ByVal text As String)
    7.         Me.TextBox1.AppendText(text)
    8.     End Sub
    9.  
    10.     '...
    11.  
    12. End Class
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Labeled TextBox. I Can't see the label in design

    You can also expose the entire TextBox (as well as the Label should you need to) via a property. The general rule is not to do this, and simply expose all properties manually (as jmcilhinney showed you in the post above this), but if you need many properties, perhaps it's easier to just expose the TextBox completely.

    You can do it using a ReadOnly property that returns the textbox:
    Code:
    Public ReadOnly Property TextBox() As TextBox
       Get
          Return TextBox1  ' or whatever the name of your textbox is
       End Get
    End Property
    In the designer (as well as in code) you can now access the TextBox property, expand it (in the property list) and change its properties directly.

    Additionally, you may need to add an attribute so that any changes you make in the designer are saved. I'm not too sure about this, but you can try with and without the attribute to see if it has any effect:
    Code:
    <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
    Public ReadOnly Property TextBox() As TextBox
       Get
          Return TextBox1  ' or whatever the name of your textbox is
       End Get
    End Property
    You'll need to import a namespace (System.ComponentModel I think), but VB will help you with that.

  12. #12

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    7

    Re: Labeled TextBox. I Can't see the label in design

    Thanks again.

    But the main reason I want the component inherited from TextBox is the alignement from the Designer. I want put several LabTextBox aligned from Text-Box Left-Corner, not from Label Left-Corner.

    If I make an UserControl With Label and TextBox inside it, the alignement from Designer will be made from left-corner of UserControl, not from TextBox.

    The code I sent before, works fine in execution because I can add the label to Me.Parent.Controls.Add(Label), where "Me" is the TextBox. But in Design there is no Parent active for the TextBox.

    I Have made also this control in Delphi and it works well both in design & execution.

    Do you know how I can put in design a Label with same parent than TextBox?

    Thanks

  13. #13
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Labeled TextBox. I Can't see the label in design

    You can use a custom ControlDesigner to control the snap-lines, although that is not a trivial task. I'm sure it's possible to tell the designer to use the TextBox's snap-lines, instead of the snap-lines of the UserControl itself, but I don't know exactly how. There was a thread about this in the C# forum a while back if you want to look for it.

  14. #14
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Labeled TextBox. I Can't see the label in design

    Here, I made you a little test designer:
    vb.net Code:
    1. Imports System.Windows.Forms.Design
    2. Imports System.Windows.Forms.Design.Behavior
    3. Imports System.ComponentModel
    4. Imports System.ComponentModel.Design
    5.  
    6. <Designer(GetType(LabelTextBox.LabelTextBoxDesigner))> _
    7. Public Class LabelTextBox
    8.  
    9.     Public ReadOnly Property TextBox() As TextBox
    10.         Get
    11.             Return TextBox1
    12.         End Get
    13.     End Property
    14.  
    15.     Public ReadOnly Property Label() As Label
    16.         Get
    17.             Return Label1
    18.         End Get
    19.     End Property
    20.  
    21.     Friend Class LabelTextBoxDesigner
    22.         Inherits ControlDesigner
    23.  
    24.         Public Overrides ReadOnly Property SnapLines() As System.Collections.IList
    25.             Get
    26.                 Dim lines = MyBase.SnapLines
    27.  
    28.                 ' Get a reference to the UserControl we're designing, so we can get to its TextBox
    29.                 Dim ctrl = DirectCast(Me.Control, LabelTextBox)
    30.  
    31.                 If ctrl IsNot Nothing Then
    32.                     ' Create the designer for the TextBox, so we can get at its SnapLines
    33.                     Using designer As ControlDesigner = TypeDescriptor.CreateDesigner(ctrl.TextBox, GetType(IDesigner))
    34.                         designer.Initialize(ctrl.TextBox)
    35.  
    36.                         ' Find the BaseLine snapline (the one that aligns the TextBoxes by text)
    37.                         For Each line As SnapLine In designer.SnapLines
    38.                             If line.SnapLineType = SnapLineType.Baseline Then
    39.                                 ' We found the baseline, now add a new SnapLine with a different offset (adding the Top of the TextBox to the offset of the old baseline)
    40.                                 lines.Add(New SnapLine(line.SnapLineType, line.Offset + ctrl.TextBox.Top, line.Filter, line.Priority))
    41.                             End If
    42.                         Next
    43.                     End Using
    44.                 End If
    45.  
    46.                 ' Return our modified SnapLines
    47.                 Return lines
    48.             End Get
    49.         End Property
    50.     End Class
    51. End Class
    The code represents a UserControl with a TextBox (named TextBox1) and a Label (named Label1).

    The UserControl itself merely exposes the TextBox and Label via properties (so I can get to the TextBox in the designer).

    Then there is a nested class (LabelTextBoxDesigner) which is the designer for the LabelTextBox UserControl. It overrides the SnapLines property, and modifies the SnapLines of the UserControl by adding a new baseline SnapLine. This new SnapLine has an offset (where it is snapped... so to speak) equal to the baseline SnapLine of the TextBox on our UserControl, plus the Top of the TextBox, so that it is relative to the UserControl.


    Try it out. Add a new UserControl called LabelTextBox to your project and drag a Label and a TextBox to it (doesn't matter where they are). Copy/paste my code (quote my post and copy/paste from there to get rid of line-numbers). You'll probably get a lot of errors. If you do, you need to add a reference to System.Design (rightclick the project node in the solution explorer, choose Add Reference, scroll to System.Design and click OK).

    Finally, you can build the project, drop a LabelTextBox to your form, as well as a regular TextBox. Now you can align the LabelTextBox to the TextBox using the baseline snaplines of the two TextBox, instead of that of the UserControl:


    Cool huh

    Now, you can use the correct method of using a UserControl, and still align it by the TextBox on the UserControl instead of the UserControl being seen as one large control.

  15. #15

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    7

    Re: Labeled TextBox. I Can't see the label in design

    Many thanks NickThissen. I'm grateful for you support and Work.

    I tryed your code and it works fine.

    I also added Vertical alignement adding a newline after your number 40
    lines.Add(new SnapLine(SnapLineType.Left, ctrl.TextBox.Left))

    As an example my demo form of LabelTexBox looks as
    Attached Images Attached Images  

  16. #16
    New Member
    Join Date
    Feb 2025
    Posts
    3

    Re: [RESOLVED] Labeled TextBox. I Can't see the label in design

    The LabelTextBox_TextChanged Event not working.
    I have tried by handling this event but with no success, Can you code a functionnal TextChanged event if it's possible.
    Thanks a lot

  17. #17
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Labeled TextBox. I Can't see the label in design

    Quote Originally Posted by infoleom View Post
    The LabelTextBox_TextChanged Event not working.
    I have tried by handling this event but with no success, Can you code a functionnal TextChanged event if it's possible.
    Thanks a lot
    It is working. It just doesn't do what you want, so you have to put some thought into what it is that you actually want. The TextChanged event of the LabelTextBox is raised when the Text property of the LabelTextBox changes. You don't care about that. What you care about is the Text property of the child TextBox, so you need to handle the TextChanged event of that child TextBox. You have two choices:

    1. You can handle that event directly in the form, which would require you to access the TextBox via the property that Nick provided. This would have to be done in code, using AddHandler to attach the event, which would also require using RemoveHandler to detach the event when you're done with it.

    2. Handle the child control's event internally, then raise your own even in the LabelTextBox. That would enable you to then handle that event in the form at design time.

    If you want to go with option 1, you should do some reading on AddHandler and try to do it, then post back if you encounter an issue. If you want to go with option 2, you already know how to handle the child event in the LabelTextBox - it's just like any other event - and you can learn how to define and raise your own events here. Again, do what you can for yourself and then post back if you have issues.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  18. #18
    New Member
    Join Date
    Feb 2025
    Posts
    3

    Re: [RESOLVED] Labeled TextBox. I Can't see the label in design

    Sorry jmcilhinney i haven't succeed even after reading the link you post.
    That's why I create a new Thread : https://www.vbforums.com/showthread....Box-with-Label.
    Thanks for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width