locked
Set Form size programmatically RRS feed

  • Question

  • I set the form size in the properties of each form under Width and Height.

    "Suddenly" a couple of the forms changed size even though the size is set in the properties of each form. I could not find in the code any resize change, but this must be happening and I will look closer. In the mean time I decided to programmatically set the size of every form.

    I used 

    Me.Size = New Size(intgFormWidth, intgFormHeight)

    and placed it after each 

            InitializeComponent()

    of every form. In the non-behaving forms (the ones that resize) it worked only after I placed 

    Me.Size = New Size(intgFormWidth, intgFormHeight)

    at the end of the _Load

    From you experience what is the best way to define the form size programmatically?

    Thursday, August 9, 2018 12:18 PM

Answers

  • If you were to look at the designer file for a form, this is where form settings are done from setting the setting for the form properties, I would inspect those settings via in the IDE, project menu, show all files menu item then drill down into the form.designer file. If something seems off that is where you'll find it.

    In regards to where to set properties yourself, in this case form load is correct as doing this in form shown is too late.

    Example for what;s in the designer file of a simple form.

    <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
    Partial Class Form1
        Inherits System.Windows.Forms.Form
    
        'Form overrides dispose to clean up the component list.
        <System.Diagnostics.DebuggerNonUserCode()>
        Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            Try
                If disposing AndAlso components IsNot Nothing Then
                    components.Dispose()
                End If
            Finally
                MyBase.Dispose(disposing)
            End Try
        End Sub
    
        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer
    
        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        <System.Diagnostics.DebuggerStepThrough()>
        Private Sub InitializeComponent()
            Me.Button1 = New System.Windows.Forms.Button()
            Me.SuspendLayout()
            '
            'Button1
            '
            Me.Button1.Location = New System.Drawing.Point(14, 29)
            Me.Button1.Name = "Button1"
            Me.Button1.Size = New System.Drawing.Size(75, 23)
            Me.Button1.TabIndex = 0
            Me.Button1.Text = "Button1"
            Me.Button1.UseVisualStyleBackColor = True
            '
            'Form1
            '
            Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
            Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
            Me.ClientSize = New System.Drawing.Size(288, 192)
            Me.Controls.Add(Me.Button1)
            Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow
            Me.Name = "Form1"
            Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
            Me.Text = "Form1"
            Me.ResumeLayout(False)
    
        End Sub
    
        Friend WithEvents Button1 As Button
    End Class
    
    Last thought, sometimes forms are affected by new updates to Windows or video drivers are updated or installed. 


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by JohnPapa05 Thursday, August 9, 2018 12:46 PM
    Thursday, August 9, 2018 12:41 PM

All replies

  • If you were to look at the designer file for a form, this is where form settings are done from setting the setting for the form properties, I would inspect those settings via in the IDE, project menu, show all files menu item then drill down into the form.designer file. If something seems off that is where you'll find it.

    In regards to where to set properties yourself, in this case form load is correct as doing this in form shown is too late.

    Example for what;s in the designer file of a simple form.

    <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
    Partial Class Form1
        Inherits System.Windows.Forms.Form
    
        'Form overrides dispose to clean up the component list.
        <System.Diagnostics.DebuggerNonUserCode()>
        Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            Try
                If disposing AndAlso components IsNot Nothing Then
                    components.Dispose()
                End If
            Finally
                MyBase.Dispose(disposing)
            End Try
        End Sub
    
        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer
    
        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        <System.Diagnostics.DebuggerStepThrough()>
        Private Sub InitializeComponent()
            Me.Button1 = New System.Windows.Forms.Button()
            Me.SuspendLayout()
            '
            'Button1
            '
            Me.Button1.Location = New System.Drawing.Point(14, 29)
            Me.Button1.Name = "Button1"
            Me.Button1.Size = New System.Drawing.Size(75, 23)
            Me.Button1.TabIndex = 0
            Me.Button1.Text = "Button1"
            Me.Button1.UseVisualStyleBackColor = True
            '
            'Form1
            '
            Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
            Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
            Me.ClientSize = New System.Drawing.Size(288, 192)
            Me.Controls.Add(Me.Button1)
            Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow
            Me.Name = "Form1"
            Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
            Me.Text = "Form1"
            Me.ResumeLayout(False)
    
        End Sub
    
        Friend WithEvents Button1 As Button
    End Class
    
    Last thought, sometimes forms are affected by new updates to Windows or video drivers are updated or installed. 


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by JohnPapa05 Thursday, August 9, 2018 12:46 PM
    Thursday, August 9, 2018 12:41 PM
  • This is how I size my forms. Put this in load event. you could also ditch the centering and change the left and top properties of the form to place it where you wish

            Width = Screen.PrimaryScreen.Bounds.Width - 100
            Height = Screen.PrimaryScreen.Bounds.Height - 50
            CenterToScreen()


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Thursday, August 9, 2018 12:50 PM