VS 2015 [RESOLVED] Draw on PicBox Font Error-VBForums
Results 1 to 10 of 10

Thread: [RESOLVED] Draw on PicBox Font Error

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2016
    Location
    Sabattus, ME
    Posts
    76

    Resolved [RESOLVED] Draw on PicBox Font Error

    Hi,

    I have a PictureBox that I am drawing a font on.
    However, when I select a font from the dropdown combo...
    IF the font name doesn't have a FontStyle it will fail to draw (placing red "X" on canvas)

    Here is the issue with my code.

    #1 This code works on it's own (not using code further down #2)... but always chooses the Italic. But I always want Regular if available

    Code:
    Dim pic_font = New Font(CStr(cbFontName.Text), CInt(cbFontSize.Text), FontStyle.Regular _
            'Or FontStyle.Bold Or FontStyle.Italic, GraphicsUnit.Pixel)
    #2 If I use the below code to try and get just the Style of the font that is available (because each font can be different)
    Then NO font drawing works at all... (get red X on canvas for any font) it completely cancels out the paint event on Picbox

    Code:
    ' Create a FontFamily object.
            Dim myFontFamily As New FontFamily(CStr(cbFontName.Text))
            
            'Test what style Is available
            If myFontFamily.IsStyleAvailable(FontStyle.Regular) Then
                Dim pic_font = New Font(CStr(cbFontName.Text), CInt(cbFontSize.Text), FontStyle.Regular, GraphicsUnit.Pixel)
                
            ElseIf myFontFamily.IsStyleAvailable(FontStyle.Bold) Then
                Dim pic_font = New Font(CStr(cbFontName.Text), CInt(cbFontSize.Text), FontStyle.Bold, GraphicsUnit.Pixel)
                
            ElseIf myFontFamily.IsStyleAvailable(FontStyle.Italic) Then
                Dim pic_font = New Font(CStr(cbFontName.Text), CInt(cbFontSize.Text), FontStyle.Italic, GraphicsUnit.Pixel)
          
            End If
    Any insight will be appreciated
    Last edited by technipixel; Dec 8th, 2017 at 01:50 PM.
    My Sites: TechniPixel Solutions Pixelink Media
    Windows 7 10G RAM NIV-GForce GT 640 Graphics Card Visual Studio 2015
    Software Code: VB6, VBA, .NET (C# & VB), Lazarus (Free Pascal), InnoSetup
    Other: sql, php, html, CSS, javascript, actionscript

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    39,532

    Re: Draw on PicBox Font Error

    1. You have specified that you want Italic... if you don't want that, remove "Or FontStyle.Italic" (which is a Binary Or, and therefore adds italic to your conditions)

    2. You are declaring (and setting) variables inside the If/ElseIf clauses, and not using them at all. Declare a variable before the If, set the value inside the If/ElseIf clauses, and use it after the End If

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2016
    Location
    Sabattus, ME
    Posts
    76

    Re: Draw on PicBox Font Error

    Quote Originally Posted by si_the_geek View Post
    1. You have specified that you want Italic... if you don't want that, remove "Or FontStyle.Italic" (which is a Binary Or, and therefore adds italic to your conditions)

    2. You are declaring (and setting) variables inside the If/ElseIf clauses, and not using them at all. Declare a variable before the If, set the value inside the If/ElseIf clauses, and use it after the End If

    1) I just can't remove the Italics, because some fonts ONLY offer an Italic style

    That is why I am trying #2 code block.
    I can ONLY USE one line....
    Dim pic_font = New Font(CStr(cbFontName.Text), CInt(cbFontSize.Text), ??? whichever style is true ???, GraphicsUnit.Pixel)
    NOTE: This is in the Paint Event of the PictureBox

    I am using the this #2 block of code to check if a particular font has one or the other two styles.
    So, when the style is identified in the IF/ELSE
    Then I have to set that one line of CODE in it's completed form

    Some fonts only have one Style, so I am trying to find which one it has.
    Using the "pic_font" font declaration.... if a font generates an error (can't find it's style) then it cause the Paint event to break
    My Sites: TechniPixel Solutions Pixelink Media
    Windows 7 10G RAM NIV-GForce GT 640 Graphics Card Visual Studio 2015
    Software Code: VB6, VBA, .NET (C# & VB), Lazarus (Free Pascal), InnoSetup
    Other: sql, php, html, CSS, javascript, actionscript

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    39,532

    Re: Draw on PicBox Font Error

    This line:
    Code:
    'Declare the variable AND assign a value to it
    Dim pic_font = New Font(CStr(cbFontName.Text), CInt(cbFontSize.Text), FontStyle.Regular, GraphicsUnit.Pixel)
    ...does the same as these two lines:
    Code:
    'Declare the variable
    Dim pic_font as Font
    'assign a value to it
     pic_font = New Font(CStr(cbFontName.Text), CInt(cbFontSize.Text), FontStyle.Regular, GraphicsUnit.Pixel)
    To be able to use the variable you either need to move the declaration before the If, or have all of the code to use pic_font duplicated inside each If/ElseIf clause.

    You haven't shown the code where you want to use the font, but duplicating it would almost certainly be a bad choice, so I recommend moving the declaration... and if doing just that solves it, make sure you turn Option Explicit ON (and I strongly recommend always making sure it is On).

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jul 2016
    Location
    Sabattus, ME
    Posts
    76

    Re: Draw on PicBox Font Error

    Quote Originally Posted by si_the_geek View Post
    This line:
    Code:
    'Declare the variable AND assign a value to it
    Dim pic_font = New Font(CStr(cbFontName.Text), CInt(cbFontSize.Text), FontStyle.Regular, GraphicsUnit.Pixel)
    ...does the same as these two lines:
    Code:
    'Declare the variable
    Dim pic_font as Font
    'assign a value to it
     pic_font = New Font(CStr(cbFontName.Text), CInt(cbFontSize.Text), FontStyle.Regular, GraphicsUnit.Pixel)
    To be able to use the variable you either need to move the declaration before the If, or have all of the code to use pic_font duplicated inside each If/ElseIf clause.

    You haven't shown the code where you want to use the font, but duplicating it would almost certainly be a bad choice, so I recommend moving the declaration... and if just doing that solves it, make sure you turn Option Explicit ON (and I strongly recommend always making sure it is On).
    Yeah... I already figured that out from your last post.

    It's working better, now I have to deal with fonts that have other styles.

    My next question is... in theory.... how to I put in place for an error trap for Fonts that just generate unknow errors, BUT NOT LOSE the drawing of the canvas (big red "X")
    My Sites: TechniPixel Solutions Pixelink Media
    Windows 7 10G RAM NIV-GForce GT 640 Graphics Card Visual Studio 2015
    Software Code: VB6, VBA, .NET (C# & VB), Lazarus (Free Pascal), InnoSetup
    Other: sql, php, html, CSS, javascript, actionscript

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jul 2016
    Location
    Sabattus, ME
    Posts
    76

    Re: Draw on PicBox Font Error

    This is what I have that works better...
    Except for unforseen font errors

    Code:
    Dim pic_font As Font
    
            'Test what style Is available
            If myFontFamily.IsStyleAvailable(FontStyle.Regular) Then
                
                pic_font = New Font(CStr(cbFontName.Text), CInt(cbFontSize.Text), FontStyle.Regular, GraphicsUnit.Pixel)
                
            ElseIf myFontFamily.IsStyleAvailable(FontStyle.Bold) Then
                
                pic_font = New Font(CStr(cbFontName.Text), CInt(cbFontSize.Text), FontStyle.Bold, GraphicsUnit.Pixel)
                
            ElseIf myFontFamily.IsStyleAvailable(FontStyle.Italic) Then
                
                 pic_font = New Font(CStr(cbFontName.Text), CInt(cbFontSize.Text), FontStyle.Italic, GraphicsUnit.Pixel)
                
            Else
                MsgBox("An Error with the redraw of the application has occured. Please save your project and re-start application.")
            End If
    My Sites: TechniPixel Solutions Pixelink Media
    Windows 7 10G RAM NIV-GForce GT 640 Graphics Card Visual Studio 2015
    Software Code: VB6, VBA, .NET (C# & VB), Lazarus (Free Pascal), InnoSetup
    Other: sql, php, html, CSS, javascript, actionscript

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jul 2016
    Location
    Sabattus, ME
    Posts
    76

    Re: Draw on PicBox Font Error

    OKAY!!!

    I got it working pretty good.

    THANK YOU si_the geek!!

    Below is the working code with error handling.

    Out of the 600+ fonts.. only 2 generated errors
    Funny, they are both ITALIC

    I tested them in CorelDraw and, yep, they only style available is Italic.
    Yet... the code below doesn't pick it up if that is the only Style.

    WORKING CODE:

    Code:
           ' Create a FontFamily object.
            Dim myFontFamily As New FontFamily(CStr(cbFontName.Text))
           Dim pic_font As Font
            errMsg = "An Error has occured with the selcted font - '" & lastFont & ".' This is an issue with the font itself. The Arial font will be used instead of the selected font."
    
            'Test what style Is available
            If myFontFamily.IsStyleAvailable(FontStyle.Regular) Then
            
                pic_font = New Font(CStr(cbFontName.Text), CInt(cbFontSize.Text), FontStyle.Regular, GraphicsUnit.Pixel)
            
            ElseIf myFontFamily.IsStyleAvailable(FontStyle.Bold) Then
            
                pic_font = New Font(CStr(cbFontName.Text), CInt(cbFontSize.Text), FontStyle.Bold, GraphicsUnit.Pixel)
            
            ElseIf myFontFamily.IsStyleAvailable(FontStyle.Italic) Then
            
                pic_font = New Font(CStr(cbFontName.Text), CInt(cbFontSize.Text), FontStyle.Italic, GraphicsUnit.Pixel)
            
            ElseIf myFontFamily.IsStyleAvailable(FontStyle.Strikeout) Then
            
                 pic_font = New Font(CStr(cbFontName.Text), CInt(cbFontSize.Text), FontStyle.Strikeout, GraphicsUnit.Pixel)
             
            ElseIf myFontFamily.IsStyleAvailable(FontStyle.Underline) Then
             
                 pic_font = New Font(CStr(cbFontName.Text), CInt(cbFontSize.Text), FontStyle.Underline, GraphicsUnit.Pixel)
              
            Else
                fontErr = True
                lastFontUse = True
                MsgBox(errMsg, vbOKOnly + vbCritical, "Font Error")
                lastFontUse = False
                cbFontName.Text = "Arial"
                pic_font = New Font(CStr("Arial"), CInt(cbFontSize.Text), FontStyle.Regular, GraphicsUnit.Pixel)
                fontErr = False
    
            End If
    My Sites: TechniPixel Solutions Pixelink Media
    Windows 7 10G RAM NIV-GForce GT 640 Graphics Card Visual Studio 2015
    Software Code: VB6, VBA, .NET (C# & VB), Lazarus (Free Pascal), InnoSetup
    Other: sql, php, html, CSS, javascript, actionscript

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jul 2016
    Location
    Sabattus, ME
    Posts
    76

    Re: Draw on PicBox Font Error

    Figured it out!!!

    I have to do all combinations... ugh

    Code:
    ElseIf myFontFamily.IsStyleAvailable(FontStyle.Bold Or FontStyle.Italic) Then
       pic_font = New Font(CStr(cbFontName.Text), CInt(cbFontSize.Text), FontStyle.Bold Or FontStyle.Italic, GraphicsUnit.Pixel)
    My Sites: TechniPixel Solutions Pixelink Media
    Windows 7 10G RAM NIV-GForce GT 640 Graphics Card Visual Studio 2015
    Software Code: VB6, VBA, .NET (C# & VB), Lazarus (Free Pascal), InnoSetup
    Other: sql, php, html, CSS, javascript, actionscript

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    39,532

    Re: [RESOLVED] Draw on PicBox Font Error

    You don't have to repeat the code tho, you can just put the styles into an array and loop them:

    Code:
    Dim fontStyleList As FontStyle() = {FontStyle.Regular, FontStyle.Bold, FontStyle.Italic, 
                                        ... , FontStyle.Bold Or FontStyle.Italic}
    fontErr = False
    For Each fontStyle as FontStyle in fontStyleList
        If myFontFamily.IsStyleAvailable(fontStyle) Then
            pic_font = New Font(CStr(cbFontName.Text), CInt(cbFontSize.Text), fontStyle, GraphicsUnit.Pixel)
            fontErr = False
            Exit For
        End If
    Next
    
    If fontErr = True Then
        lastFontUse = True
        MsgBox(errMsg, vbOKOnly + vbCritical, "Font Error")
        ...

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jul 2016
    Location
    Sabattus, ME
    Posts
    76

    Re: [RESOLVED] Draw on PicBox Font Error

    Quote Originally Posted by si_the_geek View Post
    You don't have to repeat the code tho, you can just put the styles into an array and loop them:

    Code:
    Dim fontStyleList As FontStyle() = {FontStyle.Regular, FontStyle.Bold, FontStyle.Italic, 
                                        ... , FontStyle.Bold Or FontStyle.Italic}
    fontErr = False
    For Each fontStyle as FontStyle in fontStyleList
        If myFontFamily.IsStyleAvailable(fontStyle) Then
            pic_font = New Font(CStr(cbFontName.Text), CInt(cbFontSize.Text), fontStyle, GraphicsUnit.Pixel)
            fontErr = False
            Exit For
        End If
    Next
    
    If fontErr = True Then
        lastFontUse = True
        MsgBox(errMsg, vbOKOnly + vbCritical, "Font Error")
        ...
    Thanks man... that is a great idea!!!
    My Sites: TechniPixel Solutions Pixelink Media
    Windows 7 10G RAM NIV-GForce GT 640 Graphics Card Visual Studio 2015
    Software Code: VB6, VBA, .NET (C# & VB), Lazarus (Free Pascal), InnoSetup
    Other: sql, php, html, CSS, javascript, actionscript

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.