Can I automatically force all Controls in a VBoxContainer to have the same width?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Pennycook

I’ve created a simple text box, which consists of a NinePatchRect that automatically resizes based on its parent Label. I’d like to instantiate this text box multiple times in a VBoxContainer, and have each box grow to match the width of the largest text box in the list.

Current Behavior:
Current behavior

Desired Behavior:
Desired behavior

The only way I have found to do this so far is to attach a script that iterates over all of the labels to find the maximum width, and then set the minimum size of each label based on that value. It feels like there should be a solution to this using just containers and size flags, but I can’t figure it out.

I’ve uploaded a simple example project here.

:bust_in_silhouette: Reply From: AuthorSan

I’ve downloaded your example project, but I have a question. Are you also creating the text for the labels on the fly or do you already know the length of your longest text?

On the fly. The text for each label comes in from a file, and I don’t know ahead of time which labels will be read from the file.

Pennycook | 2020-08-21 23:24

Oh, okay. But you do know the longest line in that file, right? Why do you put it in and see the max size that your NinePatchRect will be having and use it for all other rects?

AuthorSan | 2020-08-22 06:50

That’s essentially what I am doing in the script that I mentioned.

I don’t understand why it’s necessary, though. The CenterContainer for each box is resizing as I’d hope, but the Label inside isn’t. If I use the built-in Button Controls then they also resize to fill the VBoxContainer.

Is there something wrong with the way that I’m nesting the different Controls in the Scene?

Pennycook | 2020-08-22 16:13

:bust_in_silhouette: Reply From: Pennycook

Clearly I need to pay better attention to the documentation. The issue here is that I’m trying to use a CenterContainer, which the documentation clearly describes as having the following behavior (emphasis mine):

CenterContainer keeps children controls centered. This container keeps all children to their minimum size, in the center.

If I use a MarginContainer instead, the Label resizes as I expected.