Autosize on a label does not work properly at runtime

0
0

Hi,

Attached a sample project showing incorrect autosizing of a label. Notice that in the designer it does work properly.

Just run the sample to see.

Thanks for looking in to this

Vincent

  • You must to post comments
0
0

I didn’t get it to work. I use a Custom Control inside a panel where a header (CustomControl) is added and a new label with (long) text.

The purpose of this usercontrol (just a panel which will contain a bunch of controls loaded/created at runtime) is to show messages/notifications to the user with a custom header/title.

Data is fetched from a database.

Public sub Boot

Using Connection As OdbcConnection = New OdbcConnection(DSN)
Dim odbcCommand As OdbcCommand = New OdbcCommand(“SELECT TOP 10 P_ID FROM dbo.NOTIFICATIONS WHERE IsActive = ? ORDER BY P_ID DESC”, Connection) With {.CommandType = CommandType.Text}

With odbcCommand
.Parameters.AddWithValue(“@IsActive”, True)
End With

Using TekstveldAdapter As OdbcDataAdapter = New OdbcDataAdapter()
TekstveldAdapter.TableMappings.Add(“Table”, “NotificationData”)
TekstveldAdapter.SelectCommand = odbcCommand
Connection.Open()

Using dataSet As DataSet = New DataSet(“NotificationData”)
TekstveldAdapter.Fill(dataSet)

For a As Integer = 0 To dataSet.Tables(0).Rows.Count – 1

If Not DBNull.Value.Equals(dataSet.Tables(0).Rows(a).Item(“P_ID”)) Then

Dim TempItem As New Notification With {.ID = dataSet.Tables(0).Rows(a).Item(“P_ID”)}
TempItem.LoadData()

Dim NewControl As New NotificationItem With {.ExistingItem = TempItem, .Dock = DockStyle.Top}

NewControl.Boot()
Panel2.Controls.Add(NewControl)
NewControl.BringToFront()

Dim TempLabel As New Label With {.AutoSize = True, .Dock = DockStyle.Top, .Text = TempItem.Message}
TempLabel.Text = TempItem.Message
Panel2.Controls.Add(TempLabel)
TempLabel.BringToFront()

End If

Next a

End Using

End Using

End Using

End Sub

In this code :

“Notification” is just a class with a few properties (ID, Subject & Message).
“NotificationItem” is a custom control which serves as the message header
“TempLabel” is the label that’s being difficult.

Can you show me how to fix this code with your suggested workarrounds?

Thanks a lot!

  • Luca (ITG)
    Dim TempLabel As New Label With {.AutoSize = True, .Dock = DockStyle.Top, .Text = TempItem.Message, .CssStyle=”line-height:1.18″}
  • vincent_
    Thanks! This worked!
  • You must to post comments
0
0

This is an ongoing issue with have with different browsers and different font measuring when the text wraps. What you see in the designer is the way IE wraps the text. Depending on the font and the unit and the size it may differ from chrome which may different from firefox. You may find some blogs that recommend using line-height: 1.2em to normalize the browsers, but that also doesn’t work when the font is bigger and may cause lines to overlap.

In this case you can simply set the CssStyle property of the label to “1.18em” to make all browsers render the line gap in the same way.

In alternative you can have the browser measure it on the client using something like this:

Private Async Sub Page1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
   Me.Label2.AutoSize = False
   Dim text = Me.Label2.Text.Replace(vbCrLf, "<br/>")
   Me.Label2.Height = (Await Wisej.Base.TextUtils.MeasureTextAsync(text, True, Me.Label2.Font, Me.Label2.Width)).Height
 End Sub

 

The replacement to “<br/>” is necessary because there is a bug in TextUtils.MeasureTextAsync when the text includes hard coded newlines, which is fixed internally.

 

 

  • vincent_
    Thanks will give this a go! This explains why my mobile browser doesn’t have this problem.
  • You must to post comments
0
0

Hi Vincent,

Thank you for reporting this issue.
We’re in the process of investigating it and I will let you know of any updates.

Thank you,
Alaa

  • You must to post comments
Showing 3 results
Your Answer

Please first to submit.