I can't remember this one from my old days of Windows programming. I want to change the width of an object when the form size is changed. How do you do this
Ken, the anchor control just anchors my control on the page. It doesn't allow the resize. I docked my control in the right and bottom edges and it appeared towards the bottom of my page. Am I misunderstanding something
I am trying to change just my control width on a resize. If the form is maximized, I want my control to widen so much. If the form is made smaller, I want it to shrink a bit.
Anchor to the side you want to resize. If you want to be able to resize the form, don't you normally resize the right and bottom edges That's where you want to dock.
Thanks for the sample. That is what I did. Here is my code block: Me.listConfig = New System.Windows.Forms.ListBox() Me.listConfig.Anchor = AnchorStyles.Bottom Or AnchorStyles.Right Me.listConfig.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle Me.listConfig.HorizontalScrollbar = True Me.listConfig.Location = New System.Drawing.Point(8, 8) Me.listConfig.Name = "listConfig" Me.listConfig.Size = New System.Drawing.Size(576, 236) Me.listConfig.TabIndex = 0Do you know why my code is keeping the control to the bottom instead of spanning it across
You have anchored your control to the bottom and right. This means that the control will maintain its design-time distance from the bottom and right edges of the form. When you resize the form, the control will maintain those distances, not maintaining the distances from the top and left edge.
If you want the control to expand left-to-right, then you must anchor the control to the left and to the right. If you want it to expand downwards, then you must anchor it to both the top and bottom. Just try the various options, really. There are only a few combinations, and it should be immediately apparent when you've hit the right one.
Try this form. Shows off most of the Anchor property features.
Public Class Form1 Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New() MyBase.New()
'This call is required by the Windows Form Designer. InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) 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. Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents TextBox1 As System.Windows.Forms.TextBox Friend WithEvents TextBox2 As System.Windows.Forms.TextBox <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.Button1 = New System.Windows.Forms.Button() Me.TextBox1 = New System.Windows.Forms.TextBox() Me.TextBox2 = New System.Windows.Forms.TextBox() Me.SuspendLayout() ' 'Button1 ' Me.Button1.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right) Me.Button1.Location = New System.Drawing.Point(176, 144) Me.Button1.Name = "Button1" Me.Button1.TabIndex = 0 Me.Button1.Text = "Button1" ' 'TextBox1 ' Me.TextBox1.Anchor = ((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right) Me.TextBox1.Location = New System.Drawing.Point(8, 8) Me.TextBox1.Name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size(240, 20) Me.TextBox1.TabIndex = 1 Me.TextBox1.Text = "TextBox1" ' 'TextBox2 ' Me.TextBox2.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _ Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right) Me.TextBox2.Location = New System.Drawing.Point(8, 32) Me.TextBox2.Multiline = True Me.TextBox2.Name = "TextBox2" Me.TextBox2.Size = New System.Drawing.Size(240, 104) Me.TextBox2.TabIndex = 2 Me.TextBox2.Text = "TextBox2" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(256, 174) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.TextBox2, Me.TextBox1, Me.Button1}) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False)
Changing component size on windows resize
Erik Reiter
Thanks for the post. It was already set to the top left and still does not work. Any other clue
Jason
Wayne Gibson
Richard Hotchkin
I am trying to change just my control width on a resize. If the form is maximized, I want my control to widen so much. If the form is made smaller, I want it to shrink a bit.
How can this be done
sentonal
J.Z
Thanks for the sample. That is what I did. Here is my code block: Me.listConfig = New System.Windows.Forms.ListBox()
Me.listConfig.Anchor = AnchorStyles.Bottom Or AnchorStyles.Right
Me.listConfig.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.listConfig.HorizontalScrollbar = True
Me.listConfig.Location = New System.Drawing.Point(8, 8)
Me.listConfig.Name = "listConfig"
Me.listConfig.Size = New System.Drawing.Size(576, 236)
Me.listConfig.TabIndex = 0Do you know why my code is keeping the control to the bottom instead of spanning it across
Thanks for all your help so far! ;)
Slowpoke
albin gorkhali
If you want the control to expand left-to-right, then you must anchor the control to the left and to the right. If you want it to expand downwards, then you must anchor it to both the top and bottom. Just try the various options, really. There are only a few combinations, and it should be immediately apparent when you've hit the right one.
Lucc
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
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.
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button()
Me.TextBox1 = New System.Windows.Forms.TextBox()
Me.TextBox2 = New System.Windows.Forms.TextBox()
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right)
Me.Button1.Location = New System.Drawing.Point(176, 144)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'TextBox1
'
Me.TextBox1.Anchor = ((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right)
Me.TextBox1.Location = New System.Drawing.Point(8, 8)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(240, 20)
Me.TextBox1.TabIndex = 1
Me.TextBox1.Text = "TextBox1"
'
'TextBox2
'
Me.TextBox2.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right)
Me.TextBox2.Location = New System.Drawing.Point(8, 32)
Me.TextBox2.Multiline = True
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(240, 104)
Me.TextBox2.TabIndex = 2
Me.TextBox2.Text = "TextBox2"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(256, 174)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.TextBox2, Me.TextBox1, Me.Button1})
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
End Class