Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

Using Print #, Display line at center of user defined page width

Hi,

I am reading one text file which have fixed width(80). This files have few lines which I am reading e.g Line01 = "I am swapnil"
Now my software should read this file and using print # method it should print it into another text file whose width is defined by user. User may select above 80 any width. Say 100 then
Line01 should get printed into new text file at the center of page width(100)

Can somebody give me code for center align???

How to do that?

Regards
swapnil
0
swapnilv
Asked:
swapnilv
1 Solution
 
DhaestCommented:
I guess you'll have to add spaces before you're string to center the text in your file...

dim CenterPos as integer
' centerpos =
' (linewidth / 2) -> give the middle of the line (center of page)
' line01 / 2 -> gives the middle of your string
CenterPos = (100 / 2) - round(len(line01) / 2,0)

Dim NewLine as string
for i = 1 to centerpos
   print " "
next i
print line01
0
 
swapnilvAuthor Commented:
Thanks man..

But the problem remains as it is.. I don't know how to proceed

the code is something like this..

Open TextFile For Output As #Numfile

Then I read line01

   Line01 = "I am swapnil"

    CenterPos = (100 / 2) - Round(Len(Line01) / 2, 0)

Dim NewLine As String
For Val1 = 1 To CenterPos
Print #Numfile, " "
Next
Print #Numfile, line01

It is printing with different lines in wordpad.whereas it should be in one line.

How to implement in one line.. I tried with spc(n) but I am not able to do with that also..I am not sure how to use spc().

Swapnil


dim CenterPos as integer
' centerpos =
' (linewidth / 2) -> give the middle of the line (center of page)
' line01 / 2 -> gives the middle of your string
CenterPos = (100 / 2) - round(len(line01) / 2,0)

Dim NewLine as string
for i = 1 to centerpos
   print " "
next i
print line01


0
 
Xentor_Commented:
I believe you need a semicolon (;) after the Print #Numfile, " " statement.  That tells VB to stay on the same line in certain cases.

I'd actually reccommend doing that a bit differently

CenterPos = (PageWidth - Len(line01)) \ 2
'(Note the use of the backslash (\) instead of the forward slash to do integer division, ignoring fractions entirely)
Print #Numfile, Space(CenterPos) & line01

That should print CenterPos space characters, then your text.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now