When RaiseEvent called, not firing

A

Andrew

On my vb.net 2.0 app I am loading an image to a picture box and auto
resizing it to show as a thumbnail. As it find the correct size it calls
RaisesEvent with the size percent. In the app I use that to show back to the
person that is using the form.

Here is my class:

Public Class ClassWithEvent

Public Event ReportImageScale(ByVal ScaledSize As Integer)

Public Function GetThumbNailSize(ByVal strImagePath As String, ByVal
iScaleSize As Integer, _
Optional ByVal ScaleWidth As Integer = 0, Optional ByVal ScaleHeight As
Integer = 0) As Size

If System.IO.File.Exists(strImagePath) Then

Dim iW As Integer = 0
Dim iH As Integer = 0

Try
Dim iThumb As Image = Image.FromFile(strImagePath)


If ScaleWidth = 0 And ScaleHeight = 0 Then
Dim X As Double = (iScaleSize * 0.01)
iW = CInt(iThumb.Width * X)
iH = CInt(iThumb.Height * X)
Else
Dim bTrue As Boolean = False

Do Until bTrue = True
Dim X As Double = (iScaleSize * 0.01)
iW = CInt(iThumb.Width * X)
iH = CInt(iThumb.Height * X)

If iW <= ScaleWidth And iH <= ScaleHeight Then
bTrue = True
RaiseEvent ReportImageScale(iScaleSize)
End If
iScaleSize -= 1

System.Windows.Forms.Application.DoEvents()
Loop

End If

GetThumbNailSize.Width = iW
GetThumbNailSize.Height = iH

Return GetThumbNailSize

Catch ex As Exception
Call mdlMain.LogError(ex.Message)
End Try
End If

End Function

End Class

Here is when I call ReportImageScale:

Dim img = New ClassWithEvent()

If Not Me.pb1.Image Is Nothing Then Me.pb1.Image.Dispose()
Me.pb1.Size = img.GetThumbNailSize(pstrLogoPath, 100,
Me.pbDark.Width - 15, Me.pbDark.Height - 15)
Me.pb1.Image = img.GetThumbNail(pstrLogoPath, 100,
Me.pbDark.Width - 15, Me.pbDark.Height - 15)
Me.pb1.Left = (Me.pbDark.Left + (Me.pbDark.Width / 2)) -
(Me.pb1.Width / 2)
Me.pb1.Top = (Me.pbDark.Top + (Me.pbDark.Height / 2)) -
(Me.pb1.Height / 2)
Me.pb1.Visible = True


Here is when the event gets fired in the app:

Public WithEvents obj As ClassWithEvent
Protected Sub obj_ReportImageScale(ByVal ScaledSize As Integer) Handles
obj.ReportImageScale
Me.nud1.Value = ScaledSize
End Sub

Does anybody have any idea why it’s not fired when I call the RaiseEvent
from the class?
 
T

Thorsten Doerfler

Andrew said:
On my vb.net 2.0 app I am loading an image to a picture box and auto
resizing it to show as a thumbnail. As it find the correct size it calls
RaisesEvent with the size percent. In the app I use that to show back to the
person that is using the form.

Here is my class:

Public Class ClassWithEvent

Public Event ReportImageScale(ByVal ScaledSize As Integer)

Public Function GetThumbNailSize(ByVal strImagePath As String, ByVal
iScaleSize As Integer, _
Optional ByVal ScaleWidth As Integer = 0, Optional ByVal ScaleHeight As
Integer = 0) As Size

If System.IO.File.Exists(strImagePath) Then

Dim iW As Integer = 0
Dim iH As Integer = 0

Try
Dim iThumb As Image = Image.FromFile(strImagePath)


If ScaleWidth = 0 And ScaleHeight = 0 Then
Dim X As Double = (iScaleSize * 0.01)
iW = CInt(iThumb.Width * X)
iH = CInt(iThumb.Height * X)
Else
Dim bTrue As Boolean = False

Do Until bTrue = True
Dim X As Double = (iScaleSize * 0.01)
iW = CInt(iThumb.Width * X)
iH = CInt(iThumb.Height * X)

