|
-
Mar 11th, 2007, 06:58 PM
#1
Thread Starter
New Member
2005 why is this so slow? form resizing
I'm trying to make my form resize itself in a stretchable type way... the code is working (converted from vb6), however in VB 2005 it is really slow to move the objects. My form has about 100 objects and it is taking approx 4 seconds to move all the controls to where they need to be.
Any way to speed this up?
p.s. I've been using vb for about 5 days, so I'm pretty ignorant.
-george
Code:
Public Class Form1
Private m_ControlPositions() As ControlPositionType
Private m_FormWid As Single
Private m_FormHgt As Single
Private Sub SaveSizes()
Dim i As Integer
Dim ctl As Control
' Save the controls' positions and sizes.
ReDim m_ControlPositions(0 To Controls.Count)
i = 0
For Each ctl In Controls
With m_ControlPositions(i)
.Left = ctl.Left
.Top = ctl.Top
.Width = ctl.Width
.Height = ctl.Height
On Error Resume Next
.FontSize = ctl.Font.Size
.daFont = ctl.Font.Style
On Error GoTo 0
End With
i = i + 1
Next ctl
' Save the form's size.
m_FormWid = Me.Width
m_FormHgt = Me.Height
End Sub
' Arrange the controls for the new size.
Private Sub ResizeControls()
Dim i As Integer
Dim ctl As Control
Dim x_scale As Single
Dim y_scale As Single
Dim font_scale As Single
Dim oldfontsize As Single
' Get the form's current scale factors.
x_scale = Me.Width / m_FormWid
y_scale = Me.Height / m_FormHgt
font_scale = (y_scale + x_scale / 2)
' Reposition the controls.
i = 0
For Each ctl In Controls
With m_ControlPositions(i)
If ctl.Name <> "MenuStrip1" Then
oldfontsize = .FontSize
ctl.Left = x_scale * .Left
ctl.Top = y_scale * .Top
ctl.Width = x_scale * .Width
ctl.Height = y_scale * .Height
On Error Resume Next
ctl.Font = New Font(ctl.Font.Name, oldfontsize * font_scale)
Else
End If
On Error GoTo 0
End With
i = i + 1
Next ctl
' Save the form's size.
m_FormWid = Me.Width
m_FormHgt = Me.Height
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SaveSizes()
Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
ResizeControls()
End Sub
SaveSizes()
-
Mar 11th, 2007, 07:07 PM
#2
Re: 2005 why is this so slow? form resizing
If you want to create a complex layout that maintains various absolute and relative proportions when resized you should use one or more TableLayoutPanel objects. There is a tutorial included in the VB Express instructional video series.
http://msdn.microsoft.com/vstudio/ex...b/default.aspx
-
Mar 11th, 2007, 07:18 PM
#3
Thread Starter
New Member
Re: 2005 why is this so slow? form resizing
 Originally Posted by jmcilhinney
If you want to create a complex layout that maintains various absolute and relative proportions when resized you should use one or more TableLayoutPanel objects. There is a tutorial included in the VB Express instructional video series.
http://msdn.microsoft.com/vstudio/ex...b/default.aspx
I originally tried to go that route using various anchors/docking, but it is not capable of making everything scalable such as fonts. The form grows, but the text sizes stayed the same. I really need to make this form "stretchy." so far, the above code is the only thing that worked.
-
Mar 11th, 2007, 07:48 PM
#4
Re: 2005 why is this so slow? form resizing
The TLP will handle all the controls Location and Size. If you want to change the font sizes then just handle the SizeChanged events of the controls and set the font size relative to the size of the control. Having said that, controls can resize both horizontally and vertically without maintaining aspect ratio while fonts can't.
-
Mar 11th, 2007, 07:57 PM
#5
Re: 2005 why is this so slow? form resizing
Also, with your code you shouldnt be simulating a movement by setting all four properties (left, top, widht, height) when its better performance to use the .Move method (less screen updates).
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Mar 11th, 2007, 09:05 PM
#6
Thread Starter
New Member
Re: 2005 why is this so slow? form resizing
 Originally Posted by RobDog888
Also, with your code you shouldnt be simulating a movement by setting all four properties (left, top, widht, height) when its better performance to use the .Move method (less screen updates).
Hmm.. I thought the .move method was for VB6 only. Could you possibly give an example how to make it work in 2005?
I did change the height, width, x, y, to a setbounds method and it doubled the speed... however, I'm still seeing the controls move individually, and it looks bad. How does this .move thing work?
-
Mar 11th, 2007, 09:10 PM
#7
Re: 2005 why is this so slow? form resizing
Yes, you are correct. Its equilivalent its the .Location and .Size properties, still better then individule properties.
Bitten again by posting under VB 6 and .NET forums lol.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Mar 11th, 2007, 09:29 PM
#8
Re: 2005 why is this so slow? form resizing
This:
vb Code:
myControl.Bounds = New Rectangle(x, y, width, height)
is equivalent to this:
vb Code:
myControl.SuspendLayout()
myControl.Left = x
myControl.Top = y
myControl.Width = width
myControl.Height = height
myControl.ResumeLayout()
-
Mar 11th, 2007, 09:30 PM
#9
Thread Starter
New Member
Re: 2005 why is this so slow? form resizing
 Originally Posted by RobDog888
