Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 242
  • Last Modified:

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

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
emi_sastra
Asked:
emi_sastra
  • 6
  • 4
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
emi_sastraAuthor Commented:
The label created usually less a character wide then the textbox.

For example:

textbox ="123"
label ="12"

Why is that?

Thank you.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I'm not sure...
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
emi_sastraAuthor Commented:
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
 
emi_sastraAuthor Commented:
Hi Idle_Mind,

Any idea how to solve the problem?

Thank you
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
emi_sastraAuthor Commented:
-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
 
emi_sastraAuthor Commented:
Hi Idle_Mind,

Any idea how to solve the problem?

Thank you
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
0
 
emi_sastraAuthor Commented:
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now