Get the Message

We've got code to send the message now we need to receive them on the server side. This is also done using the MessageQueue object If (Not MessageQueue.Exists(QUEUEPATH)) Then MessageQueue.Create(QUEUEPATH) End If oMSMQ New MessageQueue(QUEUEPATH) oMSMQ.Formatter New BinaryMessageFormatter() AddHandler oMSMQ.ReceiveCompleted, New _ ReceiveCompletedEventHandler(AddressOf _ ReceiveCompleted) oMSMQ.BeginReceive() The first part of this code checks to see if a queue exists and creates it if not....

Declarative Security

The type of permission checking in the sample program is called imperative security it is performed at runtime. A second type of code access security can be employed called declarative security. This type of security is defined by using security attributes (metadata) at the assembly level. This type of security offers different types of benefits. For one, a program could be written that scans an assembly and lists exactly what types of permissions that assembly is defined as having. This might...

Dynamic Object Creation Using RTTI

The dynamic object code can be found in folder prjRTTI. Earlier, you learned about creating user interface controls such as buttons and menu items at runtime. The code for creating a button at runtime looks something like the following b New Button() b.Size new Size(48, 16) b.Text A New Button b.Location new Point(10,10) Me.Controls.Add(b) Now I'd like to take dynamic object creation a few steps further. The preceding code works fine as long as you know at design time that you want to create a...

Placing Your Menu

