• (5 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

Resize Form & Controls For Screen Size - VB6 Rate Topic: ***** 1 Votes

#1 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Post icon  Posted 06 October 2007 - 04:14 PM

*
POPULAR

Welcome to this tutorial on resizing a form based on screen size in Visual Basic 6. In this tutorial I will discuss a couple issues:
  • Resize a form based on screen size
  • Resize the controls on the form based on the form size
  • Resize the font size of all controls based on the above 2 items
I have received this question for many years, and in my time in </dream.in.code> I have also been asked this question several times, so I thought it would be a good idea to write a tutorial based on this question. THought there are many commercial products out there that will accomplish this, how many students and everyday Joe's actually have the money to be buying these solutions? Not many I can assure you, so after getting this question numerous time, I decided it was time to write my own solution to this problem, and give it back to the programming community.

The code I'm about to show you I have in a Code Module, names FormControl that I include in all VB6 projects I create. This comes in real handy because as developers we don't know what screen resolution a client, or anyone else using our software, will be using. The first thing I have in my module is some Global variables that will be used through out the module, so I, of course, make them Global and accessible to the entire module. Here are the Globals you need to add to your Code Module:

Private List() As Control
Private curr_obj As Object
Private iHeight As Integer
Private iWidth As Integer
Private x_size As Double
Private y_size As Double



As you will see, the Globals are Private, I don't want them to be accessed from outside this Code Module. The first Global is an Array or type Control. This is a private Type I have created to hold all the properties of the controls on the form. Creating your own user defined type saves a lot of headaches down the road, and allows for resizing all controls in a single loop, referencing your type:

Private Type Control
	Index As Integer
	Name As String
	Left As Integer
	Top As Integer
	width As Integer
	height As Integer
End Type



As you can see, this Type holds all the information needed about a control: Index, Name, Left, Top, Width and Height, these items will come in use later, when we write the procedure to resize all the controls at once. Now lets talk about resizing a form based on the current screen resolution we will be referencing the Screen Object available to us in Visual Basic 6.

The 2 properties we're concerned with are Width and Height. These properties of the Screen Object give us access to the screen size available to us. So, to set the Forms size in relationship to the Screen size, we will be accessing the properties of the Form Object, mainly the width and height properties. In my Module I set the forms size to the screen's size divided by 2, you may want to test and find your own resolution. Here's the simple procedure for resizing your form in relationship to the available screen resolution:

Public Sub ResizeForm(frm As Form)
	'Set the forms height
	frm.height = Screen.height / 2
	'Set the forms width
	frm.width = Screen.width / 2
	'Resize all of the controls
	'based on the forms new size
	ResizeControls frm
End Sub



Simple isn't it, we change the forms size based on the screen size, then we reference a procedure called ResizeControls, we do this because resizing the form alone isn't what we're after. Well doing only this will cause some pretty ugly user interfaces, simply because you may be resizing your form, but the controls are staying the same size, which isn't a good thing.

So, you can either write a really long and ugly procedure to resize each control individually,not very maintainable, especially if you rename or add controls, or you can write a nice neat little procedure, based on a user defined type, which we have in our Code Module, and loop through them like this:

Public Sub ResizeControls(frm As Form)
Dim i As Integer
'   Get ratio of initial form size to current form size
x_size = frm.height / iHeight
y_size = frm.width / iWidth

'Loop though all the objects on the form
'Based on the upper bound of the # of controls
For i = 0 To UBound(List)
	'Grad each control individually
	For Each curr_obj In frm
		'Check to make sure its the right control
		If curr_obj.TabIndex = List(i).Index Then
			'Then resize the control
			 With curr_obj
				.Left = List(i).Left * y_size
				.width = List(i).width * y_size
				.height = List(i).height * x_size
				.Top = List(i).Top * x_size
			 End With
		End If
	'Get the next control
	Next curr_obj
Next i
End Sub



Here we use the UBound Function To get the highest index on the form, which gives us the last control's index. We then loop through all the controls, stopping at the highest index, and change the size of the control. Before this procedure can actually work, we need to know the current location of each control, well I have a solution for that as well.

In this Module is a method called GetLocation. What this method does is it logs the current position of each control, looping through all the controls located in our List() Array, which is populated with the controls on the form. On each iteration of the loop, we use the ReDim Statement and the Preserve keyword to increment the size of the array by 1 and preserve the objects already in the array. The code for that method is as follows:

Public Sub GetLocation(frm As Form)
Dim i As Integer
'   Load the current positions of each object into a user defined type array.
'   This information will be used to rescale them in the Resize function.

'Loop through each control
For Each curr_obj In frm
'Resize the Array by 1, and preserve
'the original objects in the array
	ReDim Preserve List(i)
	With List(i)
		.Name = curr_obj
		.Index = curr_obj.TabIndex
		.Left = curr_obj.Left
		.Top = curr_obj.Top
		.width = curr_obj.width
		.height = curr_obj.height
	End With
	i = i + 1
Next curr_obj
	
'   This is what the object sizes will be compared to on rescaling.
	iHeight = frm.height
	iWidth = frm.width
End Sub



Our Code Module is almost complete, there is but one thing we need to think about resizing, and that is the font size used in the application. Resizing, say a Label, and not resizing the font being used on the label will make for an ugly User Interface as well. So what we do is, we get the value of x_size, which is one of our Globals, and set in the ResizeControls method, we multiply that by 8 and that gives the font size I'm looking for, you needs may vary. Here is the code for resizing the font size:

Public Function SetFontSize() As Integer
	'Make sure x_size is greater than 0
	If Int(x_size) > 0 Then
	'Set the font size
		SetFontSize = Int(x_size * 8)
	End If
End Function



That's all the code you need for your form and control resizing based on the users screen resolution. It's all a matter of getting the mathematics right, then resetting the size of everything on the form to fit nicely in the current screen.

Now for how to use it, in the Form_Load Event you add a call to:
  • GetLocation
  • ResizeForm
And set the FontSize property of your controls, like this:

Private Sub Form_Load()
	GetLocation Me
	CenterForm Me
	ResizeForm Me
	
	lblInstructions.Font = SetFontSize()
End Sub



This Code Module also offers another feature. If someone were the resize your form while havin the application running, if you put a call to ResizeControls in the Form_Resize Event, this will take care of resizing everthing:

Private Sub Form_Resize()
	 ResizeControls Me
	 lblInstructions.FontSize = SetFontSize()
End Sub



I am providing the Code Module this code is in, this Module and code is under the GNU General Public License, so you can use, modify or distribute as you see fit, but the license header must stay intact. I hope you have found this tutorial informative and useful. For all your Visual Basic 6 Reference Needs go to the Visual Basic 6.0 Resource Center. Thank you for reading.

Happy Coding :)
Attached File  FormControl.zip (12.7K)
Number of downloads: 32600

