CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2006
    Location
    Belgium
    Posts
    8

    Question Quick question about adding whitespace to a string

    Hi everyone, I'm working on this project where I need to have tons of comboboxes with certain numbers and codes in them. Every one of these codes has a certain description and I would like to show them all this way:

    "code | description"

    Now, the problem with this is when the codes have different lengths. Usually it only differs one char, so I thought it was just a matter of adding a little check on the length of the code and then adding a blank in front of the "|" when the code had the shorter form, but the problem with this is that it just never alligns nicely. There will always be two or three pixels of difference between the length in front of the "|" for the shorter and the longer form of the code.

    If anyone has any suggestions (adding a certain length of white pixels for example, although I think this might be a bit far fetched) or any other ways to display the codes and descriptions nicely, I'd be immensely greatful!

    Thanks in advance!!

  2. #2
    Join Date
    Nov 2004
    Location
    Poland
    Posts
    1,355

    Re: Quick question about adding whitespace to a string

    1) U can do "owner drawing" in ComboBox to achieve that (more to do but it can look very cool

    2) U can use fixed pitch font like "Courier New" for example (easy way) and than U have to make all strings equal in count of characters.

    Best regards,
    Krzemo.

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Quick question about adding whitespace to a string

    Just for interest sake, I think you can use the Space function, once you have determinde the length of the first string, to add spaces in front of the second string

  4. #4
    Join Date
    Aug 2004
    Posts
    391

    Re: Quick question about adding whitespace to a string

    try looking into padright and padleft functions of string class

  5. #5
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    1,080

    Re: Quick question about adding whitespace to a string

    Padding strings with spaces won't do you much good unless you're using a fixed-width font. This is exactly what you want.
    Tutorials: Home & Learn | Start VB.NET | Learn VB.NET | C# Station | GotDotNet | Games in VB.NET 101 Samples: 2002 | 2003 | 2005 | More .NET 2.0 (VB.NET, C#) Articles: VB.NET | C# | ASP.NET | MoreFree Components: WFC | XPCC | ElementsEx | VBPP | Mentalis | ADO.NET/MySQL | VisualStyles | Charting (NPlot, ZedGraph) | iTextSharp (PDF) | SDF (CF) ● Free Literature: VB 2005 (eBook) | VB6 to VB.NET (eBook) | MSDN Magazine (CHM format) ● Bookmarks: MSDN | WinForms .NET | ASP.NET | WinForms FAQ | WebForms FAQ | GotDotNet | Code Project | DevBuzz (CF) ● Code Converter: C#/VB.NET | VB.NET/C# | VS 2005 add-in

  6. #6
    Join Date
    Feb 2006
    Location
    Belgium
    Posts
    8

    Re: Quick question about adding whitespace to a string

    Thank you all for the help! It took me a day or so to finetune the owner drawn combobox, but it looks great now, I'm glad I didn't go for the easy way :P

    For those of you who are interested in this matter, I think this article is very useful too.

    Thanks for the "Space" function too, I didn't know it existed and it sure is a lot easier to count the spaces in "Space(10)" then in
    Code:
     "          "
    Last edited by hangman; February 28th, 2006 at 08:26 AM.

  7. #7
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    1,080

    Re: Quick question about adding whitespace to a string

    There is no Spaces class. Space is a function, specifically the Microsoft.VisualBasic.Strings.Space function.
    Tutorials: Home & Learn | Start VB.NET | Learn VB.NET | C# Station | GotDotNet | Games in VB.NET 101 Samples: 2002 | 2003 | 2005 | More .NET 2.0 (VB.NET, C#) Articles: VB.NET | C# | ASP.NET | MoreFree Components: WFC | XPCC | ElementsEx | VBPP | Mentalis | ADO.NET/MySQL | VisualStyles | Charting (NPlot, ZedGraph) | iTextSharp (PDF) | SDF (CF) ● Free Literature: VB 2005 (eBook) | VB6 to VB.NET (eBook) | MSDN Magazine (CHM format) ● Bookmarks: MSDN | WinForms .NET | ASP.NET | WinForms FAQ | WebForms FAQ | GotDotNet | Code Project | DevBuzz (CF) ● Code Converter: C#/VB.NET | VB.NET/C# | VS 2005 add-in

  8. #8
    Join Date
    Feb 2006
    Location
    Belgium
    Posts
    8

    Re: Quick question about adding whitespace to a string

    Who said anything about a space class?

    I just tend to mix up those names, a space class would be quite silly I think, so it was even more silly of me not to see that I had written it wrong...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured