TextWidth in TextGadget

Just starting out? Need help? Post your questions and find answers here.
User avatar
marcoagpinto
Addict
Addict
Posts: 947
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

TextWidth in TextGadget

Post by marcoagpinto »

Hello!

Imagine that I have a string, for example: "Filename:N/A".

I want to create a TextGadget with it, but with the exact size of the text above.

I tried:
TextWidth("Filename:N/A")
but I got an error as it seems to work only with drawing text

Is there a way to make it work with normal gadgets?

Thank you!

Kind regards,
>Marco A.G.Pinto
---------------
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: TextWidth in TextGadget

Post by srod »

GadgetWidth() with the mode set to #PB_Gadget_RequiredSize should give you what you are after.
I may look like a mule, but I'm not a complete ass.
User avatar
marcoagpinto
Addict
Addict
Posts: 947
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

Re: TextWidth in TextGadget

Post by marcoagpinto »

srod wrote:GadgetWidth() with the mode set to #PB_Gadget_RequiredSize should give you what you are after.
Thank you for your suggestion, but it doesn't work:

Code: Select all

    TextGadget(#TEXT_PANEL_BOX_DICTIONARY_NUMBER_WORDS,10,y,200+10,16,"# Words:"+Str(number_of_words))
    ResizeGadget(#TEXT_PANEL_BOX_DICTIONARY_NUMBER_WORDS,GadgetX(#TEXT_PANEL_BOX_DICTIONARY_NUMBER_WORDS),GadgetY(#TEXT_PANEL_BOX_DICTIONARY_NUMBER_WORDS),GadgetWidth(#TEXT_PANEL_BOX_DICTIONARY_NUMBER_WORDS),#PB_Gadget_RequiredSize)
    TextGadget(#TEXT_PANEL_BOX_DICTIONARY_NUMBER_WORDS_ASTERISK,GadgetX(#TEXT_PANEL_BOX_DICTIONARY_NUMBER_WORDS)+GadgetWidth(#TEXT_PANEL_BOX_DICTIONARY_NUMBER_WORDS),y,16,16,"*")
    SetGadgetColor(#TEXT_PANEL_BOX_DICTIONARY_NUMBER_WORDS_ASTERISK,#PB_Gadget_FrontColor,#Red) 
The asterisk is no longer in the "Filename:N/A" but in the number of items being changed.

Could you help?

Thank you!
User avatar
marcoagpinto
Addict
Addict
Posts: 947
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

Re: TextWidth in TextGadget

Post by marcoagpinto »

ahhhh... it is working... sorry.

Thank you again!
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: TextWidth in TextGadget

Post by #NULL »

You have put the constant as a parameter to ResizeGadget() instead of GadgetWidth().
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Re: TextWidth in TextGadget

Post by Marc56us »

With variable names that long, I wonder how you can see anything? :shock:
(I hope it's just for the example?)

:idea: For the compiler, spaces don't count, add spaces and split, split, split lines ( , + - )
The indentation is automatic and will tell you if you cut in the right place

Code: Select all

TextGadget(#TEXT_PANEL_BOX_DICTIONARY_NUMBER_WORDS_ASTERISK,
           GadgetX(#TEXT_PANEL_BOX_DICTIONARY_NUMBER_WORDS) +
           GadgetWidth(#TEXT_PANEL_BOX_DICTIONARY_NUMBER_WORDS, #PB_Gadget_RequiredSize),
           y, 16, 16, "*")
It's easier to see if you've forgotten a parameter, or put it in the wrong place
:wink:
User avatar
marcoagpinto
Addict
Addict
Posts: 947
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

Re: TextWidth in TextGadget

Post by marcoagpinto »

Marc56us wrote:With variable names that long, I wonder how you can see anything? :shock:
(I hope it's just for the example?)

:idea: For the compiler, spaces don't count, add spaces and split, split, split lines ( , + - )
The indentation is automatic and will tell you if you cut in the right place

Code: Select all

TextGadget(#TEXT_PANEL_BOX_DICTIONARY_NUMBER_WORDS_ASTERISK,
           GadgetX(#TEXT_PANEL_BOX_DICTIONARY_NUMBER_WORDS) +
           GadgetWidth(#TEXT_PANEL_BOX_DICTIONARY_NUMBER_WORDS, #PB_Gadget_RequiredSize),
           y, 16, 16, "*")
It's easier to see if you've forgotten a parameter, or put it in the wrong place
:wink:
Buaaaaaaaaaaa... lately I have been using variables bigger and bigger to make it easier to remember what they are for.

Thank you guys for all the suggestions.
Post Reply