Results 1 to 6 of 6

Thread: Subscripts

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2012
    Posts
    5

    Question Subscripts

    How do I format the numbers so that they are subscripts? I get an output of X1Y1 + X2Y2 +…, but ideally I would like it to be X1Y1 + X2Y2 +… so that it is easier to read.

    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim n, i, count As Integer
            Dim string2 As String = ""
            Dim string3 As String = ""
    
            i = 1
            n = 10
    
            For count = i To n - 1
                string2 &= "X" & count.ToString & "Y" & count.ToString & " + "
            Next
    
            string3 &= "X" & n.ToString & "Y" & n.ToString
    
            lbl_ans.Text &= string2 & string3
        End Sub
    End Class

  2. #2
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Subscripts

    Without resorting to a RichTextBox and applying formatting, you'll need to use the Subscript Digit characters (unicode characters U+2080 -> U+2089). Since you only have ten characters to display, I would simply set up a mapping from the integer to the string - if it was a variable number you might want to set up some kind of generalised function.

    vbnet Code:
    1. Public Class Form1
    2.  
    3.     Private _subscripts As New Dictionary(Of Integer, String) From {
    4.         {1, ChrW(2081)},
    5.         {2, ChrW(2082)},
    6.         {3, ChrW(2083)},
    7.         {4, ChrW(2084)},
    8.         {5, ChrW(2085)},
    9.         {6, ChrW(2086)},
    10.         {7, ChrW(2087)},
    11.         {8, ChrW(2088)},
    12.         {9, ChrW(2089)},
    13.         {10, ChrW(2081) & ChrW(2080)}}
    14.  
    15.  
    16.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    17.         Dim n, i, count As Integer
    18.         Dim string2 As String = ""
    19.         Dim string3 As String = ""
    20.  
    21.         i = 1
    22.         n = 10
    23.  
    24.         For count = i To n - 1
    25.             string2 &= "X" & _subscripts(count) & "Y" & _subscripts(count) & " + "
    26.         Next
    27.  
    28.         string3 &= "X" & _subscripts(n) & "Y" & _subscripts(n)
    29.  
    30.         lbl_ans.Text &= string2 & string3
    31.     End Sub
    32. End Class

    Your only problem then is to find a font that supports those characters, or you get little boxes instead of your subscripts.

  3. #3
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Subscripts

    Don't forget those numbers are hexadecimal, so you'll need to include the hexadecimal literal prefix
    Code:
        Private _subscripts As New Dictionary(Of Integer, String) From {
        {1, ChrW(&H2081)},
        {2, ChrW(&H2082)},
        {3, ChrW(&H2083)},
        {4, ChrW(&H2084)},
        {5, ChrW(&H2085)},
        {6, ChrW(&H2086)},
        {7, ChrW(&H2087)},
        {8, ChrW(&H2088)},
        {9, ChrW(&H2089)},
        {10, ChrW(&H2081) & ChrW(&H2080)}}
    As for fonts, Google says try Palatino Linotype, or Calibri which I think is only supplied with Windows 7.

  4. #4

    Thread Starter
    New Member
    Join Date
    Nov 2012
    Posts
    5

    Re: Subscripts

    Without resorting to a RichTextBox and applying formatting
    How would I do this with the richtextbox?

    if it was a variable number you might want to set up some kind of generalised function.
    I would like this to be for any number not just one through ten.

    Your only problem then is to find a font that supports those characters, or you get little boxes instead of your subscripts.
    Yes I am getting little boxes. Even with arial unicode ms font which supposedly supports it.
    http://www.fileformat.info/info/unic...and_subscripts

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2012
    Posts
    5

    Re: Subscripts

    Quote Originally Posted by Inferrd View Post
    Don't forget those numbers are hexadecimal, so you'll need to include the hexadecimal literal prefix
    Code:
        Private _subscripts As New Dictionary(Of Integer, String) From {
        {1, ChrW(&H2081)},
        {2, ChrW(&H2082)},
        {3, ChrW(&H2083)},
        {4, ChrW(&H2084)},
        {5, ChrW(&H2085)},
        {6, ChrW(&H2086)},
        {7, ChrW(&H2087)},
        {8, ChrW(&H2088)},
        {9, ChrW(&H2089)},
        {10, ChrW(&H2081) & ChrW(&H2080)}}
    As for fonts, Google says try Palatino Linotype, or Calibri which I think is only supplied with Windows 7.
    Thank you. Those changes did the trick. Palatino Linotype and Calibri also works with windows 8. I guess now I would have to create a function so that it works for any number.

  6. #6
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Subscripts

    In making the function, I would first convert the integer to a string with the "D" format string. This will ensure you get just integral digits. Then spin through each of the characters in the string which you know are simply the digits 0->9 (assuming you ignore negative numbers!). You can change the lookup dictionary to be (Of Char,String) and map "0"c to ChrW(&H2080) (thanks for the correction, Inferred!) and so on. For each character, lookup the string and append that string to your value that will be returned (you may want to investigate the StringBuilder class in System.Text namespace)

Tags for this Thread

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