|
Post by pandawdy on Aug 9, 2019 17:37:49 GMT -5
I came up with a way to center text.. but I think it's kind of barbaric and it seems like there is a mathematical approach but I can't work it out. In this case, the end result needs to be a string consisting of 10 characters, with empty spaces at the beginning and end.
My method had the user input a string, use LEN to count how many characters, then using SELECT CASE depending on the length of the string. This requires 10 cases. It works great, but is there a better method?
|
|
|
Post by Chris Iverson on Aug 10, 2019 0:55:40 GMT -5
Simplest way would be to get length of string, subtract length from 10 for number of spaces needed, divide by 2 to figure out how many spaces needed on each side. (Obviously with a little adjustment for odd numbers).
Then just add that number of spaces to each side of the string.
|
|
|
Post by Rod on Aug 10, 2019 3:23:43 GMT -5
w=10 a$="?" while a$<>"" input a$ s=w-len(a$)/2 a$=space$(s)+a$+space$(s) print a$ a$="?" wend
However, where is this text displayed, statictext, textbox, texteditor, graphicbox? what kind of text, font? fixed or variable width? Each might have different solutions.
|
|
|
Post by tsh73 on Aug 10, 2019 4:48:23 GMT -5
s=(w-len(a$))/2 LOL with "little ajustment",
s=(w-len(a$))/2 a$=space$(s)+a$+space$(s - len(a$) mod 2)
|
|
|
Post by meerkat on Aug 10, 2019 10:03:19 GMT -5
Have a look at code here. It will list any database and align the columns. I use a function called align$ that lets you align Left,Right, or Center. libertybasiccom.proboards.com/thread/613/lb5-sqlite-maintenance-code-generator' ------------------------------------- ' Align fld$ to left right or center ' of a given width ' ------------------------------------- function align$(fld$,width,lrc$) s = width - len(fld$) fld$ = left$(fld$,width) if s < 1 then align$ = fld$ else b$ = space$(width) if lrc$ = "l" then align$ = fld$;left$(b$,s) if lrc$ = "r" then align$ = left$(b$,s);fld$ if lrc$ = "c" then align$ = left$(b$,int(s / 2));fld$;left$(b$,int(s / 2) + (s and 1)) end if end function
|
|
|
Post by pandawdy on Aug 14, 2019 16:35:12 GMT -5
So I've got a routine I like for centering... and I tried moving it to a sub. Now I'm confused a little. Help me make sure I'm understanding things correctly. A sub does not return values, but a function does. Is this correct?
|
|
|
Post by tsh73 on Aug 14, 2019 17:00:55 GMT -5
Yes. So a function takes ("abba",10) and returns " abba " (spaces added). You can store it, print it, add to another string - whatever.
Now what will be result of a sub? You call it with CALL
CALL yourSub "abba",10 don't you need some result as well? (if it instantly prints " abba " and forgets about it, that' just fine, overwise - not so good)
(there is a way to change parameters passed to sub/function. Read on byRef on a help file)
just my 0.02$
|
|
|
Post by pandawdy on Aug 14, 2019 18:05:12 GMT -5
Yes. So a function takes ("abba",10) and returns " abba " (spaces added). You can store it, print it, add to another string - whatever. Now what will be result of a sub? You call it with CALL CALL yourSub "abba",10 don't you need some result as well? (if it instantly prints " abba " and forgets about it, that' just fine, overwise - not so good) (there is a way to change parameters passed to sub/function. Read on byRef on a help file) just my 0.02$ Thank you. This helps me a lot.
|
|