Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String Concatenation 1

Status
Not open for further replies.

AlastairOz

Technical User
Jul 27, 2010
81
AU
I a using variables in a message box for the message text.
eg:
"any text" + variable1 + "Some other Text" + variable2

One of the variables is from a number field.
I get an error message when trying to concatenate the
text with this variable.
Is there a way to solve this?

 
Sure, just convert the numeric variable to string:
Code:
any text" + variable1 + "Some other Text" + STR(variable2,10,2)

Borislav Borissov
VFP9 SP2, SQL Server 2000,2005 & 2008.
 
Thanks that works!
Also, is there a way to justify the text in a message box?
 
For doing concatenation one of the easiest ways is to utilize the text merging capabilities of VFP. ie:

Code:
lcText = TextMerge("any text<< variable1 >>Some other Text<< variable2 >>")

* or

text to lcText texmerge noshow
any text<< variable1 >>Some other Text<< variable2 >>
endtext

* or

set textmerge to memvar lcText on noshow 
\\any text<< variable1 >>
\\Some other Text<< variable2 >>
set textmerge to
set textmerge off
Using merging it is easier to format the text as you want.

Cetin Basoz
MS Foxpro MVP, MCP
 
is there a way to justify the text in a message box?

Do you mean you want to pad it with space to the right or left? If so, you can use either PADR() or PADL() to do that. But I'm not sure why you would want to do that if message only takes up one line.

If you
Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
Rather than using a fixed length Numeric-to-Character conversion such as STR(variable2,10,2) I might suggest that you trim it at the start with ALLTRIM(STR(variable2)) or TRANSFORM() (look in your VFP Help file if needed) and then pad it out like you would do for any character string using the functions that Mike suggested above - PADL(), PADR(), PADC().

Good Luck,
JRB-Bldr
 

For numeric-to-character conversion, in many cases you can go straight to PADL(), PADR() or alike, skipping the ALLTRIM(STR(variable2)) or TRANSFORM() steps. PAD?() functions take a number as an argument, too:

Code:
nnn=123.456
?PADL(nnn,5,'0')  && result: 123.4
?PADL(nnn,6,'0')  && result: 123.45 
?PADL(nnn,7,'0')  && result: 123.456
?PADL(nnn,8,'0')  && result: 0123.456
?PADR(nnn,8,'0')  && result: 123.4560

FOR nnn=1 TO 10
    nn_string="Text"+PADL(nnn,2,'0')
    ?nn_string
NEXT

*!*The results will be strigs "Text01" through "Text10"

I learned this long ago from someone on this site (I think, Tamar Granor) and use it a lot.
 
Thank you all for your replies.
I used the ALLTRIM(STR() to get the result I was after.
Cetin: I will have a look at textmerge, thanks.

In regards to the justifying:
The message box has a few lines of text. They all are left justified.
I would like to have all the lines centre justified.
Is that possible?
 
If you're putting this in a report you can center there with properties. That is also true for a screen.
 
I would like to have all the lines centre justified.
Is that possible?

So you want the text centred within the box? That's got nothing to do with justification.

You can centre a string by using PADC(). So the following will work:

Code:
MESSAGEBOX(PADC("Hello World", 60))

But that won't do anything useful if the string is split over multiple lines.

Also, keep in mind that the width the of the box increases with the width of the string (up to a certain limit). So fiddling around with the string in this way won't always produce the desired effect.

If you really want exact control over the alignment of the text, you will have to create a form to use in place of the built in message box. Personally, I wouldn't bother.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top