Tuesday, January 06, 2009
Home
.Net
Return of the Repeater
Complete image control & app
Part 2 - Starting the control
Part 3 - Loading images
Part 4 - Custom exceptions
Part 5 - Fax/Multipage tifs
Part 6 - Custom events
Part 7 - Selecting fax pages
Part 8 - Rotating displays
Part 9 - The power of GDI+
Part 10 - Zooming
Part 11 - .Net's fatal exception
Part 12 - Fix the squishies
Part 13 - Zoom to fit
Part 14 - You beat the pros
Part 15 - Cropping
Part 16 - Bonus: StickyMouse
Part 17 - Finishing touches?
Part 18 - Make the application
Part 19 - Adding viewports
Part 20 - A better toolbar
Part 21 - Connect the toolbar
Part 22 - Adding ImageEditors
Part 23 - Toolbar ZoomCombo
Part 24 - VB3 style ease
Part 25 - Saving images to files
Part 26 - Integer-only textBox
Part 27 - Passing save settings
Part 28 - The last exception
Part 29 - Menus offer more
Part 30 - Book, app & source
Couple of CS Snippets
XML processing quickies
File extension extensions
McAfee.Not
cs IntBox
Floating Holidays
Code snippets
Flexible sorts
Converted deserializations
Autodeploy not found
Autodeploy just stops
VB must be killed
Media file attributes
Fastest file searches
Webservicing custom objects
Aspect correct resize
Funky thumbnails
Wide interval timing
VB2005 or bust?
Recurring events
Single instances
Proper proper casing
Simple address formating
Combo filling reminder
Easy gradient forms
Grrrr on interops?
Winform memory usage
Windows service your ISP
Pretty up VS code printing
Remote configs with no BS
GDI+ printing cd cases
Your own flat combo
Where's the splitter?
Full autoemail
ReessppoonncceeWwrriittee
Kill the back cache
Color to hex
Source to web
Recode without recompile
Prop snippet for VS2008
Database tricks

Your own image control

Originally published December 2002 on DoItIn.net using VB7.0/2002. Updated for VB7.1 February 2005
Links for compiled demo versions, all required resources and source code are included at the end of this article.

Plus, get the complete eBook in Adobe Acrobat 7 format ... all here.


6) Custom events

Switch mental gears for a second and put yourself in the role of dev-user. When you load a file that contains multiple images you're probably going to want to show the count to your end users right away. You can do this with the current code by remembering to check the TotalPageCount property but it would be nicer if there was an event you could trap, so we'll add one called "ImageLoaded" that has the count as one of its arguments.

Custom events have come a long way from the VBClassic days, now we have lots of options including tooling everything out with Delegates, wiring and unwiring events dynamically at runtime and even just forgetting we're in .Net and doing it "The VB Way". We're going to use a hybrid style to get the advantages of both VB and .Net.

In VBClassic we added events to objects by just declaring "Public Event MyEvent" in a class's declarations section and called it anywhere in code with a RaiseEvent. If the event had to pass extra information you put that in the signature and filled in the blank at the raise. You can still do this. It is perfectly fine and other VB consumers will have no problem with it (and if you're doing Interop out to a COM client it's great to have this option). However, VB isn't the only guy using the VBRuntime these days so it's a better idea to follow the .Net standard of having your event signature start with a "Sender" object and follow with an instance of a class derived from EventArgs.

The Sender will most always be a reference to the object that did the raise (in our case, the svImageEditor instance) but the EventArgs argument is a class so it'll take a bit more code.

Right click on the svImageEdit project node of the Solution Explorer, click on "Add" then on "Class" or "Module", it doesn't matter which you pick, we're just needing an empty file shell with a *.vb extension. Name the file "svEventArguments.vb" and hit ok. When the code designer opens, select all of the default text and delete it. Now paste the following code into the empty codeview:

Public Class svImageLoadEventArgs
    Inherits EventArgs

    Private m_TotalPages As Integer

    Public Sub New(ByVal totalPages As Integer)
        MyBase.New()
        m_TotalPages = totalPages
    End Sub

    Public ReadOnly Property TotalPages() As Integer
        Get
            Return m_TotalPages
        End Get

    End Property
End Class

The base EventArgs class has a number of overloaded constructors and we aren't implementing any of them. Instead, we're using a single constructor that forces us to pass in the page count value and then exposing that value to the client via a read-only property. A developer once attacked this style saying that 'all derived classes *MUST* expose a parameterless default constructor if the base class has one' and if you also were trained this way then I have to tell you that I've never found a clear technical reason for such a rule; "Implementing"? then yes, you have to expose the whole interface, but "Inheriting" doesn't follow the same set of rules.

Go to codeview of the svImageEdit UC and create a new sub-region in the Declarations region, under (not in, under) the "Private Variables" sub-region. Name the new sub-region "Events".

Click in the Events subregion and add your nearly-traditional VB event signature:

#Region "Events"
    Public Event ImageLoaded(ByVal sender As Object, ByVal e As svImageLoadEventArgs)
    
#End Region

Now scroll down to the LoadImage function and add the raise. Note that we'll use the helpful in-line style to create a new instance of the svImageLoadEventArgs class right as we raise the event and in that single line of code we'll be passing in the constructor's required page count value with the ping of the UC's TotalPageCount property. Saves a lot of typing, doesn't it?

Public Function LoadImage(ByVal img As Image) As Boolean
       Try
           m_OriginalImage = Image.FromFile(fileName)
           ResetImage()

RaiseEvent ImageLoaded(Me, New svImageLoadEventArgs(TotalPageCount))

       Catch ex As Exception
           'todo:  test to figure errors
           '       and remove catch-all
           Throw ex

       End Try
  
    End Function

Rebuild the solution so the harness gets the updated file, Save because now would be the perfect time for a crash, then open the testHarness form in codeview. Drop down the IDE object combo and select svImageEditor1, then drop down the IDE event combo and scroll down to the new ImageLoaded event. Click on it to add a stub and in the stub put a little messagebox call to check your work:

Private Sub SvImageEditor1_ImageLoaded(ByVal sender As Object, _
       ByVal e As svImageLoadEventArgs) _
       Handles SvImageEditor1.ImageLoaded

       MsgBox("Image file loaded." & vbcrlf & _
        "The file as " & e.TotalPages & " page(s)")

    End Sub

If you haven't saved the solution recently, do it now.

Next: Selecting fax pages

Robert Smith
Kirkland, WA

 

added to smithvoice march 2005


Print  

pagecomment
  Add Comment



Submit Comment
  View Ratings
50.00%0
40.00%0
30.00%0
20.00%0
10.00%0

Number of Comments 0 , Average of Ratings
  View Comments
No comment.


Privacy Statement  |  Terms Of Use
Copyright 2008 by Robert C. Smith