Yes, you are correct. Its equilivalent its the .Location and .Size properties, still better then individule properties.
Bitten again by posting under VB 6 and .NET forums lol.
Sorry if my post was confusing...
I changed the code to a .location .size format... but still didn't speed it up... i'm going to change it on the save size side as well... here is what I have changed so far....
Code:
Private Sub ResizeControls()
Dim i As Integer
Dim ctl As Control
Dim x_scale As Double
Dim y_scale As Single
Dim font_scale As Single
Dim oldfontsize As Single
' Get the form's current scale factors.
x_scale = Me.Width / m_FormWid
y_scale = Me.Height / m_FormHgt
font_scale = (y_scale + x_scale / 2)
' Reposition the controls.
i = 0
For Each ctl In Controls
With m_ControlPositions(i)
If ctl.Name <> "MenuStrip1" Then
ctl.Location = New Point(x_scale * .Left, y_scale * .Top)
ctl.Size = New Size(x_scale * .Width, y_scale * .Height)
oldfontsize = .FontSize
On Error Resume Next
ctl.Font = New Font(ctl.Font.Name, oldfontsize * font_scale)
Else
End If
On Error GoTo 0
End With
i = i + 1
Next ctl
End Sub
-
Mar 11th, 2007, 09:35 PM
#10
Re: 2005 why is this so slow? form resizing
But you are not using SuspendLayout/ResumeLayout too.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Mar 11th, 2007, 10:34 PM
#11
Thread Starter
New Member
Re: 2005 why is this so slow? form resizing
 Originally Posted by RobDog888
But you are not using SuspendLayout/ResumeLayout too.
Sorry for being so annoying...
I added a suspendlayout and resumelayout to the form and to the individual controls and vice versa. Still lagging.
Is there some kind of a SuspendPaintTillDoneDoingCalculations? So it can just write all the controls at once without having to do the math for each position?
-george
-
Mar 11th, 2007, 10:48 PM
#12
Re: 2005 why is this so slow? form resizing
What event are you calling the procedure?
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Mar 11th, 2007, 11:04 PM
#13
Thread Starter
New Member
Re: 2005 why is this so slow? form resizing
 Originally Posted by RobDog888
What event are you calling the procedure?
From the form resize event.
-
Mar 11th, 2007, 11:06 PM
#14
Re: 2005 why is this so slow? form resizing
About the only thing you can probably do is over ride the OnResize event of the form and place your code in there. Also, dependiong on how your needs are for resizing, couldnt you make do with most controls just being anchored so you will only need to size certain ones?
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Mar 11th, 2007, 11:16 PM
#15
Thread Starter
New Member
Re: 2005 why is this so slow? form resizing
 Originally Posted by RobDog888
About the only thing you can probably do is over ride the OnResize event of the form and place your code in there. Also, dependiong on how your needs are for resizing, couldnt you make do with most controls just being anchored so you will only need to size certain ones?
I wish the anchoring thing would work. I guess I'm going to have to make do with knowing that the form is going to be displayed on a big screen for hours/days and that the resize event is only going to happen if the user decides to resize it for no reason. I think this is going to get filed in the "annoying for just me" folder.
I just hate that some VB6 coder made some beautiful form that looked exactly the same no matter what constrained proportions the user set it to and there was no lag in the redraw.
-thanks for your help guys... i guess I'm done beating my head on this wall.
this is what I finally came up with... just a tad bit faster...
Code:
'to save control positions for resizing
Private m_ControlPositions() As ControlPositionType
Private m_FormWid As Single
Private m_FormHgt As Single
' Arrange the controls for the new size.
Private Sub ResizeControls()
Dim i As Integer
Dim ctl As Control
Dim x_scale As Single
Dim y_scale As Single
Dim font_scale As Single
' Get the form's current scale factors.
x_scale = Me.Width / m_FormWid
y_scale = Me.Height / m_FormHgt
font_scale = (y_scale + x_scale / 2)
' Reposition the controls.
i = 0
For Each ctl In Controls
With m_ControlPositions(i)
If ctl.Name <> "MenuStrip1" Then
Dim x As Single
Dim y As Single
Dim h As Single
Dim w As Single
Dim fs As Single
fs = .FontSize * font_scale
x = x_scale * .Left
y = y_scale * .Top
w = x_scale * .Width
h = y_scale * .Height
ctl.Bounds = (New Rectangle(x, y, w, h))
ctl.Font = New Font(ctl.Font.Name, fs)
On Error Resume Next
Else
End If
On Error GoTo 0
End With
i = i + 1
Next ctl
Last edited by georgeburns; Mar 11th, 2007 at 11:18 PM.
Reason: add code
-
Mar 11th, 2007, 11:24 PM
#16
Re: 2005 why is this so slow? form resizing
I see your still not using suspendlayout/resumelayout so what about placing a DoEvents in there to allow the CPU to update the drawing during the loop.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Mar 11th, 2007, 11:31 PM
#17
Thread Starter
New Member
Re: 2005 why is this so slow? form resizing
 Originally Posted by RobDog888
I see your still not using suspendlayout/resumelayout so what about placing a DoEvents in there to allow the CPU to update the drawing during the loop.
I put the suspendlayout/resumelayout before and after the call to the procedure. Also tried it within the procedure and for each control. All three options seemed to have little/no effect.
How would I go about putting a doevents? I haven't heard of that one yet.
-george
-
Mar 11th, 2007, 11:32 PM
#18
Re: 2005 why is this so slow? form resizing
Application.DoEvents
Basically place it in your loop anywhere.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|