Results 1 to 17 of 17

Thread: RichTextBox SubScript

  1. #1
    Guest
    Well guys, I have seen this asked before. About how to make text SubScript. As like H2O would be HO (lower 2). I just wrote this for all yous that want it.
    Treat it with care .
    It's not the best.
    But this gives you the idea.


    Code:
    Private Sub SubScript(rtb As RichTextBox, SubString As String, Size As Integer)
    
        SubString$ = Mid$(Trim$(rtb.Text), InStr(rtb.Text, SubString$), Len(SubString$))
        With rtb
            .SelStart = InStr(1, rtb.Text, SubString$) - 1
            .SelLength = Len(SubString$)
            .SelFontSize = Size
            .SelText = SubString$
        End With
        
    End Sub
    
    
    Usage
    
    Private Sub Command1_Click()
        Call SubScript(RichTextBox1, "MyString", 8)
    End Sub

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Eeeh, am i correct:

    you search up the first substring and then change it's size to 8? well how do you do superscript then?
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3
    Guest
    I don't know. Because with SubScript, you just set the Font's size to a smaller number. With SuperScript, if you were to raise the Font's size to a larger number, you'd just get a huge number. So I'm not sure about how to go about doing so. And I don't think anyone knows. RichTextBox simply does not support these properties/functions. Maybe we should beg Microsoft to put them in next version of RichTextBox. I'm sure they could do it. They can do just about anything...because they are Microsoft!

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Well i know, or i just checked it out. The rtfTExt for a
    "1 km^2" is the following:
    Code:
    {\rtf1\ansi\ansicpg1252\deff0\deftab720{\fonttbl{\f0\fswiss MS Sans Serif;}{\f1\froman\fcharset2 Symbol;}{\f2\fswiss\fprq2 Arial;}{\f3\froman Times New Roman;}}
    {\colortbl\red0\green0\blue0;}
    \deflang1053\pard\plain\f2\fs24 1 km\plain\f2\fs16\up12 2\plain\lang1035\f3\fs20 
    \par }
    so i guess it does something like you did but with some more code, like up16.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5
    Guest
    Keep in mind, however, that chaning the font size is not "ture subscript." Programs like MS Word, will actually have it lower (or raised) yet, the font size remains the same.

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Nope, i got that straight from a word document i made with wordpad.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7
    Guest
    I was referring to Matthew Gates's function.

  8. #8
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Well but the rtf format is doing something similar, raising the character and using a smaller font
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  9. #9
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    This was something I was actually messing with a few weeks ago as I was making an MDI text editor with an RTB and wanted to add super/subscripts. This was before I got distracted and did other stuff... but anyway, you can use the .SelCharOffset property to change how text appears. If it's 0, the text is normal. Positive integers will be superscripted by the number of twips, and negatives will be subscripted. That's as far as I went though because I got too tied up in my grad work . It would need some tweaking to make it look right, ie figuring out a good number of twips to use for sub and super based on font size. This might be what things like Word actually do (but that's just a guess on my part).
    I'm baaaack...
    VB5 Professional Edition, VC++ 6
    Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se

    I feel special because I finally figured out how to loop midis: Post link
    I'm a fanatic too

  10. #10
    Guest
    Hey hey! Go easy on me Megatron, I tried to manipulate the Subscript function (or html function, whatever..). It works, not the best, but it's good .

    And Kaverin, I will take a look at your idea/what you tried as well.

  11. #11
    Guest
    Kaverin, the .SelCharOffset property isn't exactly the best. It does raise the text a bit, but it isn't very great. Doesn't lower it at all.

    http://msdn.microsoft.com/library/de...haroffsetx.htm

  12. #12
    Fanatic Member
    Join Date
    Feb 2000
    Location
    The Netherlands
    Posts
    715
    I think a RichTextBox supports Subscript and Superscript, but the vb control doesn't.
    But, =) on vbaccelerator is a rtb control that creates the rtb with the api. And that control supports subscript and supercript.
    Oetje
    oetje@home.nl
    93606776
    Visual Basic 6, Windows 2000

    Never pet a burning dog

  13. #13
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    Matt, I don't know exactly what you did, but it works fine for me. It both superscripts and subscripts. Did you take into account that the .SelCharOffset is in twips, and you should convert that value to points (based on the font size) in order to make it look best? At first I didn't do that, and was just using something like half of the font size, and because .SelCharOffset was in twips, I didn't see any effect. Just multiply the font size by 20 to get the points into twips, and I used 1/3 of that value as a distance for the formatting and it looked fine.
    I'm baaaack...
    VB5 Professional Edition, VC++ 6
    Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se

    I feel special because I finally figured out how to loop midis: Post link
    I'm a fanatic too

  14. #14
    Guest
    Here is an updated method.
    Code:
    'Used for SubScripts
    Function SubScript(rtb As RichTextBox, text As String, amount As Integer, size As Integer)
        rtb.SelCharOffset = amount
        rtb.SelFontSize = size
        rtb.SelText = text
    End Function
    
    'Used for SuperScripts
    Function SuperScript(rtb As RichTextBox, text As String, amount As Integer, size As Integer)
        rtb.SelCharOffset = amount
        rtb.SelFontSize = size
        rtb.SelText = text
    End Function
    Add a RichTextBox and a CommandButton
    Code:
    'Prints H20 -4 (2 in SubScript and -4 in Superscript)
    RichTextBox1.SetFocus
    RichTextBox1.SelText = "H"
    SubScript RichTextBox1, "2", -40, 8
    RichTextBox1.SelText = "O"
    SuperScript RichTextBox1, "-4", 40, 8

  15. #15
    Hyperactive Member dsy5's Avatar
    Join Date
    Jul 2000
    Location
    Lockport, NY
    Posts
    362
    Megatron,

    I tried your code and the "O" is also subscripted. And the font size does not change at
    all. Tried it in VB6.
    Donald Sy - VB (ab)user

  16. #16
    Guest
    This should fix the problems.
    Code:
    'Used for SubScripts
    Function SubScript(rtb As RichTextBox, text As String, amount As Integer, size As Integer)
        prevfnt = rtb.SelFontSize
        rtb.SelCharOffset = amount
        rtb.SelFontSize = size
        rtb.SelText = text
        rtb.SelCharOffset = 0
        rtb.SelFontSize = prevfnt
    End Function
    
    'Used for SuperScripts
    Function SuperScript(rtb As RichTextBox, text As String, amount As Integer, size As Integer)
        prevfnt = rtb.SelFontSize
        rtb.SelCharOffset = amount
        rtb.SelFontSize = size
        rtb.SelText = text
        rtb.SelCharOffset = 0
        rtb.SelFontSize = prevfnt
    End Function
    
    Private Sub Command1_Click()
        'Prints H20 -4 (2 in SubScript and -4 in Superscript)
        RichTextBox1.SetFocus
        RichTextBox1.SelText = "H"
        SubScript RichTextBox1, "2", -40, 8
        RichTextBox1.SelText = "O"
        SuperScript RichTextBox1, "-4", 40, 8
    End Sub

  17. #17
    Addicted Member
    Join Date
    Jul 2012
    Location
    Wiltshire, England
    Posts
    211

    Re: RichTextBox SubScript

    Only a 12 year old thread, but I had the same problem, but I only needed 1 text box with a couple of subscripts and I was doing this in .Net.
    My Solution was to type the text in a normal text box on my form and lay it out so it looked right except for the subscript, then screenshot it and load it into Paint and then cut and paste the smaller number twos that happened to be on the form and put them over the normal size ones. I initially redid all the text in word, but couldn't find the right background colour so abandoned that. When saving the file, the jpg format sometimes dithered the background, but a bmp seemed fine. Put the picture in a picturebox and all look fine.

    Kristian

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