The second event that needs to be created is the one that actually draws the menu into the proper area. It is called the DrawItem event Public Sub mNew_DrawItem(ByVal sender As Object, _ ByVal e As System.Windows.Forms.DrawItemEventArgs) _ Handles mNew.DrawItem, mOpen.DrawItem, mSave.DrawItem, _ mExit.DrawItem Dim mi As MenuItem CType(sender, MenuItem) Dim iImage As Integer Dim textColor As Color SystemColors.MenuText Dim textBounds As New _ RectangleF(e.Bounds.Left + 20, _ e.Bounds.Top + 2,...

Do You Know What the Matrix Is

Take note that the GraphicsPath object just discussed is defined in a small coordinate space the entire object fits into a rectangle from (0,0) to (40,40) in the screen. My eventual goal is to take this little object and move it all across a painted area. Furthermore, it would be cool if I could rotate it around like it was spinning. Both of these operations can be accomplished though the use of the Matrix class. A matrix is a set of numbers arranged in rows and columns. Matrices are most...

Braving the Basics of GDI

The GDI+ code can be found in the folder prjBasicGDIPlus. I've always been drawn to the subject of computer graphics the very thought of creating animated pixels on a computer screen was one of the primary things that drew me into the computer field. I remember writing a program on my very first PC an Apple 2c (I'm dating myself now, aren't I Oh well). The program recreated the cover for Pink Floyd's album The Wall (more self-dating I said album, as opposed to CD). The program plotted the basic...

Debugging RTTI

I'd like to give one final word about coding using runtime type information. Although it can be extremely powerful, it also adds a layer of complexity that can make debugging an application much more difficult. This is because many errors that are normally design-time errors become runtime errors in the RTTI world. Consider the following simple line of code This line of code declares a variable of type SomeButtonOutThere. This type must be locatable by Visual Studio in your current defined list...

Sending Output to the Debug Window

The Debug object should be familiar to VB6 veterans, but it has undergone some enhancements in VB.NET. Instead of a single Print method to write output to the immediate window (now called the Output window), you have a choice of several methods Writes the specified output to the output window, followed by a linefeed If passed-in condition is true, writes the specified output to the output window, followed by a linefeed Writes the specified output to the output window If passed-in condition is...

Performing a Ping

The ping code can be found in folder prjPinger. Note The basis for the prjPinger project was borrowed from a source code example found at the VBNET web site, www.mvps.org vbnet. This site (which was named long before VB.NET was introduced, so the name is really a coincidence) has dozens of great VB examples, and new VB.NET examples are coming every day. With permission from the author and web master, Randy Birch, I took a VB6 ping example and hacked it up into a VB.NET equivalent. I highly...

Using RTTI in the Real World

The previous example might seem a bit strange why use RTTI when you can use a much more standard method to accomplish the same thing However, if you give it some thought, you might be able to conceive of some interesting uses for RTTI. Consider a database of information that contains not only the data, but also the VB class information used to edit that data. For example, you might have a series of custom data types that represent your business objects (customers, orders, and so on). Given some...

Querying the Performance Counter

With the addition of instances, you finally have all the information needed to actually query a performance counter value. The following code, executed when a button is clicked, retrieves the currently selected performance counter value in the application. Protected Sub cbCheck_Click(ByVal sender As Object, ByVal e As System.EventArgs) Dim cInstance As String tnNode tvCounters.SelectedNode cCntName tnNode.text cCatName tnNode.Parent.text If lblnstance.Selectedlndex > -1 Then cInstance...

Seeing the Inner Workings of Your Code

The StackFrame code can be found in folder prjStackFrames. Writing code that displays information about your code now that's pretty cool. The .NET Framework allows you to access information about the procedure-calling stack using the StackTrace and StackFrame classes. The following procedure gives an example Private Sub DisplayStackFrameReport() Dim oST As New StackTrace(O) Dim oSF As StackFrame Dim oMeth As MethodBase Dim oParm As ParameterInfo Dim i As Integer Call lbOut.Items.Clear() For i 0...

Version Example

To demonstrate .NET Framework versioning, I decided to create a component that acted like the ProgressBar component that comes as a built-in .NET class. No, I didn't fail to realize that such a component already existed, but I wanted to see if I could create a slightly different visual look to my progress bar. Here's what I came up with < ToolboxItem(True)> _ Public Class myProgressBar Inherits Panel Me.BorderStyle BorderStyle.Fixed3D End Sub properties for Minimum, Maximum, and Value...

Using the Pinger Class

The VB.NET Pinger class does the same thing as ping. Given a domain name, the code will ping-test that domain and return the results of that test. This type of test might be useful in a program that relies on a web service as part of its functionally. You could easily build in a ping test to the machine upon which the web service is running to support your application. If the ping fails, then the web service probably cannot be contacted either, and the portion of your application that relies on...

Handling Control Arrays Another

The control array code can be found in the folder prjNoControlArrays. From my very first days of Visual Basic, I was enamored with using control arrays. My first real Visual Basic program was a card game, and it seemed a perfect solution to create an array of picture box controls with the appropriate bitmaps for playing cards. I completed my card game, uploaded it to a local BBS (this was a few years before the Internet), and received a few comments about it. My use of control arrays didn't...

Manual XML

If your goal is to manually produce an XML file on disk, then the XMLText- Writer class is what you'll probably use. This class contains methods for creating nodes structures of any complexity level. Shown next is a code fragment and the resultant XML from that code Dim oW As New XmlTextWriter(cXMLFilename, Nothing) Try oW.Formatting System.Xml.Formatting.Indented oW.WriteStartDocument(False) oW.WriteComment(This file represents another & _ fragment of a book store inventory database)...

Filling the Types Treeview

Imagine it's time now to add your first class to the Treeview. Say it's the ToolboxBitmapAttribute class (this is an actual class in the System .Drawing namespace). To add this class to the proper place in Treeview, you'll first have to locate the ancestor type for ToolboxBitmapAttribute class, which happens to be the Attribute class. What if this class doesn't yet exist in the Treeview (in this case, it won't I already stated that the ToolboxBitmapAttribute class is the first class you've...

Yet Another Type Safe Solution

There is a third way you can implement type-safe collections. This method is built right into the .NET Framework. It involves creating your class by making it a descendant of the class CollectionBase, as shown here < Serializable()> Class AnotherBookEncapsulaterStack Inherits System.Collections.CollectionBase Sub Push(ByVal oEmp As BookEncapsulater) Dim iCtr As Integer MyBase.InnerList.Count Dim oBook As BookEncapsulater oBook CType(MyBase.InnerList.Item(iCtr - 1), _ BookEncapsulater)...

Monitoring Your Applications Performance

The Performance Monitor code can be found in the folder prjPerformanceCounters. The Windows NT 2000 platform has some wonderful performance monitoring tools built right into the operating system. Most people think of the Performance Monitor as the program used to query and log system performance results such as processor utilization, hard disk performance, thread utilization, and so on. With the introduction of the .NET Framework, however, programmers have access to the same operating system...

System Trays in Their Full Upright Positions

The system tray code can be found in the folder prjSlashdotReader. Note The SlashDot reader program was originally conceived by John O'Donnell, and was the source of an article at http www.c-sharpcorner.com. This article was the first in a two-part set which explained how to retrieve and parse the XML data from SlashDot, but it had not yet placed the article titles into a system tray icon. Given proper permission, I borrowed this excellent idea, completed the program, and used it in this book....

Bitwise Operators

Visual Basic has never had operators for performing bitwise functions until now, that is. The following table shows the three bitwise operators available in VB.NET. Both left and right side of the operator Either left or right side of operator is 1 Either left or right side of operator is 1, but not both As a refresher, the following table shows the four possible combinations of left and right sides of bitwise operators and the result of each

Legal Stored Procedures

The stored procedure code can be found in folder prjStoredProcedures. Most database programmers learn the power and usefulness of the stored procedure as they become familiar with client-server programming. Stored procedures are chunks of code that are written in Transact-SQL that run on the database server machine, as opposed to the client machine. When your database server is a 20,000, multiprocessor box, and your client is a 1,200 box, which one should you choose to perform most of the...

Screen Capture

Many screensavers appear to be drawing their effects right over the top of your word processor, email program, or whatever applications you happened to have open when the screensaver kicks in. In truth, this is a cleverly disguised trick. Most screensavers perform this trick by grabbing an image of the screen, copying it onto a bitmap, and then drawing on that bitmap. In the Win32 environment, there were several API-based methods for grabbing the Desktop window handle for the purpose of...

Watching for Files

I decided to use a form-level property for this project to track the directory that I wanted the FileSystemWatcher to watch. Properties are useful because you can place code inside the Set procedure, which causes the code to execute whenever the property changes. Here is the code for that form level property Property pFolderName As String Get pFolderName llFolderName.text End Get oWatcher New tagFileSystemWatcher oWatcher.Path Value oWatcher.IncludeSubdirectories False 'oWatcher.Target...

Still More Binding

The binding code can be found in the folder prjBindingToACollection. You've seen how binding works on the database-like DataSet. This construct makes it easy to load information out of a database and into a DataSet residing as some form of middle tier object perhaps and use the DataSet to bind to presentation tier controls. Binding is not limited to DataSets, however. You can bind your own classes and custom collections to UI controls. This is useful because not all applications are database...

Speed Reading Using the Data Reader

The DataReader code can be found in the folder prjDataReader. The workload of the database programmer can be summed up in a few simple words read the data, display the data, edit the data, and write the data. The details under these broad tasks vary widely from project to project, of course, depending on the type of application 1-tier, 2-tier, n-tier , the structure of the data itself, the desired user interface, and many other factors. However, the read edit write cycle of the typical database...