If iW <= ScaleWidth And iH <= ScaleHeight Then
bTrue = True
RaiseEvent ReportImageScale(iScaleSize)
End If
iScaleSize -= 1

System.Windows.Forms.Application.DoEvents()
Loop

End If

GetThumbNailSize.Width = iW
GetThumbNailSize.Height = iH

Return GetThumbNailSize

Catch ex As Exception
Call mdlMain.LogError(ex.Message)
End Try
End If

End Function

End Class

[...]

Does anybody have any idea why it’s not fired when I call the RaiseEvent
from the class?

The event is fired for sure. 'obj' didn't know, what your instance
'img' is doing. The event is only fired for the instance which is
doing the job. Change your code as follows:

Public WithEvents img As New ClassWithEvent()

' [...]

If Not Me.pb1.Image Is Nothing Then Me.pb1.Image.Dispose()
Me.pb1.Size = img.GetThumbNailSize(pstrLogoPath, 100,
Me.pbDark.Width - 15, Me.pbDark.Height - 15)
Me.pb1.Image = img.GetThumbNail(pstrLogoPath, 100,
Me.pbDark.Width - 15, Me.pbDark.Height - 15)
Me.pb1.Left = (Me.pbDark.Left + (Me.pbDark.Width / 2)) -
(Me.pb1.Width / 2)
Me.pb1.Top = (Me.pbDark.Top + (Me.pbDark.Height / 2)) -
(Me.pb1.Height / 2)
Me.pb1.Visible = True

Protected Sub img_ReportImageScale( _
ByVal ScaledSize As Integer _
) Handles img.ReportImageScale

Me.nud1.Value = ScaledSize
End Sub

btw. You should avoid using Application.DoEvents(). Use threads or the
BackgroundWorker instead.

Thorsten Doerfler
 
S

Stephany Young

Your event handler is subscribing to the ReportImageScale event on object
'obj' but you are actually operating on object 'img'.

Your line:

Dim img = New ClassWithEvent()

should be changed to:

obj = New ClassWithEvent()

and your line call to img.GetThumbNail(...) should be changed to
obj.GetThumbNail(...)
 
A

Andrew

I pug the Public WithEvents img As New ClassWithEvent() and visual studio
2005 have me change it back to
Dim img As New ClassWithEvent()

And still not working?





Thorsten Doerfler said:
Andrew said:
On my vb.net 2.0 app I am loading an image to a picture box and auto
resizing it to show as a thumbnail. As it find the correct size it calls
RaisesEvent with the size percent. In the app I use that to show back to the
person that is using the form.

Here is my class:

Public Class ClassWithEvent

Public Event ReportImageScale(ByVal ScaledSize As Integer)

Public Function GetThumbNailSize(ByVal strImagePath As String, ByVal
iScaleSize As Integer, _
Optional ByVal ScaleWidth As Integer = 0, Optional ByVal ScaleHeight As
Integer = 0) As Size

If System.IO.File.Exists(strImagePath) Then

Dim iW As Integer = 0
Dim iH As Integer = 0

Try
Dim iThumb As Image = Image.FromFile(strImagePath)


If ScaleWidth = 0 And ScaleHeight = 0 Then
Dim X As Double = (iScaleSize * 0.01)
iW = CInt(iThumb.Width * X)
iH = CInt(iThumb.Height * X)
Else
Dim bTrue As Boolean = False

Do Until bTrue = True
Dim X As Double = (iScaleSize * 0.01)
iW = CInt(iThumb.Width * X)
iH = CInt(iThumb.Height * X)

If iW <= ScaleWidth And iH <= ScaleHeight Then
bTrue = True
RaiseEvent ReportImageScale(iScaleSize)
End If
iScaleSize -= 1

System.Windows.Forms.Application.DoEvents()
Loop

End If

GetThumbNailSize.Width = iW
GetThumbNailSize.Height = iH

Return GetThumbNailSize

Catch ex As Exception
Call mdlMain.LogError(ex.Message)
End Try
End If

End Function

End Class

[...]

Does anybody have any idea why it’s not fired when I call the RaiseEvent
from the class?

The event is fired for sure. 'obj' didn't know, what your instance
'img' is doing. The event is only fired for the instance which is
doing the job. Change your code as follows:

