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

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!

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

729 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