Resize windowform1
hi,
i am new to VB.net(Windows application)
how to dynamically increase/decrease control size
when form size is increased/decreased by user
i tried using tablelayoutpanel/Anchor/docking.i find it using very diffcult to use.(I think i dont know how to use it )
is there material available in online about how to use tablelayoutpanel/anchor/docking
Thanks in advance
[400 byte] By [
narend] at [2007-12-25]
Not to be rude... But could you use the '?' indicator to show which are your questions. Its not apparent if you have one or two questions and reading them without punctuation could result in very
different answers. Thanks.
hi i am sorry about that..
how to dynamically increase/decrease control size when form size is increased/decreased by user ?
Thanks
Controls have an .anchor property: this property locks one or more edges of the control to the same relative position to the appropriate edge of the form. This may do what you need, but won't scale controls proportionally.
Further, VS2005 (VB2005 Express) has the TableLayout control, which can allow controls to 'scale' proportionally, which you may want to experiment with.
Edit: I believe Johan's post containing the link, described the TableLayout control, also.
hi SJWhiteley
thanks for your reply.
i am using VS2005
sorry i am new to this VB.net windows application
when i used tablelLayout control , all my controls gets overlaped when form size is increased/decreased
Have you determined that the Anchor property won't work for you? Anchoring and docking are probably the two most common ways to resize a control with it's parent container.
You can also handle the Form Resize revents and change the size of your controls as needed before, during, or after the form is resized. But like I say, most of the time anchoring is all you need.
hi rkimble ,
thanks for your reply
Do you have any sample code that resize contols when form size is changed?
so that i can handle in form resize event
There's different ways to go about it depending on the desired effect, but here's an example that will resize a picturebox based on the size changes made to the form:
Dim oldSize As Size
Private Sub Form2_ResizeBegin(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.ResizeBegin
Me.oldSize = Me.Size
End Sub
Private Sub Form2_ResizeEnd(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.ResizeEnd
Dim widdif As Single = Me.Size.Width - Me.oldSize.Width
Dim hgtdif As Single = Me.Size.Height - Me.oldSize.Height
Me.PictureBox1.Width += widdif
Me.PictureBox1.Height += hgtdif
End Sub
Hi thanks for code
i need to set this for all the controls in my form?
it is not working when i clicked the maximize button of the form
working when i resize the form thro mouse.
Sorry, number one, I forgot a line of code! That was one issue.
As far as maximize not working, like I said, you'll have to play with it to suit your needs. In this case, switching to the SizeChanged event will work:
Dim oldSize As Size = Me.Size
Private Sub Form2_ResizeBegin(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.ResizeBegin
Me.oldSize = Me.Size
End Sub
Private Sub Form2_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged
Dim widdif As Single = Me.Size.Width - Me.oldSize.Width
Dim hgtdif As Single = Me.Size.Height - Me.oldSize.Height
If (Me.PictureBox1.Width + widdif > 0) And (Me.PictureBox1.Height + hgtdif > 0) Then
Me.PictureBox1.Width += widdif
Me.PictureBox1.Height += hgtdif
Me.oldSize = Me.Size
End If
End Sub
I forgot the line in orange and the If statement in yellow is necessary to keep the controls from changing size after being shrunk to 0x0.
Hi rkimble
Thanks.
When i have to do for all controls in the form , i have write with for loop?
Dim widdif As Single = Me.Size.Width - Me.oldSize.Width
Dim hgtdif As Single = Me.Size.Height - Me.oldSize.Height
Dim i As Int16
For i = 0 To Me.Controls.Count - 1
If (Me.Controls.Item(i).Width + widdif > 0) And (Me.Controls.Item(i).Height + hgtdif > 0) Then
'MsgBox(Me.Controls.Item(i).Name)Me.Controls.Item(i).Width += widdifMe.Controls.Item(i).Height += hgtdifEnd IfNextSorry , i am new to VB.net(windows application)
can you plz send me code , incase if i want to do for all the controls in form?
when i tried with this code, label control are not re-positioned
Sorry to interject, here. Reed is giving you the information you asked for. However, it can start getting messy and complicated when the number of controls on the form increases.
It's possible that the label is set to autosize: in which case, setting the width/height will do nothing. You'll probably have to do sme experimentation with each control type you have to make it size properly.
The 'repositioning of controls' is one of the reasons the TableLayout was designed. Can you explain a bit about why it's not working? Remember, it's a container control. You will need to add rows and columns as appropriate for your controls that you want, and place those controls in the TableLayout Control, setting docking and autosizing as appropriate. Again, it needs some experimentation to get the feel of it, but it's a marvelous tool.
Your welcome, but seriously, listen to what the others are saying...
SJ is dead-right. I'll give you the code to adjust all the controls on your form, but be warned - with too many controls it could take too long to execute and get very ugly. You should really look at anchoring and the various container controls available. Also keep in mind that just because you want your form to look or act a certain way, that doesn't mean that windows (or at least the framework) was designed to let you do it.
Also be sure to check the Autosize property of your labels as SJ mentioned - if it is set to true than they can't be resized this way.
Ok, so you're right - you'll need a loop. But you'll also have to use recursion to get all the controls within controls. You'll also want to set a minimum size that stops the controls from shrinking beyond their original size. The whole thing gets pretty messy (as you've been warned)...
Dim oldSize As Size = Me.Size
Private Sub Form2_ResizeBegin(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.ResizeBegin
Me.oldSize = Me.Size
End Sub
Private Sub Form2_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged
Me.ResizeControls(Me.Controls)
End Sub
Private Sub ResizeControls(ByVal controls As System.Windows.Forms.Control.ControlCollection)
Dim widdif As Single = Me.Size.Width - Me.oldSize.Width
Dim hgtdif As Single = Me.Size.Height - Me.oldSize.Height
For Each c As System.Windows.Forms.Control In controls
Me.ResizeControls(c.Controls)
If (c.Width + widdif > 0) And (c.Height + hgtdif > 0) Then
c.Width += widdif
c.Height += hgtdif
End If
Next
Me.oldSize = Me.Size
End Sub
Hope this helps ya understand...