Web Development



Visual Basic - Spaces added when String starts with a number.

By martinbullock15 ·

Hope someone can help.

I have made a little program which lists a data from a SQL database in to a flex grid but for some reason when the String starts with a number it adds loads of white spaces in the field which the user sees (not always the same amount of spaces), i have tried just passing the String to a normal text box on the form and this comes out correct but as soon as it is displayed in to the Flex Grid it adds the white spaces.

I have tried using Trim before i pass the value to the Flex Grid with no luck.

Any suggestions would be very grateful with any sample code!!

Kind Regards


This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Answers

Collapse -

It's not spaces it's right justify

by Tony Hopkinson In reply to Visual Basic - Spaces add ...

The thimble brained jobsworth who wrote it had each cell autojustify baseed on the first character in it.

If I remeber right, there's a columns collection on th grid with a justify property.
Force it to left and it will sort itself out.

Collapse -

How to set Left Jusify

by martinbullock15 In reply to It's not spaces it's righ ...


Thanks for that, that makes sense of what is happening now the question is how to set the FlexGrid Column in question to always be Left Justify with data which starts with letters or numbers?

I have tried a few different things but none have worked so far:

flxProduct.ColAlignment(1) = flexAlignLeftCenter

flxProduct.CellAlignment = flexAlignLeftCenter

The above gave me errors either not declared or not part of the Flex Grid libary. Although i believe 2nd one does work in VB6 as i was looking at a different program that uses Flex Grids and that is used to CenterCenter, with no errors.

Then i found the number which matched to the options so i tried to make everything Algin Left using:

flxProduct.CellAlignment = 1

Nothing happened data which started with letters stayed left and numbers stayed right, so i tried to make everything Align right:

flxProduct.CellAlignment = 7

and again everything stayed as it was.

Any suggestions would be very grateful as a thing which seems soooo simple is taking so long to sort out.

Kind Regards

Collapse -

A week ago...

by TobiF In reply to How to set Left Jusify

A similar problem was discussed in another place. (Google knew, I didn't)

Collapse -

Thats for HTML

by martinbullock15 In reply to A week ago...


Thanks for your reply but the link seems to be help for HTML where as i'm looking for VB .NET


Collapse -


by Slayer_ In reply to How to set Left Jusify

Are you cstr'ing everything before assigning it to the flex grid?

Collapse -

Cstr code

by martinbullock15 In reply to cstr?


Thanks for you post, no i wasn't and i was just wondering if you could expand further to how i would do this.

Would i need to assign a temp field to first use Cstr and then assign that output into the flex grid or should i able to use CStr straight onto the value to be placed in to the Flex Grid (like below):

Me.flxProduct.Text = CStr(SQLRecSet2.Fields("STK_Name").Value)

I tried this but it made no difference.

I also tried the code below with no difference:

varTrim = CStr(SQLRecSet2.Fields("STK_Name").Value)
Me.flxProduct.Text = varTrim

Thanks again for all your suggestions

Collapse -

Now I am not sure I understand

by Slayer_ In reply to Cstr code

Are you staying that the text box is getting spaces added to it?
'Remove all leading and trailing spaces (helps with CHAR type fields)
Me.flxProduct.Text = TRIM$(CStr(SQLRecSet2.Fields("STK_Name").Value))

That should help if the spaces are before the number.

If they are between the number and the words, you can try something like this

Dim sFinal as string

'Loops through and find all double spaces, replace with a single space.
'Helps fix formated fields, such as excel formatting ex
'"1. Data"
sFinal = Replace$(CStr(SQLRecSet2.Fields("STK_Name").Value)," "," ")
Loop Until sFinal = Replace$(CStr(SQLRecSet2.Fields("STK_Name").Value)," "," ")

Me.flxProduct.Text = sFinal

Also, FYI you can post code in TR by wrapping it with the pre tag.
Paste your code here, tabs may need to be converted to spaces

Collapse -

Same again... How to override the Align setting in Flex Grid VB.NET

by martinbullock15 In reply to Now I am not sure I under ...


Thanks for your reply

The spaces are at the start, and i tried your first suggestion but it didn't make any difference.

I am wondering if anyone could help me with the first suggestion that i was given which was about the Text Align Setting within the Flex Grid when the first character is a number it is setting the algin to the right as it is sitting perfectly to the right when the data starts with a number but is fine when the data starts with a letter.

It's like when you open a fresh Excel Spreadsheet and type "a" into one cell it aligns left then if you type "1" into another cell it aligns right.

So if anyone has any ideas of how to override the Align setting in the Flex Grid for VB.NET (i'm using VB 2008 Software) and this is to algin the TEXT within the column of the Flex Grid.

Thanks again to everyone!!

Kind Regards

Collapse -

So you want it all left aligned, why not cheat a bit

by Slayer_ In reply to Same again... How to over ...

And put a space in front of all the data you pass to the flex grid. Thus it is all non numeric.

Collapse -

Very Clever!!

by martinbullock15 In reply to Same again... How to over ...

Hi SinisterSlay

That was very simple and very clever at the same time that has worked a treat and now everything is just one space along which you can't tell!!

Needless to say i won't forget that trick in a hurry!

Thanks to everyone who sent suggestions, but if anyone knows the "Real" way to solve my problem please post, but the forced space works fine.

Kind Regards

Related Discussions

Related Forums