Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai
i have windows application,in one Form i give properties AutoSizeMode=GrowAndShrink,in that i remove controls manually,so if controls reduces then automatically form size also decrease.

If Form close and open then reduce the Form size (i.e Form GrowAndShrink) and if i give in coding not reduces size,so pls help me how to handle this in coding.

The below code i write after drag and drop control from Form.

VB
Dim toolb As New Toolbox
toolb.AutoSize = True
toolb.AutoSizeMode = Windows.Forms.AutoSizeMode.GrowAndShrink


Note:i have controls one by one upto 20 buttons,if i click and drag to other form need to reduce Form size(i.e above my code works).if i drag and drop buttons from bottoms then reduce size,but if drag and drop buttons form top or middle buttons it not reduce Form size

Regards
Aravind
Posted
Updated 6-Feb-14 19:15pm
v4
Comments
G-code101 6-Feb-14 11:04am    
Try this after the toolbox is added.... This is basic...
x = the width of the form
y = the height of the form

First import System.drawing

form1.size = new size(x,y)
Aravindba 6-Feb-14 21:43pm    
Toolbox is a Form,in that i have 20 buttons one by one,if i move that buttons i need to decrease the Form Size,but ur code is how work ? actually now work what i need,but buttons only remove from bottoms,if i remove form middle or top not shrink.
G-code101 7-Feb-14 1:08am    
If I am understanding you correctly this all gets done at run time yes?
Aravindba 7-Feb-14 1:11am    
x and y form which form width and height ? same form or other form ? ur code change width and height of form in run time i know,but where u from u get x and y value ?

pls read my description clearly,read note description
G-code101 7-Feb-14 2:35am    
kindly look at the solution I posted...

1 solution

This should get you sparking in the right direction at run time.....


VB
Imports System.Drawing

Public Class ToolBox

    Dim btn As Control
    Dim singleBtnHeight As Integer 
    Dim FormWidth As Integer = 636 'Change this to the size of your form's width property

    Private Sub AddNewButtonToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AddNewButtonToolStripMenuItem.Click
        singleBtnHeight = 26 'This is the Height of the new button...Height of new button set to 26, you can change this value as you like
        Dim newButton As New Button
        With newButton
            .Text = "Left Click to remove me! "
            .Name = "btnClickRemove"
            Dim i As Integer = Me.Controls.Count - 1
            'Change this line where Me.Controls(i).ClientRectangle.Width for your width of the new Button.
            .Size = New Size(Me.Controls(i).ClientRectangle.Width, singleBtnHeight)
            .Dock = DockStyle.Bottom
        End With
        Me.ClientSize = New Size(FormWidth, (Me.ClientSize.Height + singleBtnHeight))
        Me.Controls.Add(newButton)
        AddHandler newButton.Click, AddressOf New_Button_Click

        'Dispose of new control
        newButton = Nothing
    End Sub

    Private Sub New_Button_Click(sender As Object, e As MouseEventArgs)
        singleBtnHeight = 26 'This is the Height of the new button...Height of new button set to 26, you can change this value as you like        
       If e.Button = Windows.Forms.MouseButtons.Left Then
            btn = sender
            If MessageBox.Show("Remove Button?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                Me.Controls.Remove(btn)
                Dim x As Integer = FormWidth
                Dim y As Integer = Me.ClientSize.Height - singleBtnHeight
                Me.ClientSize = New Size(x, y)
                'Dispose Variables.
                x = Nothing
                y = Nothing
                singleBtnHeight = Nothing
            End If
        End If
    End Sub
End Class
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900