Results 1 to 8 of 8

Thread: Setting Tab Stops for Strings

  1. #1

    Thread Starter
    Hyperactive Member neef's Avatar
    Join Date
    Dec 2001
    Location
    Boston
    Posts
    311

    Setting Tab Stops for Strings

    This is in a RichTextBox.

    I'm making a string of sports stats and I need to have them aligned neatly in the RichTextBox. Here is an example:
    str = "12/24 (tab) 225 yards (tab) 2 TDs (tab) 2 INTs"

    When I use the vbTab constant in place of (tab) I run into problems as the size of the text in each column can be different which makes the tab not balance out correctly.

    I need to stop specifically at say, the 50th character no matter what the length of the characters before it. Something like TAB() for printing but this doesn't seem to work for strings.

    Thanks,
    neef
    Intermediate Level Programmer Extraordinaire

  2. #2
    Member fixo's Avatar
    Join Date
    Aug 2011
    Location
    SPb, Russia
    Posts
    45

    Re: Setting Tab Stops for Strings

    You could be use repeated blanks instead, eg:
    Code:
         Dim txt As String = "12/24" & StrDup(8, " "c) & "225 yards" & StrDup(8, " "c) & "2 TDs" & StrDup(8, " "c) & "2 INTs"
            Me.RichTextBox1.Text = txt
    See if this will be looking better

  3. #3
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Setting Tab Stops for Strings

    Code:
    Dim Rows As String() = _
        { _
            String.Concat("5/24".TabStop, "5 yards".TabStop, "112 TDs".TabStop, "11"), _
            String.Concat("12/24".TabStop, "225 yards".TabStop, "2 TDs".TabStop, "11"), _
            String.Concat("1".TabStop, "2".TabStop, "3".TabStop) _
        }
    
    
    For Each row In Rows
        Console.WriteLine("[{0}]", row)
    Next
    
    For Each row In Rows
        If row.Length > 50 Then
            Console.WriteLine("[{0}]", row.Substring(0, 49))
        Else
            Console.WriteLine("[{0}]", row)
        End If
    Next
    The following goes into a code module
    Code:
    <System.Diagnostics.DebuggerStepThrough()> _
    <System.Runtime.CompilerServices.Extension()> _
    Function TabStop(ByVal value As String) As String
        Return value & System.Text.RegularExpressions.Regex.Unescape("\t")
    End Function

  4. #4

    Thread Starter
    Hyperactive Member neef's Avatar
    Join Date
    Dec 2001
    Location
    Boston
    Posts
    311

    Re: Setting Tab Stops for Strings

    This wasn't an exact solution but it set me in the right direction. I just needed to do some figuring to determine the correct value of StrDup's first input (instead of a 8 or 6 or whatever). I wanted the total spaces including the text to be 12; I subtracted 12 minus the string length and used that number as the number of duplicate spaces.

    Works like a charm now, thanks!

    One question, in StrDup(8, " "c) what is the c after the quotes for? That is new to me and I'm curious.
    Intermediate Level Programmer Extraordinaire

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: Setting Tab Stops for Strings

    Kind of a pain to do all that just to duplicate the functionality of a ListView (in detail mode) in an RTB. Is there a reason why you have to do it that way?
    My usual boring signature: Nothing

  6. #6
    Member fixo's Avatar
    Join Date
    Aug 2011
    Location
    SPb, Russia
    Posts
    45

    Re: Setting Tab Stops for Strings

    Quote Originally Posted by neef View Post
    This wasn't an exact solution but it set me in the right direction. I just needed to do some figuring to determine the correct value of StrDup's first input (instead of a 8 or 6 or whatever). I wanted the total spaces including the text to be 12; I subtracted 12 minus the string length and used that number as the number of duplicate spaces.

    Works like a charm now, thanks!

    One question, in StrDup(8, " "c) what is the c after the quotes for? That is new to me and I'm curious.
    this means a char - (c)

  7. #7
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Setting Tab Stops for Strings

    Type Characters. Appending the literal type character C to a single-character string literal forces it to the Char data type. Char has no identifier type character.

    http://msdn.microsoft.com/en-us/libr...v=VS.100).aspx

  8. #8

    Thread Starter
    Hyperactive Member neef's Avatar
    Join Date
    Dec 2001
    Location
    Boston
    Posts
    311

    Re: Setting Tab Stops for Strings

    Quote Originally Posted by Shaggy Hiker View Post
    Kind of a pain to do all that just to duplicate the functionality of a ListView (in detail mode) in an RTB. Is there a reason why you have to do it that way?
    That's a good question. I didn't want a spreadsheet look, but rather a newspaper style.
    Intermediate Level Programmer Extraordinaire

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