Problems using Me.Scale in Framework 2.0

J

Jerry Spence1

A very useful feature was added in 2.0 of .NET framework which was the
scaling of a form and all the controls within it. This is really useful but
I am finding very little information of how to use it.

I have managed to implement it as follows:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim addSize As New SizeF(0.5F, 0.5F)
Me.Scale(addSize)

End Sub

And this quite rightly reduces everything down to half of what it was.
However I am wanting to put this in the Form Resize event, and I tried this
rather clumsy approach:

Private Sub Form2_Resize(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Resize

Static OldWidth As Integer
Static OldHeight As Integer
If OldWidth = 0 Then OldWidth = Me.Width
If OldHeight = 0 Then OldHeight = Me.Height

Dim ScaleX As Decimal = Me.Width / OldWidth
Dim ScaleY As Decimal = Me.Height / OldHeightDim MyScale As New
SizeF(ScaleX, ScaleY)

Me.Scale(MyScale)

OldWidth = Me.Width
OldHeight = Me.Height

End Sub

The problem is that it suffers from re-entry. As soon as the form and
everything else is scaled, the Resize event is triggered again and so on.

How are we supposed to use this scale facility?

-Jerry
 
S

Samuel Shulman

I think that you can add a static Boolean variable that will be used a flag
to ignore the event

Static b As Boolean

b = not b

if b then
Scaling code goes here
end if


That will mean that the event will be implemented once though fired twice
(second time ignore)


hth,
Samuel Shulman
 
J

Jerry Spence1

Thanks for that tip Samuel. Yes, that part did work OK, although the rest of
the code doesn't perform too well. The result is very jerky and only some of
my controls are actuall scaling. I think it doesn't help that I am
physically scaling the form with the mouse, and then it is also being scaled
through my code - one is fighting the other. I am surprised that help
doesn't help and there are no examples on the net anywhere.

-Jerry
 
G

gene kelley

A very useful feature was added in 2.0 of .NET framework which was the
scaling of a form and all the controls within it. This is really useful but
I am finding very little information of how to use it.

I have managed to implement it as follows:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim addSize As New SizeF(0.5F, 0.5F)
Me.Scale(addSize)

End Sub

And this quite rightly reduces everything down to half of what it was.
However I am wanting to put this in the Form Resize event, and I tried this
rather clumsy approach:

Private Sub Form2_Resize(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Resize

Static OldWidth As Integer
Static OldHeight As Integer
If OldWidth = 0 Then OldWidth = Me.Width
If OldHeight = 0 Then OldHeight = Me.Height

Dim ScaleX As Decimal = Me.Width / OldWidth
Dim ScaleY As Decimal = Me.Height / OldHeightDim MyScale As New
SizeF(ScaleX, ScaleY)

Me.Scale(MyScale)

OldWidth = Me.Width
OldHeight = Me.Height

End Sub

The problem is that it suffers from re-entry. As soon as the form and
everything else is scaled, the Resize event is triggered again and so on.

How are we supposed to use this scale facility?

-Jerry

When resizing the form, you don't want to scale the form, itself -
just the form's controls. The Try/Catch is needed as during starup,
the static variables are not initialized until the first pass through
of the resize event:
Private Sub Form1_Resize(ByVal sender As Object, ByVal _
e As System.EventArgs) Handles Me.Resize
Static OldWidth As Integer
Static OldHeight As Integer
Try
Dim ScaleX As Decimal = CDec(Me.Width / OldWidth)
Dim ScaleY As Decimal = CDec(Me.Height / OldHeight)
Dim MyScale As New SizeF(ScaleX, ScaleY)

For Each ctl As Control In Me.Controls
ctl.Scale(MyScale)
Next
Catch
Finally
OldWidth = Me.Width
OldHeight = Me.Height
End Try

You also can selectively scale only specfic controls in the For Each
loop.


Gene
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top