• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1785
  • Last Modified:

How do I display ascii character omega in a picture box with regular letters?

I'm relatively new to visual basic and wrote a program to calculate ohms law. Displaying units for volts and amps is easy of course, but what of displaying something like picture1.print answer & "k" & "ohms", replacing ohms with the omega symbol. I can change the picture box font to symbols, then char(87) equals the omega symbol, but then everything else is junk. How do I insert the omega symbol and not change anything else and get a proper display?
0
xetron
Asked:
xetron
  • 3
  • 2
  • 2
  • +1
2 Solutions
 
List244Commented:
Private Declare Function GetTextExtentPoint32 Lib "gdi32" Alias "GetTextExtentPoint32A" (ByVal hdc As Long, ByVal lpsz As String, ByVal cbString As Long, lpSize As Size) As Long

Private Type Size
  Width As Long
  Height As Long
End Type

Private Sub Command1_Click()
Dim CharSize As Size
Picture1.Print answer & "k"
GetTextExtentPoint32 Picture1.hdc, answer & "k", 4, CharSize
Picture1.Font = "symbol"
Picture1.CurrentX = CharSize.Width * Screen.TwipsPerPixelX
Picture1.CurrentY = 0
Picture1.Print Chr(87)
picture1.font = "Times New Roman" ''' Or whichever starting font was...
End Sub

This will write the first part, change the font, then write the OMEGA after
the first part. Hope that helps.
0
 
dbrckoviCommented:
Try this:
This function will raplace all "Ohm" in your text with actual Omega signs.
Just use PrintOhms function in the place where you originaly used Picture1.Print
'------------------------------------------
Private Sub Command1_Click()
    PrintOhms "Result of calculation was 13 kOhm, but measured value was 12.5 kOhm."
End Sub

Sub PrintOhms(Text As String)
    Text = Replace(Text, "Ohm", Chr(87), 1, -1, vbTextCompare)

    OldY = Picture1.CurrentY
    OldFont = Picture1.Font

    For Char = 1 To Len(Text)
        Character = Mid(Text, Char, 1)
        If Asc(Character) = 87 Then
            Picture1.Font = "Symbol"
        Else
            Picture1.Font = OldFont
        End If
        Picture1.Print Character
        Picture1.CurrentY = OldY
        Picture1.CurrentX = Picture1.TextWidth(Left(Text, Char))
    Next Char

    Picture1.Font = OldFont
End Sub
'-----------------------------------------------
0
 
Erick37Commented:
Here is another alternative using a unicode characterset and the TextOutW API call:

Option Explicit

Private Declare Function TextOutW Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal lpString As String, ByVal nCount As Long) As Long

Private Sub Command1_Click()

    Dim sOMEGA As String
    Dim sOut As String
   
    sOMEGA = ChrW$(&H3A9) 'This is the unicode omega value
   
    'Use Verdana cause it has the Omega symbol
    Picture1.FontName = "Verdana"
    Picture1.FontSize = 14
   
    Picture1.Cls
   
    'Here is a sample string
    sOut = "The answer is 330 " & "k" & sOMEGA
   
    'Print the unicode text, remember that x and y are in pixels
    Call TextOutW(Picture1.hdc, 0, 20, StrConv(sOut, vbUnicode), Len(sOut))

End Sub
0
Industry Leaders: 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!

 
Erick37Commented:
Note:
It seems that most fonts have the Ohm sign at ChrW(&H2126) so you are not limited to only Verdana.  You can use almost any unicode truetype font and get the ohm sign using TextOutW.
0
 
xetronAuthor Commented:
Thanks for all your help. I think List244 is on the right track. Here is a chunk of the code I'm trying to improve. z = 0, k = 1000, m = 1000000, ohm1 is single.

If (z < ohm1) And (ohm1 < 1) Then
    Picture1.Print Round((ohm1 * k), 2) & " m" & "ohms"
    ElseIf (z < ohm1) And (ohm1 < k) Then
    Picture1.Print Round((ohm1), 2) & " ohms"
    ElseIf (k <= ohm1) And (ohm1 <= 999999) Then
    Picture1.Print Round((ohm1 / k), 2) & " k" & "ohms"
    ElseIf (m <= ohm1) Then
    Picture1.Print Round((ohm1 / m), 2) & " M" & "ohms"
  End If

I hope this helps.
Thanks again,
Viktor
0
 
dbrckoviCommented:
try this:

  If (z < ohm1) And (ohm1 < 1) Then
        PrintOhms Round((ohm1 * k), 2) & " m" & "ohm"
        ElseIf (z < ohm1) And (ohm1 < k) Then
        PrintOhms Round((ohm1), 2) & " ohms"
        ElseIf (k <= ohm1) And (ohm1 <= 999999) Then
        PrintOhms Round((ohm1 / k), 2) & " k" & "ohm"
        ElseIf (m <= ohm1) Then
        PrintOhms Round((ohm1 / m), 2) & " M" & "ohm"
  End If

'add this function
Sub PrintOhms(Text As String)
    Text = Replace(Text, "Ohm", Chr(87), 1, -1, vbTextCompare)

    OldY = Picture1.CurrentY
    OldFont = Picture1.Font

    For Char = 1 To Len(Text)
        Character = Mid(Text, Char, 1)
        If Asc(Character) = 87 Then
            Picture1.Font = "Symbol"
        Else
            Picture1.Font = OldFont
        End If
        Picture1.Print Character
        Picture1.CurrentY = OldY
        Picture1.CurrentX = Picture1.TextWidth(Left(Text, Char))
    Next Char

    Picture1.Font = OldFont
End Sub
0
 
List244Commented:
Your new function:
If (z < ohm1) And (ohm1 < 1) Then
    PrintToPicture Picture1, Round((ohm1 * k), 2) & " m"
    ElseIf (z < ohm1) And (ohm1 < k) Then
   PrintToPicture Picture1, Round((ohm1), 2)
    ElseIf (k <= ohm1) And (ohm1 <= 999999) Then
   PrintToPicture Picture1, Round((ohm1 / k), 2) & " k"
    ElseIf (m <= ohm1) Then
   PrintToPicture Picture1, Round((ohm1 / m), 2) & " M"
  End If

Declarations:
Private Declare Function GetTextExtentPoint32 Lib "gdi32" Alias "GetTextExtentPoint32A" (ByVal hdc As Long, ByVal lpsz As String, ByVal cbString As Long, lpSize As Size) As Long

Private Type Size
  Width As Long
  Height As Long
End Type

Private Function PrintToPicture(PBox As PictureBox, PrintText As String) 'Print function/autowrite omega
Dim CharSize As Size, PFont As String, CurrY As Long
CurrY = PBox.CurrentY
PFont = PBox.Font
PBox.Print PrintText
GetTextExtentPoint32 PBox.hdc, PrintText, Len(PrintText), CharSize
PBox.Font = "symbol"
PBox.CurrentX = CharSize.Width * Screen.TwipsPerPixelX
PBox.CurrentY = CurrY
PBox.Print Chr(87)
PBox.Font = PFont
End Function

There, that should make it easy on you. Now whenever you need to write the
omega you simply do:

PrintToPicture Pictureboxname,"TEXT TO PRINT"

The rest is done for you.
0
 
List244Commented:
dbrckovi and myself have both provided for the user what he/she has asked. I recommend
splitting points.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now