Solved

Superscripts / Subscripts in VB Labels/Textbox

Posted on 1998-10-23
6
1,377 Views
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
0
Comment
Question by:alexa102398
  • 3
  • 2
6 Comments
 
LVL 7

Expert Comment

by:kamall
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.

Regards
0
 

Author Comment

by:alexa102398
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
0
 
LVL 15

Accepted Solution

by:
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
    Next
    Cls
    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
                Else
                    curbuf = curbuf & tmpchar ' append current character to buffer
                End If
            Next
            ctl.Container.Print curbuf;
            ctl.Container.Font.Size = savedfs ' restore font
            ctl.Container.CurrentY = ctl.Top
        End If
    Next
End Sub

0
Independent Software Vendors: 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

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

Author Comment

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

Expert Comment

by:ameba
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.
0

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

713 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