• (5 Pages)
  • +
  • « First
  • 2
  • 3
  • 4
  • 5

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

#46 Guest_Anklebuster*


Reputation:

Posted 23 September 2010 - 09:02 PM

View Postlsioi, on 07 July 2010 - 05:19 AM, said:

View Postravmonster, on 14 December 2009 - 04:40 AM, said:

Error division by ZERO!!!
@iHeight


Why?????


Hi ravmonster,

You get this error if you call ResizeControls directly.
You need to call ResizeForms. That's where iheight is defined.

In other news:
It seems that GetLocation should be called by ResizeForms, is this an omission? I stuck it in there and it seems to work...
...however, resizing the form won't let me drag edges too close together.

Cheers,

Mitch
Was This Post Helpful? 0

#47 bkandler  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-October 10

Posted 11 October 2010 - 08:53 AM

I stumbled across your tutorial yesterday (10/10/10) while researching a problem I'm having with form/dialog resizing.

I have a set of procedures, much like what you describe, that do a good job of resizing just about everything in a VB6 dialog. There's just a couple of small glitches.

1. In a dialog having a picturebox into which a picture is stretchblt-ed and some pixels are drawn. During a resize, the picture disappears. I can get it back by reselecting and loading the picture. During form load, the picture fails to display. Again, reselecting and loading the picture gets it back. It's as if picture loading is not allowed during a Load or Resize event. Redisplay is always a part of the resize event.

2. the other issue is with a dialog that has runtime created controls (scrollbars). If I don't use my resizing, all is well. If I use resizing, as soon as I change the size by the tiniest amount, the display goes haywire. The dynamically created scrollbars disappear, much of the gray background goes white, and several controls no longer display. Again, redisplay is a part of the resize event.

A couple of comments on your tutorial:

1. The dialog needs to save its size in the Resize event so that the same size can be used on form load in future executions.

2. It's a good thing you used .Left, .Top, .Width, and .Height in your resize as .Move does not work for ALL controls. Specifically, Comboboxes are not supported.

3. Your code does not support all control types. I found that lines don't have the same size variables, but use X1,Y1 and X2,Y2.
Was This Post Helpful? 0
  • +
  • -

#48 bkandler  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-October 10

Posted 14 October 2010 - 09:04 AM

[quote name='bkandler' date='11 October 2010 - 07:53 AM' timestamp='1286812391' post='1137751']

Problem #2 solved. My saving of the dialog specifics was taking place before the dynamically created scrollbars were set up. An On Error Resume Next was hiding the problem.

Problem #1 evaded, though not elegantly. I created a short timer to allow auto-clicking the background select combobox after the Resize event. The background now survives resize and shows up on the initial form load. :online2long:
Was This Post Helpful? 0
  • +
  • -

#49 gaius  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-November 10

Posted 09 November 2010 - 01:54 AM

thnks for the source. it will realy help me a lot. but a have 1 more problem. i dont know how im gonna put it in my system that im creating. can somebody please tell me how. :)

by d way im new here. novice in programming. :)

im just making my own system for my practise. cause im not that good enough. :)

thanks for the source. it will really help me a lot. but a have 1 more problem. i don't know how I'm gonna put it in my system that I'm creating. can somebody please tell me how.

by d way I'm new here. novice in programming.

I'm just making my own system for my practice. cause I'm not that good enough. :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#50 Guest_Mark*


Reputation:

Posted 08 December 2010 - 10:03 PM

Hi,

I'm trying to get the screen size using
xx.height = screen.height
xx.width = screen.width

When I try and run the code I get the following error when it reaches this line
Runtime error '424': Object required

What am I missing here, why doesn't it recognise 'screen' as a valid object?

Cheers
Was This Post Helpful? 0

#51 Guest_Montclair*


Reputation:

Posted 18 February 2011 - 10:59 AM

There are various issues with this demo code, and one large inefficiency. To work around the bugs, simply add this to all the procedures:
On Error Resume Next


That will bypass the TabIndex error, if you leave the code as is. Nonetheless, none of that tabindex code is required at all. There is a much simpler solution by just referencing each control by its name -- but the original contributor has a bug which must be fixed first as follows.

Update the GetLocation procedure from this:
With List(i)
        .Name = curr_obj
...


To this:
With List(i)
        .Name = curr_obj.Name
...


With that corrected, you can completely erase and replace ResizeControls with this much simpler code that references each control's properties by the name of the control (stored in the List() array). That will remove all TabIndex errors as we don't need that property for anything functionality of these routines:
Public Sub ResizeControls(frm As Form)
    On Error Resume Next
    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 control objects in the array
    'based on the upper bound of the # of controls
    For i = 0 To UBound(List)
        frm(List(i).Name).Left = List(i).Left * y_size
        frm(List(i).Name).width = List(i).width * y_size
        frm(List(i).Name).height = List(i).height * x_size
        frm(List(i).Name).Top = List(i).Top * x_size
    Next
    Err.Clear: On Error GoTo 0
End Sub


Finally, clean the rest of the code by removing the Index member from the Control type as follows:
Private Type Control
    Index As Integer
    Name As String
...


Change to:
Private Type Control
    Name As String
...


And in GetLocation, changing this:
    With List(i)
        .Name = curr_obj.Name
        .Index = curr_obj.TabIndex
        .Left = curr_obj.Left