Public WithEvents img As New ClassWithEvent()

' [...]

If Not Me.pb1.Image Is Nothing Then Me.pb1.Image.Dispose()
Me.pb1.Size = img.GetThumbNailSize(pstrLogoPath, 100,
Me.pbDark.Width - 15, Me.pbDark.Height - 15)
Me.pb1.Image = img.GetThumbNail(pstrLogoPath, 100,
Me.pbDark.Width - 15, Me.pbDark.Height - 15)
Me.pb1.Left = (Me.pbDark.Left + (Me.pbDark.Width / 2)) -
(Me.pb1.Width / 2)
Me.pb1.Top = (Me.pbDark.Top + (Me.pbDark.Height / 2)) -
(Me.pb1.Height / 2)
Me.pb1.Visible = True

Protected Sub img_ReportImageScale( _
ByVal ScaledSize As Integer _
) Handles img.ReportImageScale

Me.nud1.Value = ScaledSize
End Sub

btw. You should avoid using Application.DoEvents(). Use threads or the
BackgroundWorker instead.

Thorsten Doerfler
--
Microsoft MVP Visual Basic

vb-hellfire visual basic faq | vb-hellfire - einfach anders
http://vb-faq.de/ | http://www.vb-hellfire.de/
 
A

Andrew

I changeed everthing back to img Dim img = New ClassWithEvent(),

Public WithEvents img As ClassWithEvent
Protected Sub img_ReportImageScale(ByVal ScaledSize As Integer) Handles
img.ReportImageScale
Me.nud1.Value = ScaledSize
End Sub


and still not working, any other ideas?
 
S

Steve Gerrard

Andrew said:
Dim img = New ClassWithEvent()

Public WithEvents obj As ClassWithEvent

Let's try again.

You can't have both declarations, and expect it to work.

You should have one declaration, say
Public WithEvents obj As ClassWithEvent

Then, when you need to use it, do
obj = New ClassWithEvent ' ( no Dim on this line)

You may need to place the declaration at module level, so it is available in the
procedure where you are using it.
 
A

Andrew

Steve Gerrard said:
Let's try again.

You can't have both declarations, and expect it to work.

You should have one declaration, say
Public WithEvents obj As ClassWithEvent

Then, when you need to use it, do
obj = New ClassWithEvent ' ( no Dim on this line)

You may need to place the declaration at module level, so it is available in the
procedure where you are using it.


I have put both this in the module or on the form it's self (Public
WithEvents obj As ClassWithEvent)

and took this (obj = New ClassWithEvent) out and still get an error saying
that I need to "Use the New keyword to create an object instance"..

and if I put it in the module this line gets an error:

Protected Sub img_ReportImageScale(ByVal ScaledSize As Integer) Handles
img.ReportImageScale
Me.nud1.Value = ScaledSize
End Sub


------------------------------------------------------------------------------ here is my corrected code:

Here is my class:

Public Class ClassWithEvent

Public Event ReportImageScale(ByVal ScaledSize As Integer)

Public Function GetThumbNailSize(ByVal strImagePath As String, ByVal
iScaleSize As Integer, _
Optional ByVal ScaleWidth As Integer = 0, Optional ByVal ScaleHeight As
Integer = 0) As Size

If System.IO.File.Exists(strImagePath) Then

Dim iW As Integer = 0
Dim iH As Integer = 0

Try
Dim iThumb As Image = Image.FromFile(strImagePath)


If ScaleWidth = 0 And ScaleHeight = 0 Then
Dim X As Double = (iScaleSize * 0.01)
iW = CInt(iThumb.Width * X)
iH = CInt(iThumb.Height * X)
Else
Dim bTrue As Boolean = False

Do Until bTrue = True
Dim X As Double = (iScaleSize * 0.01)
iW = CInt(iThumb.Width * X)
iH = CInt(iThumb.Height * X)

If iW <= ScaleWidth And iH <= ScaleHeight Then
bTrue = True
RaiseEvent ReportImageScale(iScaleSize)
End If
iScaleSize -= 1

System.Windows.Forms.Application.DoEvents()
Loop

End If