This post has been edited by PsychoCoder: 06 October 2007 - 04:17 PM


Is This A Good Question/Topic? 7
  • +

Replies To: Resize Form & Controls For Screen Size - VB6

#2 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Posted 06 October 2007 - 05:55 PM

Finally a solution. Thanks a lot PsychoCoder!!! :)
EDIT:
I noticed something however. Suppose you resize your form (manually by click and drag), the SetFontSize() function runs. Now when the screen size reaches a particular lower limit, the font size returned will not be a valid value. So an error is generated. Basically this happens when Font Size becomes zero.

This post has been edited by Louisda16th: 06 October 2007 - 06:06 PM

Was This Post Helpful? 0
  • +
  • -

#3 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Posted 07 October 2007 - 01:20 AM

That's a minor issue. A simple IF condition would solve the problem.
Great Tutorial Psycho!!
Was This Post Helpful? 0
  • +
  • -

#4 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Posted 07 October 2007 - 06:52 AM

Yeah. But I mentioned it just in case. This tut's solved a big problem for me.
Was This Post Helpful? 0
  • +
  • -

#5 shad0w3rz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 10-October 07

Posted 10 October 2007 - 09:24 PM

what program i need to run this file?
Was This Post Helpful? 1
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Posted 10 October 2007 - 09:33 PM

shad0w3rz this is written in VB6
Was This Post Helpful? 0
  • +
  • -

#7 shad0w3rz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 10-October 07

Posted 10 October 2007 - 09:39 PM

so that mean i need VB to run this file? sry this my first day.

i have already download the vb. it come out with only the text. how can i run it?
Was This Post Helpful? 0
  • +
  • -

#8 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Posted 11 October 2007 - 04:14 AM

This is a class module you include in a VB6 Project, when creating an application. You cannot just "run" this file.
Was This Post Helpful? 0
  • +
  • -

#9 DougieW  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-February 08

Posted 22 February 2008 - 08:32 AM

[quote name='PsychoCoder' date='6 Oct, 2007 - 04:14 PM' post='264127']
Welcome to this tutorial on resizing a form based on screen size in Visual Basic 6.

Hi,
Is there any way of adapting this for VBA - I've written an application in word 2000 also using excel.
Resizing for differen screens/resolution etc., would be nice to have. My form fills the screen ok i.e.

With Application
lAppWindowState = .WindowState
.WindowState = wdWindowStateMaximize

but the control buttons and text boxes size, position and font stay the same.
In trying to use your module, compile gives an error with the type 'As Form'. Form type isn't available. Which would you suggest
1) upgrade to Office 2003/2007
2) convert to VB 6 or VB.net
3) lie down in darkened room
4) give up and suffer :huh:

Thanks DougieW
Was This Post Helpful? 0
  • +
  • -

#10 Cowman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-December 07

