Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / March 2008

Tip: Looking for answers? Try searching our database.

When RaiseEvent called, not firing

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Andrew - 08 Mar 2008 16:58 GMT
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?
Thorsten Doerfler - 08 Mar 2008 17:22 GMT
Andrew schrieb:
> 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
[quoted text clipped - 61 lines]
> 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
Signature

Microsoft MVP Visual Basic

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

Andrew - 08 Mar 2008 17:49 GMT
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?

> Andrew schrieb:
> > On my vb.net 2.0 app I am loading an image to a picture box and auto
[quoted text clipped - 93 lines]
>
> Thorsten Doerfler
Herfried K. Wagner [MVP] - 09 Mar 2008 00:26 GMT
"Andrew" <Andrew@discussions.microsoft.com> schrieb:
>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.

Signature

M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>

Stephany Young - 08 Mar 2008 17:31 GMT
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(...)

> 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
[quoted text clipped - 83 lines]
> Does anybody have any idea why it’s not fired when I call the RaiseEvent
> from the class?
Andrew - 08 Mar 2008 17:51 GMT
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?

> Your event handler is subscribing to the ReportImageScale event on object
> 'obj' but you are actually operating on object 'img'.
[quoted text clipped - 97 lines]
> > Does anybody have any idea why it’s not fired when I call the RaiseEvent
> > from the class?
Steve Gerrard - 08 Mar 2008 19:03 GMT
> 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.
Andrew - 08 Mar 2008 21:32 GMT
> > Dim img = New ClassWithEvent()
> >
[quoted text clipped - 12 lines]
> 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..
Steve Gerrard - 08 Mar 2008 21:47 GMT
> 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.
Andrew - 08 Mar 2008 23:05 GMT
that last one worked.. so any were in the app I have this ( Public WithEvents
img As ClassWithEvent
) I should do.. (img = New ClassWithEvent())?

> > This is on my form:
> >
[quoted text clipped - 13 lines]
>
> Don't declare it (Public, Dim, Private, etc) more than once.
Steve Gerrard - 09 Mar 2008 00:01 GMT
> 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...
Andrew - 09 Mar 2008 02:18 GMT
thanks...

> > that last one worked.. so any were in the app I have this ( Public
> > WithEvents img As ClassWithEvent
[quoted text clipped - 29 lines]
>
> ' and add an event handler for img in Form2 as well...

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.