Question Locked

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

0 helpful
12 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hi

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

Martin

12 replies

by tony hopkinson

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.

by martinbullock15

Hi

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

by slayer_

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

by tony hopkinson

and wahtever version of flexgrid was out in 2005.

According to the documentation that should work, but flexgrid is the second naffest component I've seen in decades.

(MSChart being the worst ever !)

http://msdn.microsoft.com/en-us/library/aa227985%28v=vs.60%29.aspx

When are you setting the alignment.
I have some vague memory of forcing the Cols and Rows properties to the correct values as I wasn't doing data bound.

That seemed to screw up on a regular basis
.

by martinbullock15

Hi

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

Thanks

by martinbullock15

Hi

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

by slayer_

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"
Do
   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.
<pre>
Paste your code here, tabs may need to be converted to spaces
</pre>

by martinbullock15

Hi

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

by slayer_

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

by martinbullock15

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

This discussion is closed to new replies.