GetThumbNailSize.Width = iW
GetThumbNailSize.Height = iH

Return GetThumbNailSize

Catch ex As Exception
Call mdlMain.LogError(ex.Message)
End Try
End If

End Function

End Class

This is on my form:


Here is when I call ReportImageScale:
Class Form1

Here is when the event gets fired in the app:

Public WithEvents img As ClassWithEvent
Protected Sub img_ReportImageScale(ByVal ScaledSize As Integer) Handles
img.ReportImageScale
Me.nud1.Value = ScaledSize
End Sub


Dim img = New ClassWithEvent()

If Not Me.pb1.Image Is Nothing Then Me.pb1.Image.Dispose()
Me.pb1.Size = img.GetThumbNailSize(pstrLogoPath, 100,
Me.pbDark.Width - 15, Me.pbDark.Height - 15)
Me.pb1.Image = img.GetThumbNail(pstrLogoPath, 100,
Me.pbDark.Width - 15, Me.pbDark.Height - 15)
Me.pb1.Left = (Me.pbDark.Left + (Me.pbDark.Width / 2)) -
(Me.pb1.Width / 2)
Me.pb1.Top = (Me.pbDark.Top + (Me.pbDark.Height / 2)) -
(Me.pb1.Height / 2)
Me.pb1.Visible = True

End class



I have done simple version os this and it works,, I don't see why it isn't
working..
 
S

Steve Gerrard

Andrew said:
This is on my form:



Public WithEvents img As ClassWithEvent

That is declaration #1
Dim img = New ClassWithEvent()

That is declaration #2.

Change it to read
img = New ClassWithEvent()

You can call it obj or img, no one cares which. Just don't name it one thing in
one place, and another in another.

Don't declare it (Public, Dim, Private, etc) more than once.
 
A

Andrew

that last one worked.. so any were in the app I have this ( Public WithEvents
img As ClassWithEvent
) I should do.. (img = New ClassWithEvent())?
 
S

Steve Gerrard

Andrew said:
that last one worked.. so any were in the app I have this ( Public
WithEvents img As ClassWithEvent
) I should do.. (img = New ClassWithEvent())?

The "anywhere in the app" part of that is a little troubling.

The "normal" scenario would be something like this:

' In Form1 general declarations
Dim WithEvents img As ClassWithEvent

'In Form1 Load event:
img = New ClassWithEvent

' now img is set in Form1. Leave it alone.
' anywhere in Form1 you need to use it, just start using it, i.e.

Me.pb1.Size = img.GetThumbNailSize(pstrLogoPath, 100, _
Me.pbDark.Width - 15, Me.pbDark.Height - 15)

' if you have Form2, it won't have an img object in it.
' So, if you need one in there as well, then do

' In Form2 general declarations
Dim WithEvents img As ClassWithEvent

'In Form2 Load event:
img = New ClassWithEvent

' now img is set in Form2. Leave it alone.
' anywhere in Form2 you need to use it, just start using it

' and add an event handler for img in Form2 as well...
 
H

Herfried K. Wagner [MVP]

Andrew said:
I pug the Public WithEvents img As New ClassWithEvent() and visual studio
2005 have me change it back to
Dim img As New ClassWithEvent()

Declare the variable on the type level (class level, module level), not as a
local variable.
 
A

Andrew

thanks...

Steve Gerrard said:
The "anywhere in the app" part of that is a little troubling.

The "normal" scenario would be something like this:

' In Form1 general declarations
Dim WithEvents img As ClassWithEvent

'In Form1 Load event:
img = New ClassWithEvent

' now img is set in Form1. Leave it alone.
' anywhere in Form1 you need to use it, just start using it, i.e.

Me.pb1.Size = img.GetThumbNailSize(pstrLogoPath, 100, _
Me.pbDark.Width - 15, Me.pbDark.Height - 15)

' if you have Form2, it won't have an img object in it.
' So, if you need one in there as well, then do

' In Form2 general declarations
Dim WithEvents img As ClassWithEvent

'In Form2 Load event:
img = New ClassWithEvent

' now img is set in Form2. Leave it alone.
' anywhere in Form2 you need to use it, just start using it

' and add an event handler for img in Form2 as well...
 

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