Superscripts / Subscripts in VB Labels/Textbox

Posted on 1998-10-23
Last Modified: 2009-07-29
With a sciebtific VB project I need to use super/sub scripts and othe scientific notation in both labels and text boxes user entries eg H20 or 3 squared or cubed or 10 to the power of 6 as well as Degrees etc
Question by:alexa102398
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2

Expert Comment

ID: 1441348
Unfortunately, VB controls that have a Font property does not allow you to set the font as Superscript or Subscript.
A possible solution is that you print every character the user enters in a text box, you print it on a picture box with the appropriate font size and position. This however involves a lot of work in code to take into consideration the cases where the user deletes a character after typing it, etc.


Author Comment

ID: 1441349
Thanks Ka,a;;
Nothing wrong with the answer and it clarifies our thoughts but would like to know if there is any other alternative to that suggested by kamall
LVL 15

Accepted Solution

ameba earned 100 total points
ID: 1441350
A simple solution:
{ - marks start of block
} - marks end of block
_ - subscript
^ - superscript

Start new project
Set Fontsize of form1 to 10 or bigger.
Add 2 Labels
- caption = "x_{1}^{2} + x_{2}^{2} = 2 x^{4}"
- tag = "SCI"  ' scientific label only for label2
Paste this code:

Private Sub Form_Paint()
    Dim ctl As Control
    ' hide SCI labels
    For Each ctl In Controls
        If TypeOf ctl Is Label And ctl.Tag = "SCI" Then
            ctl.Visible = False
        End If
    For Each ctl In Controls
        If TypeOf ctl Is Label And ctl.Tag = "SCI" Then
            ctl.Container.CurrentX = ctl.Left
            ctl.Container.CurrentY = ctl.Top
            Dim tmpCaption As String, tmplen As Integer, i As Integer
            Dim curlevel As Integer, prevlevel As Integer, curbuf As String
            tmpCaption = ctl.Caption
            Dim savedfs As Single ' saved font size
            savedfs = ctl.Container.Font.Size
            tmplen = Len(tmpCaption)
            curlevel = 0      'normal text
            curbuf = ""    ' empty buffer
            For i = 1 To tmplen
                Dim tmpchar As String
                tmpchar = Mid$(tmpCaption, i, 1)
                If tmpchar = "{" Then ' new block of text
                    Select Case Mid$(tmpCaption, i - 1, 1)
                    Case "_"
                        curbuf = Left$(curbuf, Len(curbuf) - 1)
                        ctl.Container.Print curbuf;
                        curbuf = ""  ' empty buffer
                        prevlevel = 0
                        curlevel = -1 ' set subscript
                        ctl.Container.CurrentY = ctl.Top + 0.5 * ctl.Container.TextHeight("x")
                        ctl.Container.Font.Size = savedfs - 1
                    Case "^"
                        curbuf = Left$(curbuf, Len(curbuf) - 1)
                        ctl.Container.Print curbuf;
                        curbuf = ""  ' empty buffer
                        prevlevel = 0
                        curlevel = 1 ' set superscript
                        ctl.Container.CurrentY = ctl.Top - 0.2 * ctl.Container.TextHeight("a")
                        ctl.Container.Font.Size = savedfs - 1
                    Case Else
                        prevlevel = curlevel
                        curbuf = curbuf & tmpchar ' add character to buffer
                    End Select
                ElseIf tmpchar = "}" Then ' close block of text
                    ctl.Container.Print curbuf;
                    curbuf = ""  ' empty buffer
                    curlevel = prevlevel
                    ctl.Container.CurrentY = ctl.Top
                    ctl.Container.Font.Size = savedfs
                    curbuf = curbuf & tmpchar ' append current character to buffer
                End If
            ctl.Container.Print curbuf;
            ctl.Container.Font.Size = savedfs ' restore font
            ctl.Container.CurrentY = ctl.Top
        End If
End Sub

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

LVL 15

Expert Comment

ID: 1441351
I know this will not work for all containers.
For textboxes you must use RichTextBox.

Author Comment

ID: 1441352
I think this might now give us a solution to the problem.
LVL 15

Expert Comment

ID: 1441353
You will have to check also:
 ctl.Container.Visible=True ' this will allow using tabstrip control
You can also add a little colour (e.g. blue for superscript, red for subscript)
Just set "ctl.Container.ForeColor" at 3 places.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question