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
Solved

Dynamic generated label does not have the same width with textbox width.

Posted on 2009-06-28
10
236 Views
Last Modified: 2012-05-07
Hi,

I generate label using coding from certain textbox.
Why the width of the label is not the same as textbox width?

Thank you.
Public Shared Sub Create_Label_Control(ByRef objObject As Object, _
                                             ByVal objControlToClone As Control, _
                                             ByRef lblControl As Label, _
                                             ByVal strLabelName As String, _
                                             Optional ByVal blnLeft As Boolean = True, _
                                             Optional ByVal intAddWidth As Integer = 0)
 
        With lblControl
            .AutoSize = False
            .SetBounds(objControlToClone.Location.X, objControlToClone.Location.Y, objControlToClone.Width, objControlToClone.Height)
            .BorderStyle = Windows.Forms.BorderStyle.FixedSingle
            .BackColor = objControlToClone.BackColor
            .ForeColor = objControlToClone.ForeColor
            .Font = objControlToClone.Font
 
            If blnLeft Then
                If TypeOf (objControlToClone) Is TextBox Then
                    Dim txt As TextBox = CType(objControlToClone, TextBox)
 
                    If txt.Multiline = True Then
                        .TextAlign = ContentAlignment.TopLeft
                    Else
                        .TextAlign = ContentAlignment.MiddleLeft
                    End If
                Else
                    .TextAlign = ContentAlignment.MiddleLeft
                End If
            Else
                .TextAlign = ContentAlignment.MiddleRight
            End If
 
            .Text = objControlToClone.Text
 
            .Width = .Width + intAddWidth
 
            If TypeOf objControlToClone Is DateTimePicker Then
                .Width = .Width + 10
            End If
 
            .Name = strLabelName
 
            objObject.Controls.Add(lblControl)
 
        End With
 
    End Sub

Open in new window

0
Comment
Question by:emi_sastra
  • 6
  • 4
10 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24731792
Could you be a little more specific?...how far off is the label from the original?

It appears to be working in my simple tests...
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 24732931
The label created usually less a character wide then the textbox.

For example:

textbox ="123"
label ="12"

Why is that?

Thank you.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24733005
I'm not sure...
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:emi_sastra
ID: 24778588
Hi Idle_Mind,

Sorry, just back again.

After test it using IDE, I find out that label should be greater width than textbox.
Try it by :

1. Drop 1 label and 1 textbox.
2. Font size for both is Courier New 11 Regular.
3. Autosize = false.
4. Fill it both with "1234"
5. Adjust it to the same Size.

The you can see the different.

Thank you.

0
 
LVL 1

Author Comment

by:emi_sastra
ID: 24785438
Hi Idle_Mind,

Any idea how to solve the problem?

Thank you
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24799297
I can see the difference...just not sure what to do about it.

Is the end goal to put the Label directly on top of the TextBox so that it looks like the TextBox is "locked"?
DifferenceLabelTextBox.jpg
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 24800014
-Is the end goal to put the Label directly on top of the TextBox so that it looks like the TextBox is "locked"?
Yes, it is.

Thank you.
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 24819899
Hi Idle_Mind,

Any idea how to solve the problem?

Thank you
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 24819919
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 24820050
Hi Idle_Mind,

Thanks for the link. I get idea from it, it seems not perfect yet, but should try it first.

Thank you very much for your help.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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 (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

809 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