Posted 16 March 2008 - 06:10 PM

VB6 must be a lot different than VB 2008 Express...
Although your code wouldn't work for me it did send
me in the right direction. Thanks Here is how I reworked
it for VB express. Hope this helps some one.

[Public Sub ResizeControls()
REM By Cowman thanks to Richard L. McCutchen's sample
REM for Visual Basic Express 2005/2008 to fit large forms to smaller screen res.
REM should be easy enough to make smaller forms fill screen as well with a little work.
REM Paste into application and put 'ResizeControls()' at begining of formload.
Dim contrls As Object
Dim ratioW, ratioH, ratio As Double
Dim screenW, screenH, fhight, fwidth, z As Integer
Dim Fsz As Single
ratio = 0 : ratioH = 0 : ratioW = 0
REM Get Current screen res.
screenW = CInt(My.Computer.Screen.WorkingArea.Width)
screenH = CInt(My.Computer.Screen.WorkingArea.Height)
REM get form size
fhight = Me.Height
fwidth = Me.Width

If fhight > screenH Then ratioH = CInt((screenH / fhight) * 100) REM to 2 decimal points
If fwidth > screenW Then ratioW = CInt((screenW / fwidth) * 100)
REM chose the smaller of either width or height ratio to ensure fit
If ratioH > ratioW Then
ratio = ratioW / 100
Else : ratio = ratioH / 100
End If
If ratio = 0 Then Exit Sub REM form is smaller than screen- -exit
Me.Width = screenW REM maxized to working area-- developer can choose to make smaller with more code here
Me.Height = screenH
Me.CenterToParent()
formitems = Me.Controls.Count - 1
contrls = Me.Controls
REM resize all the forms items
For z = 0 To formitems
Me.Controls.Item(z).Width = CInt((Me.Controls.Item(z).Width) * ratio)
Me.Controls.Item(z).Height = CInt((Me.Controls.Item(z).Height) * ratio)
Me.Controls.Item(z).Left = CInt((Me.Controls.Item(z).Left) * ratio)
Me.Controls.Item(z).Top = CInt((Me.Controls.Item(z).Top) * ratio)
Fsz = CSng(Me.Controls.Item(z).Font.Size)
Fsz = CSng((CInt((Fsz * ratio) * 10) / 10)) REM font must be single using 1 decimal point
Dim f As New System.Drawing.Font("Arial", Fsz)
' Assign the font to the control
Me.Controls.Item(z).Font = f
Next z
End Sub]
Was This Post Helpful? 0
  • +
  • -

#11 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Posted 16 March 2008 - 06:42 PM

Cowman, VB 2005, VB 2005 Express, VB 2008, VB 2008 Express are VB.Net, this is a Visual Basic 6 tutorial so it will not work as written for VB.Net

This post has been edited by PsychoCoder: 16 March 2008 - 06:43 PM

Was This Post Helpful? 0
  • +
  • -

#12 iParker  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-April 08

Posted 17 April 2008 - 09:02 AM

PsychoCoder,

Few problems here, where do the variables: iHeight, iWidth get assigned to an integer?

Further, there is a call Form_Load for 'CenterForm', however, this function is nowhere to be found in your example.

Seems like you didn't post the completed example.

Thanks, iParker
Was This Post Helpful? 0
  • +
  • -

#13 iParker  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-April 08

Posted 17 April 2008 - 11:36 AM

View PostiParker, on 17 Apr, 2008 - 09:02 AM, said:

PsychoCoder,

Few problems here, where do the variables: iHeight, iWidth get assigned to an integer?

Further, there is a call Form_Load for 'CenterForm', however, this function is nowhere to be found in your example.

Seems like you didn't post the completed example.

Thanks, iParker


Okay, I see in GetLocation(Frm As Form) that iHeight and iWidth are assigned integers that are passed from the form the call in Form_Load().


However, the compiler is complaining about .Name = curr_obj, Name seems to have no value. The compiler says: .Name=" "

Any help would be appreciated.
Was This Post Helpful? 0
  • +
  • -

#14 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Posted 17 April 2008 - 11:45 AM

How are you implementing this? I just ran the project I created for testing this and have no problems at all. Also, you are using VB6 right? (Sorry, Ive had people trying to use this code in VB.Net, which is VB 2005, VB 2008, etc)
Was This Post Helpful? 0
  • +
  • -

#15 Jabber  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 21-June 08

Posted 21 June 2008 - 03:05 PM

Google'n help for my game - resize form this looks just like what I need.
So I signed up in hope of help.
I got vb6 and loaded this module and added code to form_load()

I get a err.num 438
"Run-time error '438' Object doesn't support this property or method"
It don't like tab.index

I tried a few changes but nothing helped.

Any clues to why I am getting this error?
Was This Post Helpful? 0
  • +
  • -

  • (5 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »