[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")?
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
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.
Re: How to change font size at runtime
Thanks, jmc.
Quote:
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.
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
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)?
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:
Using var As New SomeDisposableType
'Use var here.
End Using
is equivalent to this:
vb.net Code:
Dim var As New SomeType
Try
'Use var here.
Finally
var.Dispose()
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.
Re: How to change font size at runtime
Quote:
Originally Posted by
Mark@SF
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.
Re: How to change font size at runtime
Point taken...
Quote:
It won't hurt but it is generally pointless.
Thanks :)!