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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

align columns in a list box

Status
Not open for further replies.

skibascott

IS-IT--Management
Mar 18, 2004
82
US
I am trying to align the columns in a text box. Here is the snippet of code that pertains:
Code:
LastCol = 2
LastRow = UBound(RecSet_Array, 2)
  
Response.Write "<SELECT NAME='sheetlist' SIZE=20 onChange=FillFields()>"
For Row = 0 To LastRow                
    Response.Write "<OPTION value=" & RecSet_Array(1, Row) & ">"
		For Col = 0 To LastCol 
		   Response.Write RecSet_Array(Col,Row) & "&nbsp&nbsp"
             Next
  Next
	Response.Write "</OPTION>"
	Response.Write "</SELECT>"

Because the first item in each list box option has a variable length and the "&nbsp" I used is a fixed length, the alignment is thrown off. I believe the way to go would be to create a variable length whitespace string that could be inserted in place of the 2 non-breaking spaces? I have tried this to no avail.
Am I on the right track or is there another way?
 
I'm not sure your idea is really going to work in general. What I do know, though, is that you don't need to space out every one of the entries, since it's the longest entry that's going to determine the width of the menu, isn't it?

Part of the problem is going to be that the menu (combobox) text uses a variable width font, so sometimes your padding will be enough, sometimes it will be too little, and sometimes it will be too much.

Generally, I'd think that you'd want to determine the length (in characters) of your widest menu item, and if it was insufficient, pad that one out. I think you'd want to work out some formula (based on testing), to determine the average number of characters per space. Let's say for the sake of the example that you determine that there are .8 characters per full space, on average (I have no idea, it could be 1.2, or whatever).

Then you'd determine the length in spaces you want all of your menus to be. Let's say for this example that it's 30 spaces. You could then calculate how much to pad the longest one like this:
Code:
Ratio = .8
Full = 30

MaxItem = Len(yourLongestItemHere)

PadLength = (Full - MaxItem) * Ratio

Padding = ""
For i = 1 to PadLength
    Padding = Padding & "&nbsp;"
Next
then when it's time to output that particular line you'd use
Code:
Response.Write(yourLongestItemHere & Padding)
while otherwise you'd just write out the item.
 
Thank you for the suggestion Genimuse.
I believe what I have here is quite similar. The Length function is getting the number of characters in the item I want to display and I am multiplying that value by 5(#of characters in &nbsp) and subtracting this value from 100(#of characters in the &nbsp string that I am using the Left function on. Now the Left function should return the number of spaces that I want to append to the end of each displayed item. But, I am getting an invalid argument error on the line using the Left Function: Whitespace=Left
Code:
LastCol = 2
LastRow = UBound(RecSet_Array, 2)
  
Response.Write "<SELECT NAME='sheetlist' SIZE=20 onChange=FillFields()>"
For Row = 0 To LastRow                
    Response.Write "<OPTION value=" & RecSet_Array(1, Row) & ">"
    For Col = 0 To LastCol 
	StringLength = Len(RecSet_Array(Col, Row))
	AmountToTrim = 100 -(StringLength * 5)
        WhiteSpace = Left("&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp",AmountToTrim)
        Response.Write RecSet_Array(Col, Row) & Whitespace
    Next
Next
Response.Write "</OPTION>"
Response.Write "</SELECT>"

I have checked the value of StringLength and AmountToTrim and they always contain valid values. Can anyone see where I am going wrong here?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top