troubleshooting Question

Zoom in/out for datagridview

Avatar of craigdev
craigdev asked on
Visual Basic.NET
1 Comment1 Solution4029 ViewsLast Modified:
In excel when you hold down the control key and scroll the mouse wheel the scale of the grid will increase or decrease so you can easily zoom in or out.

My users want the same functionality, so the code below mostly achieves this.

Only trouble is, holding down ctrl when using the scroll wheel will also move the scroll bar left or right. How do I disable this part so that the same column stays where it is?
Private Sub DataGridView1_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseWheel
        If My.Computer.Keyboard.CtrlKeyDown Then
            Dim newHeight As Int32 = DataGridView1.RowTemplate.Height + (e.Delta / 120) * 2
            DataGridView1.RowTemplate.Height = newHeight
            For Each row As DataGridViewRow In DataGridView1.Rows
                row.Height = newHeight
            For Each col As DataGridViewColumn In DataGridView1.Columns
                col.Width += (e.Delta / 120) * 40
            Dim newFont As Font
                newFont = New System.Drawing.Font(Me.DataGridView1.Font.FontFamily, Me.DataGridView1.Font.Size + (e.Delta / 120) * 1, Me.DataGridView1.Font.Style, Me.DataGridView1.Font.Unit)
            Catch ex As Exception
                newFont = Me.DataGridView1.Font
            End Try
            Me.DataGridView1.Font = newFont
        End If
    End Sub
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 1 Comment.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros