Solved

Superscripts / Subscripts in VB Labels/Textbox

Posted on 1998-10-23
6
1,300 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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…

770 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