Results 1 to 9 of 9

Thread: [RESOLVED] How to change font size at runtime

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2013
    Location
    San Francisco, CA
    Posts
    528

    Resolved [RESOLVED] How to change font size at runtime

    I would like to change the font size of a DataGridView control at runtime and be able to display the new font size without having to restart the application.

    Here's my code for changing the font size.

    Code:
        Sub GridColumnFont(myGrid As DataGridView, fontSize As Integer)
    
            Dim strMethodName = New System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name    '...this procedure's name
    
            Dim flgDebug As Boolean = False   '...debug/test purposes only
            Dim f As Font = Nothing
    
            Try
                If flgDebug Then Debug.WriteLine("{0} ({1})", strMethodName, Date.Now)
    
                f = New Font(family:=myGrid.Font.FontFamily, emSize:=fontSize, unit:=GraphicsUnit.Point, style:=FontStyle.Regular)
    
                For i As Integer = 0 To myGrid.ColumnCount - 1
                    If myGrid.Columns(i).GetType = GetType(DataGridViewTextBoxColumn) AndAlso myGrid.Columns(i).Visible Then
                        myGrid.Columns(i).DefaultCellStyle.Font = f
                    End If
                Next
    
            Catch ex As Exception
                HandleError(ex, True)
    
            Finally
                f.Dispose()
                f = Nothing
    
            End Try
    
        End Sub
    Is it possible to change font size at runtime ("on-the-fly")?

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: How to change font size at runtime

    You. Are. Changing the font size at runtime... you called it "emSize"...
    https://docs.microsoft.com/en-us/dot...tframework-4.8

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to change font size at runtime

    You should simply be able to set the Font property of the grid and that will filter down to the rows, columns and cells.

    That said, why are you disposing the Font you just created? The whole point of disposal is to release resources once you're finished with them. Are you finished with that Font? I would think not.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2013
    Location
    San Francisco, CA
    Posts
    528

    Re: How to change font size at runtime

    Thanks, jmc.

    That said, why are you disposing the Font you just created? The whole point of disposal is to release resources once you're finished with them. Are you finished with that Font? I would think not.
    I removed the font object disposal/destroy from my code and everything works.

    Can you point me to a source that would explain why/when an object should be disposed and set to Nothing (destroyed)? This is a topic that I need to better understand.

    Thanks again for all your help.

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: How to change font size at runtime

    you .Dispose an object that has the .Dispose method (not all do, as not all need it), IF you created it, AND when you are done with it. That's pretty much it in a nutshell. The problem in this case was that you weren't done with the font you had created - it was still in use, right up until you .Disposed it. That's when it no longer became useful to the grid.
    Typically for those cases where you do create an object that isn't going to stick aorund, AND has the IDispose interface immplemented (which is where .Dispose method comes from) you can use the Uses keyword... which will allow you to create an object and use it... it will then be automatically disposed at the end when the End Uses is encountered. You don't need to do anythign further. But that works for short lived objects, and wouldn't have helped in this case either.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2013
    Location
    San Francisco, CA
    Posts
    528

    Re: How to change font size at runtime

    techgnome -

    Thanks for the brief tutorial re: the Dispose method.

    Is it good practice to set an object = Nothing in the Finally section of a Try/Catch block for any object that I’ve declared in my code (whether or not the IDispose interface is implemented)?
    Last edited by Mark@SF; Apr 21st, 2020 at 03:33 PM.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to change font size at runtime

    Just a small correction to tg's post: the keyword of interest is Using. This:
    vb.net Code:
    1. Using var As New SomeDisposableType
    2.     'Use var here.
    3. End Using
    is equivalent to this:
    vb.net Code:
    1. Dim var As New SomeType
    2.  
    3. Try
    4.     'Use var here.
    5. Finally
    6.     var.Dispose()
    7. End Try
    Once you enter the Using block, the object you created is guaranteed to be disposed, even if a Return statement is hit or am exception is thrown.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to change font size at runtime

    Quote Originally Posted by Mark@SF View Post
    Is it good practice to set an object = Nothing in the Finally section of a Try/Catch block for any object that I’ve declared in my code (whether or not the IDispose interface is implemented)?
    It won't hurt but it is generally pointless. Objects to which there are no longer any references become eligible for garbage collection, meaning that the system can reclaim the memory they occupy. Disposing an object also speeds up this process because it means that the garbage collector has one less step to perform in cleaning up that object. By setting a variable to Nothing, you remove that reference and possibly make the object that it referred to eligible for garbage collection sooner.

    The thing is, unless that variable remains in scope and referring to that variable for some time afterwards, you have gained nothing. For instance, a local variable falls out of scope at the end of the method it's declared in. Setting that variable to Nothing just before the method completes gains you nothing because the variable ceases to exist soon after so the reference to the object is removed anyway. Generally speaking, you would only set member variables to Nothing and only when the object they are a member of lives on much longer than the object they refer to once you're done with it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2013
    Location
    San Francisco, CA
    Posts
    528

    Re: How to change font size at runtime

    Point taken...

    It won't hurt but it is generally pointless.
    Thanks !

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width