Auto width for Label

I’d like to adapt the width of a Label to its Text length. (Text property varies with localization)
But I have found no event to detect change in Text.

I’ll appreciate any suggestion.

You don’t need an event, since the change will occur when you modify the content of the Text property. Just manage the label width at that moment.

Alternatively, create a custom label, add to it a computed string property Caption, and manage the autosize in the Set event. Then replace the super of your existing label instances by CustomLabel.

In Xojo versions 2015R1 and over, you cannot create a new Text property, since it is a type. It is possible in older versions, but I have not succeeded shadowing that property. Super.Text autocompletes fine, but triggers a “does not exist” error at compile.

I vaguely remember reading posts (from Norman I think) doing things with Super, but could not locate that back. There maybe some special way to go that may work.

You don’t use Super on properties. Instead, cast ME to the super class, eg.,

label(me).text = “xyz”

Thanks Tim. I knew there was a way.

So here is the set of the Text computed property in 2013R1 :

Set Label(me).Text = value dim g as new picture(1,1,32) if me.Textsize = 0 then g.graphics.textsize = 12 else g.graphics.Textsize = me.TextSize end if g.graphics.TextFont = me.TextFont me.width = g.graphics.stringwidth(value)*1.10 End Set

Once the customLabel is pasted into 2016R1.1 it works, because Xojo allows older classes to use the word “Text” for properties, not to break existing code.

If you can use the Label.Open Event

   me.Text = "my new label Text" // your text here
  Dim d As Double
  Dim p As New Picture(me.Width, me.Height, 32)
  d = p.Graphics.StringWidth(me.Text)
  me.Width = floor (d) + 2

Interesting. I didn’t know it was possible to use .Text.
So that needs to be done in 2013R1 and then copied to 2016R1.1?

I use this as my auto size label: https://dl.dropboxusercontent.com/u/609785/Xojo/Forum/AutoSizeLabel.xojo_binary_project

[quote=275762:@Tim Hare]You don’t use Super on properties. Instead, cast ME to the super class, eg.,

label(me).text = “xyz”[/quote]
Be aware that shadowing can lead to problems. Say you have a Label subclass called MyLabel and you shadow the Text property:

Class MyLabel Inherits Label Computed Property Text Get() As String Return "*** " + Label(Self).Text + " ***" End Get Set(value As String) Label(Self).Text = value End End Computer Property End Class
And then you use it like that:

For i As Integer = 0 To Window.ControlCount - 1 Dim ctl As Control = Window.Control(i) If ctl IsA Label Then System.DebugLog Label(ctl).Text End Next
This will print the text of the label for all Label and for all MyLabel instances, even if you expected that for MyLabels it would print something like “*** xyz ". The For loop would have to look like this to have " xyz ***” printed:

For i As Integer = 0 To Window.ControlCount - 1 Dim ctl As Control = Window.Control(i) Select Case ctl Case IsA MyLabel System.DebugLog MyLabel(ctl).Text Case ctl IsA Label Then System.DebugLog Label(ctl).Text End Next
Note that the MyLabel case has to come before the Label case.

Note that shadowing a property like this is not recommended. Your crops may fail and your hair may fall out. Just sayin’

My original idea was to use a Caption property anyway.

[quote=275773:@Marco Hof]Interesting. I didn’t know it was possible to use .Text.
So that needs to be done in 2013R1 and then copied to 2016R1.1?
[/quote]

Any version prior to the new framework which appeared in 2015R1.