Keep text field centered when resizing window?

I would like to keep a text field centred in the window when resizing the window. I think this would be possible when unlocking the locks on left and right sides of the text field, but this isn’t possible (there is always one locked). The other option would be to lock both sides of the text field and though this does keep it centred in the window, it also increased the width. It there a way to accomplish the centering without changing the width?

I think you need to position the Textfield by Code in the Resizing or Resized Events.

I don’t see any kind of resizing events for text fields. I placed the width I want on the Open event of the text field, but that doesn’t make a difference:
me.Width = 226

You want to resize the window, not the TextField, so the Events are there:

You may want to have a look at RubberViews I created to keep controls size and place.

There is an evaluation version at rubberviews.com.

1 Like

No, I want to avoid a text field from resizing while resizing a Window. The text field should stay at the same size, but centred in the Window.

Who says that ?

You use the Event to set the TextField Properties; in your case, you compute the Left value to center your Text Field.

I do not (nor Sasha) write you have to resize the TextField…

Your firsts entence was:

I would like to keep a text field centred in the window when resizing the window.

1 Like

The Resized and Resizing events are fired when you resize the main window. You use these Events to call a method that repositions the textfield so it stays centered horizontally.

In both those events I call a method named RepositionTextField

Public Sub RepositionTextField()
  MainTextField.left = (me.Width - MainTextField.Width) / 2
End Sub

That keeps the textfield centred horizontally:

Video of Textfield centred horizontally when resizing window.

1 Like

Thanks Mark, this worked.

Thanks, Emile. I was looking at it the wrong way.

Is there also a way to keep fields in an even proportion while resizing the Window where those fields are placed on? Right now when I for example set the Window to full screen, I have set their locks in a way that only the left field will resize, not the right field, because otherwise the would overlap each other. It doesn’t look nice but it looks better than when they would overlap. Better even though would if both fields would keep the same width. Here is how it looks before and after resizing:

@André_van_Haren For your top block, in white you can simply lock it to left and right and it will keep it as wide as the window (minus your gaps).

For the TextFields you would have to do it with code. In the Resizing / Resized events calculate the width of the panels and set width for both and left for the right hand panel, along the lines of

PanelWidth = ( Window.Width - 60 ) / 2 // Assuming 20 gap left and right and 20 in between
LeftPanel.Width = PanelWidth
RightPanel.Width = PanelWidth
RightPanel.Left = PanelWidth + 40 // Again assuming 20 gap on the left and 20 between.

Obviously you need to do the column labels also.

If you put it in the Resizing event it will work as you drag it. If you put it in the Resized event it will only change when you let go. It is worth putting it in both.

Very similar code to Ian.'s.

Var w As Integer = (Me.Width - 20 * 3) / 2//20 * 3 = left, middle, right gaps
textarea1.Width = w
TextArea2.Width = w
textarea2.Left = Me.Width - (w + 20)//20 = right Gap 

You could also use Containers, Event Definitions, and Inspector Behaviour to make it easier to move and resize them without tons of code for moving both the textarea and label.

Video of application using Containers

Here’s the sample project for the video above: http://mark-sweeney.com/XojoExamples/Textareas.xojo_binary_project.zip

1 Like

This works great, thanks so much Ian.

Thanks Mark!