Anchoring a label to the right so autoresize expands to the left.

New to Gambas? Post your questions here. No question is too silly or too simple.
Post Reply
ak2766
Posts: 7
Joined: Sunday 3rd April 2022 4:59am
Location: Melbourne, Australia

Anchoring a label to the right so autoresize expands to the left.

Post by ak2766 »

I'm very new to Gambas but not to programming.

I'm creating an SMS application that has a large text area for entering an SMS message. Beneath that, I have 2 counters (as labels):

1) keeps count of characters typed, and
2) keeps count of SMS Count (being that an SMS is limited to 160 characters by SMSGlobal - my provider)

In design phase, my form looks as can be seen in attachment:
Selection_00550.png
Selection_00550.png (11.13 KiB) Viewed 1632 times
At run time, it appears as can be seen in attachment:
Selection_00551.png
Selection_00551.png (10.55 KiB) Viewed 1632 times
Is it possible to have a label be anchored on its right edge so that when it auto-resizes, it expands towards the left, similar to right justification in Word documents?

I know that I can change the Label's text property (which is how I've coded it for now), but I'd prefer to do it in code.


Cheers,
ak.

UPDATE: I've identified that I can play around with .X and .Width to come up with a runtime solution:

Code: Select all

smsCount.X = SMSMessage.X + SMSMessage.Width - smsCount.Width
If this is the only solution to my problem, then so be it.
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Anchoring a label to the right so autoresize expands to the left.

Post by cogier »

Hi and welcome to the forum.

Forum tip: - Use the 'gb' button for your code, the code will look better

Image
smsCount.X = SMSMessage.X + SMSMessage.Width - smsCount.Width
And now to the Dark art of Gambas expanding forms! This is well worth getting your head around.

If the below is complicated, have a look at the attached program. Note that there is no code needed to get this to work.

1/. If you change the Form property Arrangement to Vertical. This will cause all the components to stack from the top to the bottom.
2/. Set the Form property Padding to 5. This will give you a nice border around your Form.
3/. I suggest the SMSMessage is a TextArea. Set the properties Expand and Wrap to True. The Expand will cause the TextArea to fill whatever space is available.
4/. To stop the 2 'Count' labels from piling on top of each other, you need to add a Hbox (Container tab of the toolbox) set it's Height to 28 and put the 2 Labels in it.
5/. Set the property Expand to True for both labels and on the right label change the Alignment to Right.
6/. Put the 2 Buttons in a HBox as well and add a Spring in between them to push them apart.
7/. You can add some Panels to act as voids if you want to move items further apart, see attached program.

Image
ak2766-0.0.1.tar.gz
(12.01 KiB) Downloaded 130 times
ak2766
Posts: 7
Joined: Sunday 3rd April 2022 4:59am
Location: Melbourne, Australia

Re: Anchoring a label to the right so autoresize expands to the left.

Post by ak2766 »

cogier wrote: Sunday 3rd April 2022 11:59am Hi and welcome to the forum.

Forum tip: - Use the 'gb' button for your code, the code will look better
...
Thanks for the tip and detailed explanation on my problem, cogier, really appreciated.
Post Reply