Solved

Superscripts / Subscripts in VB Labels/Textbox

Posted on 1998-10-23
6
1,219 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 15

Expert Comment

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

Author Comment

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

Expert Comment

by:ameba
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now