|
The newsletter is compiled by DevCity.NET NewsMasters Ged Mead and Mike McIntyre
Spring is here already and the May Day holiday has been and gone. It'll soon be Christmas! Well, maybe not, but we think we have a few useful gifts in this issue of the DevCity Newsletter. A range of articles aimed at a range of developer skills, so whether you are an absolute Newbie or an Old Hand at .NET we have an item or two for you.
Our regular contributors, Mike McIntyre, Ged Mead and George Poth are joined by VBCity Leaders Mark Dryden and Andrew Sutton this week and between them they offer advice, insight and knowledge on a range of topics across the .NET spectrum. We hope you will find something, hopefully many things, of interest and use to you.
Ged Mead, Newsletter Editor
AdvertisementTable Of Content:
Advertisement
by Mark Dryden
Part 1 - Adding the Flash ActiveX Component to your Windows Form Application
Flash - ah - saviour of the universe ...
As Brian May penned those immortal lyrics there is a good chance he probably wasn't thinking about utilising the Macromedia Flash ActiveX component is his .NET Windows form based application. But had he realised that he could and that he was developing using .NET then I'm sure that song would have taken in a slightly different direction.
So what is the Macromedia Flash ActiveX component? Well, I'm sure that most developers have either seen a Flash movie, played a Flash based game or seen some funky animation on the web that simply isn't possibly using traditional scripting techniques. Flash is basically an animation and web development tool that uses vector-based graphics - a Flash developer can create intricate and complex animations for a pittance of the file size, thus making it ideal for the web. In addition, Flash has grown up and can support XML in Flash 6 and ADO in Flash 7.
However, there is no reason at all why the Flash Component cannot be utilised within a .NET form and play Flash Movie Files (these always have '.swf' extension). In fact, it more than feasible to allow the Flash Component to communicate backwards and forwards from each other as required.
OK - to start with you must add the Flash Component to your form. Now there is a high probability you've already got the Flash Player installed on your machine (usually part of the IE install) - if you haven't then you will need download the latest Flash player from the Macromedia Website (www.macromedia.com). To add the component, click onto the 'Tools' menu on the top menu bar and select 'Customise Toolbox', a new window will appear - ensure that the tab 'COM Components' is selected and from the listview goto the entry 'Shockwave Flash Object' and tick the checkbox and then OK. Go to your toolbox and you'll see a new entry titled 'ShockwaveFlash'.
To add the component to your form simply select the item and draw the control onto your form and hey presto, one Flash Player on your form.
So how do you load a movie into the Player, well, simply set the 'Movie' property to a valid Flash Movie File, e.g.
Me.axShockwaveFlash1.Movie = Application.StartupPath & "\MyLogoMovie.swf"
And that's pretty much it. You can interact with the Flash Player as the object exposes methods such as Stop, Play and GotoFrame; for a more detailed overview - check out the Object Browser and view the Interop.ShockwaveFlashObjects.IShockwaveFlash object for a full listing.
In the next part, we'll look into the more advanced communication techniques that can be employed between your .NET Application and the Flash Player/Movie.
I love the way that you can keep chipping away at a problem in VB.NET and so often you can find alternative ways to solve it and end up with exactly the result you had in mind.
Just recently, I wanted to create a small form that would pop up in response to a button click to give the user somewhere to type in temporary notes. A Do It Yourself one-page Notepad, if you will.
Starting from basics, using Form.Show certainly got the jotter form up and running. But there was a small problem with this approach. As soon as focus returned to the main form (maybe because the user clicked on a main form's control) then the popup form would be partially or even totally hidden behind the main form. In fact in cases where the main form was maximized, the popup form was completely lost' behind the main form as soon as the jotter form lost focus. OK, so the user could redisplay it from the icon in the Windows status bar at the bottom of the screen, but I wasn't too impressed with that as a professional solution, so assumed that my users wouldn't be either.
Using ShowDialog wasn't a realistic option because I wanted the user to have access to the popup form or the main form, completely at will and able to flip backto wards and forwards between the two.
So I wanted the smaller popup to stay in front of the main form, but not be displayed modally. Maybe setting the TopMost property of the jotter form to True would do the trick, then? Well, yes, it would meet the requirement to keep it on top and still allow the user access to enter data on either form. But the problem with that solution was that the jotter form then stayed on top of all other applications as well if the user switched between them, and this wasn't necessarily what they would want.
Step forward the Owned Form. A standard Windows Form, which you can call with:
Dim JP as JotterForm
Me.AddOwnedForm(JP)
JP.Show
always assuming that you have named the owned form 'JotterForm' of course!
Now this is just absolutely the perfect solution for my needs. I have the second form available and visible in all situations while the user is using my VB.NET application. Maximizing the main form doesn't obliterate the jotter form. Alt-Tabbing between the VB.NET app and other running apps didn't leave the jotter form on view. Minimizing the main form takes the jotter form down to minimized size with it. Restoring from Minimize sets everything back up the way it should be.
Oh Happy Day! Some days you could just hug your monitor, couldn't you?
By the way, if you are using something like this, but want to restrict your user to just a single instance of the second form, there's a really neat way you can use the OwnedForms Property of your main form to do this.
If Me.OwnedForms.Length = 0 Then
Dim SP As New ScribblePad
Me.AddOwnedForm(SP)
SP.Show()
End If
It's not a massive mental leap to figure out, of course, that you can change that first line with its length value to another setting and can control how many instances of the second form you're prepared to let your users have.
If Me.OwnedForms.Length < 2 Then
Dim SP As New ScribblePad
Me.AddOwnedForm(SP)
SP.Show()
End If
See what I mean about having all those lovely alternatives to choose from? Don't you just love VB.NET?
On a good day, anyway.
- Junior (Ged Mead aka XTab, xtab@vbcity.com)
by Mike McIntyre
Getting Started With the Performance Counters
Part 1 of 2
This article introduces the concept of Windows OS performance counter objects and provides details about .NET Framework performance counters.
What are Performance Counters?
Performance Counters are Windows OS objects that capture metrics about the performance of hardware and applications. For example, performance counters can capture performance metrics for processors, memory, threads, events, and processes. Metrics can be used to detect problems or to 'tune' applications and hardware for maximum performance.
Performance Counters are named and grouped into performance counter categories such as Processor, System, Thread, ASP.NET, .NET CLR Memory, and .NET LocksAndThreads. Within each category are individual performance counters. For example, within the .NET CLR Memory performance counter category are performance counters such as # Bytes in all heaps, Gen 0 heap size, # GC handles, and % time in GC.
Performance Counter objects may be accessed programmatically or with the Windows Performance Monitor Application (PerfMon).
If you are not familiar with performance counters or the Microsoft Performance Monitor application, spend an hour with the Microsoft Performance Monitor to get familiar with the basics before you continue with this article. The Performance Monitor (PerfMon) is a performance object viewer that comes with Windows NT, Windows 2000, and Windows XP. PerfMon allows you to select performance counters from performance counter categories and display them in a variety of formats including graph, histogram, or report format. To start PerfMon: From the Windows Start menu choose Run, type in PerfMon, and click the OK button to start PerfMon. Click here. for a good article on getting started with PerfMon.
.NET Framework Performance Counters
There are performance counters you can use to collect metrics about performance within the Common Language Runtime (CLR) and ASP.NET.
There are nine performance monitor categories for the CLR and two for ASP.NET as shown in the table below.
| Common Language Runtime (CLR) Performance Counter Categories (9) |
| Category | Description |
| Exceptions | Exceptions metrics |
| Interop | Metrics about interaction with COM components, COM+ services, and type libraries. |
| JIT | Metrics about JIT operations. |
| Loading | Metrics about assembly, class, and AppDomain loading. |
| Locks and Threads | Metrics about managed locks and threads. |
| Memory | Metrics about memory including the GC, and handles. |
| Networking | Metrics about networking. |
| Remoting | Metrics about remoting |
| Security | Metrics about security checks the CLR performs |
|
|
| ASP.NET Performance Counter Categories (2) |
| Category | Description |
| ASP.NET | Metrics about restarts and requests queued. |
| ASP.NET | Metrics about worker thread restarts, errors, requests per second, and instance counts. |
|
Click here to learn about the .NET Framework performance counters available in each category shown above.
Code Example One
The code example below introduces the use of performance counter objects in VB.NET code. This example uses a performance counter 'Processes' from the performance counter category 'System'. The 'Processes' performance counter collects the number of processes running in the computer at the time of data collection.
' Get number of processes running in a computer.
' 1. Declare a variable of type PerformanceCounter named
' processessPerformanceCounter.
' 2. Call the PerformanceCounter New constructor passing it
' the "System" performance counter cateory and the "Processes"
' performance counter.
' 3. Assign the performance counter object returned from the New constructor
' to the processessPerformanceCounter variable.
Dim processesPerformanceCounter As New PerformanceCounter("System", "Processes")
' Call the performance counter's RawValue method to collect the number of
' processes (programs) running in the computer.
Console.WriteLine("Processes = " & processesPerformanceCounter.RawValue.ToString)
Result: Processes = 47
Next issue - how to use performance counters to monitor the performance of the .NET CLR memory used by an individual VB.NET application.
Learn more about performance counters by clicking here and here
By Andrew Sutton
Web Matrix is a complete environment for developing ASP.NET applications. It offers a viable, cheap alternative to Visual Studio .NET. It is available as a free download at ASP.NET. At the time of writing the current version is 0.6.
Basically, Web Matrix is a WYSIWYG editor which also supports facilities like a drag and drop tool bar for HTML Elements, Web Controls, Custom Controls and Code Snippets. VB.NET, C# and J# languages are all supported. Web Matrix was developed by Microsoft using the .NET framework, C#, and Windows Forms.
Features
- Has it own built in lightweight web server for use in developing and testing your ASP.NET applications. No access to IIS is required.
- Integrated support for SQL Server, MSDE & JET databases.
- Drag and drop support for files between local development and live remote systems via FTP. When you are happy with your locally modified files just drag and drop them to a remote server (providing the remote server supports FTP)
Pros
- Cost, it's free
- Smaller footprint than Visual Studio. The installation size is only 1.3 Meg. The IDE is fast and responsive.
- Dedicated forum for supporting Web Matrix.
- Excellent on-line Guided Tour.
- Integrated support for ASP & Web Matrix help
- There is a free on-line book available from Wrox.
Cons
- Does not support Intellisense.
- Cannot create user or server controls.
- Does not support integrated version control. Makes team development unrealistic.
- Does not support integrated debugging.
- Cannot create DLLs so you will have to get familiar with the command line options for vbc to create DLLs from code files in Web Matrix.
Conclusion
As a development environment for a single developer I would recommend Web Matrix as a viable alternative to any other development environment.
by George Poth
Don't worry, my German is far too bad to write an article, lucky you! I just wanted to wake you up because the 'Tag' property of our beloved and behated toolbar is as important as the G-Spot of your partner. The better you know it, the bigger the thrill - with code of course. What did you think, huh? Aaaah, were you thinking in the wrong direction? I will come back to that in a minute, but let's first talk about our toolbar before our thoughts get sexy again.
The first time I used a toolbar, I coded it as suggested in the article I was reading, like so:
Protected Sub tbrDemo_ButtonClick(ByVal obj As Object, ByVal cea As _
ToolBarButtonClickEventArgs)
Dim btn As Object = cea.Button
If btn Is tbrOne Then
'Do something
ElseIf button Is tbrTwo Then
'Do something else
ElseIf button Is tbrThree Then
'Do something else
ElseIf button Is tbrFour Then
'Do something else
End If
End Sub
This works fine but is lots of 'ifs' and 'thens' and 'elseifs' and whatnot. Then someone said, 'Why don't you use the index to code your toolbar buttons?' With this, you can forget about the 'ifs', 'thens', and 'elseifs' - the 'Select Case' structure makes it possible. And so I did:
Protected Sub tbrDemo_ButtonClick(ByVal obj As Object, ByVal cea As _
ToolBarButtonClickEventArgs)
Select Case tbrDemo.Buttons.IndexOf(cea.Button)
Case 0
'Do something
Case 1
'Do something else
Case 2
'Do something else
Case 3
'Do something else
End Select
End Sub
The problem here is the index. If you change the sequence of your buttons, you have to change the code accordingly. Changing one button won't be much of a problem, but I had thirteen buttons with images. After I changed the button sequence, the images were messed up, the buttons were messed up, and I couldn't get it right anymore. The more I tried, the more I got confused.
I was about to play baseball with my monitor when something really simple, efficient, and useful came to my mind: the tag property. Just click the toolbar once, and then Collections on the right from 'Buttons' in the properties window, and you'll see the tag property. Type something there for each of your buttons that you can easily remember. In the example below you can see 'One', 'Two', and so on which must be the same words from the tag property.
Protected Sub tbrDemo_ButtonClick(ByVal obj As Object, ByVal cea As _
ToolBarButtonClickEventArgs)
Select Case cea.Button.Tag
Case "One"
'Do something
Case "Two"
'Do something else
Case "Three"
'Do something else
Case "Four"
End Select
So simple is that. No more messing with the button and image index and consequently no more headaches - and no new monitor.
Okay, guys, let's get sexy again. Once I was went on vacation in England. A person said 'Keep the pecker up' and I immediately thought in the wrong direction, too. Then someone told me that it simply means to 'Cheer up'. So, you guys in the UK: Keep the pecker up. And you guys in the US: Don't mess with this right now.
by Mike McIntyre
This article introduces the Queue class and provides example code that demonstrates how to use a Queue object. The Queue class is located within the .NET Framework's System.Collections namespace.
A Queue object is a first-in, first-out collection of objects. Queues are useful for storing things in the order they were received for sequential processing. Objects stored in a Queue collection are inserted at one end and removed from the other.
The following code snippets demonstrate how to use a Queue object to keep track of each player's turn in a game application.
Example One - Declare and instantiate a Queue object.
' Declare a variable of type Queue named _PlayerQueue.
' Use a Queue constructor to create a new Queue object.
' Assign the address (reference) of the new object
' to the _PlayerQueue variable.
Private _PlayerQueue As New Queue()
Example Two - A method named FillPlayerQueue is used to fill the _PlayerQueue object with player names
Private Sub FillPlayerQueue()
' The Queue Enqueue method is used to add objects to the front of a Queue.
' Add four players to _PlayerQueue.
_PlayerQueue.Enqueue("Mike")
_PlayerQueue.Enqueue("Mary")
_PlayerQueue.Enqueue("Bill")
_PlayerQueue.Enqueue("Tony")
' By default a Queue is given capacity to hold 32 objects.
' The Queue TrimToSize method sets the capacity to the
' actual number of elements in the Queue.
' Trim the size of _PlayerQue to four players.
_PlayerQueue.TrimToSize()
End Sub
Example Three - A function named GetNextPlayer pulls a player from the front of the _PlayerQueue and then adds same player back to the _PlayerQueue - at the end of the Queue - so the player's turn will come up again after the other players have had their turn.
Private Function GetNextPlayer() As String
' Declare a variable of type String named player.
Dim player As String
' The Queue Dequeue method removes and returns the object
' at the beginning of the Queue.
' Get the next player from the _PlayerQueue.
player = _PlayerQueue.Dequeue.ToString
' Trim _PlayerQueue to size.
_PlayerQueue.TrimToSize()
' The Queue Enqueue adds an object to the end of the Queue.
' Add the player back to the Queue, at the end of the Queue
' so the player's turn will come up again after the other
' players have their turns.
_PlayerQueue.Enqueue(player)
' Return the player.
Return player
End Function
Example Four - A function named ReportPlayerQueueStatus uses Queue methods to report the next player, current player order, and the number of players.
Private Function ReportPlayerQueueStatus() As String
' Declare a variable of type String named report.
Dim report As String
' Build the report.
report += "Player Queue Status Report" & vbCrLf
report += "----------------------------" & vbCrLf
' The Queue Peek method returns the object at the front of the Queue
' without removing it from the Queue.
report += "Next player will be " & _PlayerQueue.Peek.ToString & "." & vbCrLf
' The Queue Count method returns the number of objects in a Queue.
report += "Number of players is " & _PlayerQueue.Count.ToString _
& vbCrLf & vbCrLf
' Add Title for player list.
report += "Players" & vbCrLf
report += "----------------------------"
' The Queue CopyTo method is used to copy the objects
' in a Queue to an array.
' Copy _PlayerQueue objects to an array.
Dim players(_PlayerQueue.Count) As String
_PlayerQueue.CopyTo(players, 0)
' List the players in the Queue.
Dim player As String
For Each player In players
report += player & vbCrLf
Next
Return report
End Function
Learn more about the Queue class by clicking Here.
Advertisement
Missed an earlier article? You can now view back copies of the DevCity Newsletter on the DevCity pages of the site.
Click Here.
Advertisement
|