...


To this:
    With List(i)
        .Name = curr_obj.Name
        .Left = curr_obj.Left
...


There's also math issues with this code that create bigger margins around things as the form size grows, but I haven't debugged them yet, and the issue is negligible anyway.
Was This Post Helpful? 0

#52 Guest_Montclair*


Reputation:

Posted 18 February 2011 - 02:27 PM

Also, as an aside for novices and the original poster, "x_size" should refer to width, and "y_size" the height. X is across, and Y is down in computers. These values are assigned in opposite order in the ResizeControls routine, but the error is carried through the entire routine so it has no effect. It just makes the code a bit unclear.
Was This Post Helpful? 0

#53 subinms80@gmail  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 31-July 09

Posted 13 July 2011 - 12:00 AM

View PostPsychoCoder, on 06 October 2007 - 04:14 PM, said:

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 :)
Attachment attachment


This code will not work if the form contains an image or timer control.The reason is that controls like this do not have tabIndex like properties.Can you correct it
Was This Post Helpful? 0
  • +
  • -

#54 bkandler  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-October 10

Posted 13 July 2011 - 07:11 AM

View PostJabber, on 21 June 2008 - 03:05 PM, said:

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?


The problem is that not all controls support all the size properties
Was This Post Helpful? 0
  • +
  • -

#55 pmabres  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-February 12

Posted 28 February 2012 - 09:09 AM

Ok here is what i did, i managed to personalize this a little bit. what if you don't want to resize the buttons to make it bigger, just change position?.

well i attach my code and try it if you want. it worked with almost everything except tabs.

a normal call to this would be:

form_load()
SetResize Me
GetLocation me
ResizeControls Me

and if you want to setup manually some controls just do this before the resize controls
SetResize Me, RESIZEUP, TypeName(Command1) ' this will resize just up the screen all the command buttons
ComboBox1.Tag = RESIZEUP ' this will resize this specific combo box up the screen
Text1.Tag = RESIZEUPWIDTH ' this will resize up the screen and only the with of this specific text box


and you can explore more alternatives.

greetings
Was This Post Helpful? 0
  • +
  • -

#56 pmabres  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-February 12

Posted 28 February 2012 - 10:45 AM

Forgot the attachment.

Here it is

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#57 abinfra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-August 12

Posted 08 August 2012 - 11:12 AM

Hello everyone, this is my first post. I have used the tratuctor of Google to send this post, since English is not my forte, I'm Spanish.

I applied the code to resize a DataGrid Form two and a SSTab and everything perfect.

But in another Form with a DataGrid and Tab SSTab but with two, I have problems.
The problem is that in the Tab (0) which is the first thing to appear, overlapping controls Tab (1) and the controls that appear in the Tab (0) and Tab (1) do not appear in the Tab (1) .

How I can fix this problem?.

Best regards.
Was This Post Helpful? 0
  • +
  • -

#58 akogan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 21-March 13

Posted 21 March 2013 - 02:21 AM

My modification to handel some other controls that gets error by TabIndex. (Timers etc.)

The code in Form Load is just:

GetLocation Me
------------------------------------------------------------------

The code in GetLocation with my modification to handel the error:

Public Sub GetLocation(frm As Form)
Dim i As Integer
On Error Resume Next
' 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
If Err > 0 Then
i = i - 1
Err = 0
End If

i = i + 1
Next curr_obj
On Error GoTo ErrorGetLocation
' This is what the object sizes will be compared to on rescaling.
iHeight = frm.height
iWidth = frm.width
ExitGetLocation:
Exit Sub
ErrorGetLocation:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure GetLocation of Formular Form1"
Resume ExitGetLocation

End Sub
---------------------------------------------------------
in Form_Resize I use only:

Private Sub Form_Resize()
ResizeControls Me
End Sub
----------------------------------------------------------

In Resize Control I adjust directly the font size:

Public Sub ResizeControls(frm As Form)
Dim i As Integer
' Get ratio of initial form size to current form size
If iHeight = 0 Or iWidth = 0 Then Exit Sub
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
On Error GoTo GetNextControl
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
'If curr_obj.Name = "txtFieldContent" Then Stop
.FontSize = SetFontSize()
End With
End If
'Get the next control
GetNextControl:
Err = 0
Resume Next

Next curr_obj
Next i
End Sub
---------------------------------------------------------------

Great code
thanks

Was This Post Helpful? 0
  • +
  • -

#59 BasantRajpurohit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 13-September 14

Posted 13 September 2014 - 07:23 AM

View PostJabber, on 21 June 2008 - 03:05 PM, said:

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?

i have some problem .. Please anybody help me ??
Was This Post Helpful? 0
  • +
  • -

#60 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3467
  • View blog
  • Posts: 11,768
  • Joined: 12-December 12

Posted 13 September 2014 - 08:16 AM

What problem?

If your problem is specifically about this tutorial then explain the problem here. Ensure that you read the entire thread firstly though, as someone may have already have had, and resolved, the same issue.

If it is not directly related to this tutorial then start a new topic in the appropriate forum, and provide enough details for someone to help you.
Was This Post Helpful? 0
  • +
  • -

  • (5 Pages)
  • +
  • « First
  • 2
  • 3
  • 4
  • 5