Solved

Autosizing a Form based on DataGridView size?

Posted on 2014-01-28
1
674 Views
Last Modified: 2014-01-29
I have a simple form that contains a Label, DataGridView and a Panel and within the Panel, a Label and ComboBox.

What I'm trying to do is autosize the form and not sure how to calculate the dimensions of the DGV. Here is my code.

    Private Sub CalculateControlPositions()
        Try
            lblItemInfo.Top = 0
            lblItemInfo.Left = 0
            lblItemInfo.Width = Me.Width
            lblItemInfo.Height = 28

            pnl.Top = lblItemInfo.Height + dgvSpecs.Height
            pnl.Left = 0

            dgvSpecs.Top = lblItemInfo.Height

            rowHght = dgvSpecs.RowCount * dgvSpecs.RowTemplate.Height

            Me.Size = New Size(dgvSpecs.Width + 26, rowHght + pnl.Height + 26)
            Me.ClientSize = New Size(dgvSpecs.Width, rowHght + pnl.Height)

            EH.ErrorMessage = ""

        Catch ex As Exception
            EH.ErrorMessage = "CalculateControlPositions() - " & ex.Message & "~E"
        End Try

        EH.ProcessMessages(frmMain, frmMain.sbr, EH.ErrorMessage)
    End Sub

Open in new window

0
Comment
Question by:BlakeMcKenna
1 Comment
 
LVL 14

Accepted Solution

by:
Matti earned 500 total points
ID: 39817492
The event is a bit problem, you found some event from the grid or just use the code that updates it.

grid.left = 0
Me.height = Grid.height
Me.width = Grid.width

Of cause if you need to fit this perfect you need calculate all borders and bar sizes from API call's and use them to minus some of the grid size, you can estimate this, but it's not good in all operating system settings.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now