Spaces in listitem of listbox

Last post 02-08-2007 11:03 AM by vijaypvb. 7 replies.

Sort Posts:

  • Spaces in listitem of listbox

    02-02-2007, 11:58 AM
    • Member
      17 point Member
    • vijaypvb
    • Member since 02-02-2007, 11:25 AM
    • Posts 27

    Is it possible to have multiple spaces between the words in listitem of the listbox control?? This needs to be done from the code behind!

     

     

  • Re: Spaces in listitem of listbox

    02-02-2007, 12:30 PM
    Answer
    • Star
      10,577 point Star
    • ps2goat
    • Member since 11-17-2006, 5:43 PM
    • Posts 1,934
    The solution from this thread should apply to your situation as well.
    ---------------------------------------
    MCP - Web Based Client Development .NET 2.0
  • Re: Spaces in listitem of listbox

    02-03-2007, 3:47 AM
    • Member
      17 point Member
    • vijaypvb
    • Member since 02-02-2007, 11:25 AM
    • Posts 27
    Yeah that works perfect! Actually I was trying to replace the " " string with hex 0xA0.. but that didnt work.. the decimal chr(160) works perfect.. Thanks for pointing to the solution!
  • Re: Spaces in listitem of listbox

    02-07-2007, 3:06 PM
    • Member
      17 point Member
    • vijaypvb
    • Member since 02-02-2007, 11:25 AM
    • Posts 27

    yeah i thought chr(160) solved the issue replacing the spaces. But in a way it didn't.
    Consider the following list items:
    "1234    Alabama"
    "1237    Arizona"
    Replacing the spaces with chr(160) will work and will be aligned properly in this case.

    Now consider the following list items:
    "99      Alabama"
    "1237    Arizona"
    Replacing the spaces with chr(160) is not aligned properly in the list box. It seems something is happening strange!

    If the width of the same column varies as shown above, then we have problem with the alignment.

    Any solution or work around for this??

  • Re: Spaces in listitem of listbox

    02-07-2007, 3:22 PM
    • Star
      10,577 point Star
    • ps2goat
    • Member since 11-17-2006, 5:43 PM
    • Posts 1,934

    This depends on how you're building your strings. for example, the guy in the other thread had his strings typed out and read them in from a file.  If he wanted to align them, he'd do so manually.

    If you were reading from two sources and putting them together, for example, read in the ID value from one place and the state name from another, you could easily alter the values. Example:

    Let's say you always put the same number of spaces between the ID and the state (4?), and that you the maximum id value has 4 digits (as in the problem above). Currently, your solution is:

    ' assuming ReplaceSpaceWithChr160 is the function we created
    '  that replaces a hardcoded space with the ascii value of a
    '  non-breaking space, and it returns the string of chr(160) chars.
    strListText = strID & ReplaceSpaceWithChr160("    ") & strState

     To make sure that the same number of characters are always present in the ID value, we would use a Pad() function; in this case, we want extra characters to show up on the left, so we would use PadLeft():

    ' assuming ReplaceSpaceWithChr160 is the function we created
    '  that replaces a hardcoded space with the ascii value of a
    '  non-breaking space, and it returns the string of chr(160) chars.
    strListText = strID.PadLeft(4, chr(160)) & ReplaceSpaceWithChr160("    ") & strState
     PadLeft takes total number of characters (4 in this case) and the character to pad with (chr(160) in this case).
    ---------------------------------------
    MCP - Web Based Client Development .NET 2.0
  • Re: Spaces in listitem of listbox

    02-07-2007, 3:40 PM
    • Member
      17 point Member
    • vijaypvb
    • Member since 02-02-2007, 11:25 AM
    • Posts 27
    First I thank you for your time and quick response.
    
    Here is the sample code snippet:
    strText = "99      California  xyz"
    strText = strText.Replace(" ",Chr(160))
    lstState.items.add(strText)
    strText = "1237    Arizona     ab"
    strText = strText.Replace(" ",Chr(160))
    lstState.items.add(strText)
    
    This is done in the code behind. When the content is rendered to the page, the alignment is changed. Can you please try the same example? Or correct me if I am wrong? Thanks again.
    
    (Note:I initially tried with Padright.. that also didnt work)
    
     
  • Re: Spaces in listitem of listbox

    02-07-2007, 4:15 PM
    Answer
    • Star
      10,577 point Star
    • ps2goat
    • Member since 11-17-2006, 5:43 PM
    • Posts 1,934

    No, no. You were right.  My hands don't translate from my brain correctly: it should have been PadRight.

    Anyway, what type of font are you using in your listbox?  The other guy was using New Courier, which is a monospace font.  This means every character will take up the same amount of space, and line up correctly.  This means a capital M takes the same size as a period ( . )

    ---------------------------------------
    MCP - Web Based Client Development .NET 2.0
  • Re: Spaces in listitem of listbox

    02-08-2007, 11:03 AM
    • Member
      17 point Member
    • vijaypvb
    • Member since 02-02-2007, 11:25 AM
    • Posts 27

    Coruier New does the trick! Great. Thanks!!.. I was using the font 'Arial'.. hence it didn't align properly.. My web standard says Arial 12 px to be used in the table contents.. In that way I will be violating by having Courier New font in the list box..

    Thanks for your analysis and solution!! Have a great day.

    Thanks, Vijay

Page 1 of 1 (8 items)