Label. Is there TextChanged event or similar?

I’d like to adapt window’s dimensions when some label changes its content.
But I can’t find which event to use to adapt these dimensions when I change the text of the label.

Can you give me any hint?
Thanks in advance.

Labels are a decoration with no means for the user to change their caption, therefore have no event for that. Assuming you’re changing it via code, you would carry out whatever operation you wish to carry out when you set the caption.

1 Like

I’m doing something similar with a custom label subclass. My suggestion:

  • Add a CurrentText property, to store the last label caption

  • Add a “TextHasChanged” event definition

  • Add a ‘Text’ property as computed property, this will shadow the Text regular property

  • In this property setter, put this code:

    label(self).Text = value  // To avoid shadow troubles
    Dim CurrValue as string = CurrentText // keep current stored value, for compare
    CurrentText = value // Store the new value
    if CurrValue <> value then RaiseEvent TextHasChanged // Raise event if text has changed
  • FInally, in the Text property getter, put this code:

    return Label(self).Text

This way you will receive a TextHasChanged event when the text is changed.

Hope this helps.

1 Like

Thanks Anthony and Xavier for your suggestions. They are really valuable.
Although in my case Labels are not mere decorations. They change and explain things according to the development of the application. Of course I know when I change the text by code and I can do everything there, but I think that “events” were invented for this reason and, again in my case, it is a pity that this event doesn’t exist for labels.
I’ve thought of using a TextField without border, transparent and read only, which has a TextChange event and looks like a label, but this should be a Label, shouldn’t it?

Create a label subclass and override the Text property with a computed property and raise your own event. :slight_smile:

Another idea: use a TextField without a focus ring or border set to Read Only.

Haven’t tested on Windows, but on a Mac, that’s indistinguishable from a Label.

Thanks to all.
But it seems that finally I got a way to know how many lines has a text wrapped in a label.

Var ht As Double = Self.BitmapForCaching(Self.Width, Self.Height).Graphics.TextHeight(myLabel.Text, myLabel.Width)
Var hl As Double = Self.BitmapForCaching(Self.Width, Self.Height).Graphics.TextHeight

Var NumberOfLines As Integer = ht / hl

And as in my case I always change the text from outside the window, I use the open event to adapt the dimensions.
It is not perfect, but it woks for me.