Exploring the GrabAPicture Application (Part 2)

In the previous post, Exploring the GrabAPicture Application (Part 1), we discussed the basic parameters for the application—that it provides a means of changing the desktop wallpaper automatically, use online as well as local resources, and sports a command line interface (amongst other features). This post starts building the application.

You’ll start with the basic Windows Forms Application template. Rename the initial form as frmMain (this application will contain a number of forms) by right clicking the form’s entry in Solution Explorer and choosing Rename File from the context menu. This first form is the one that you’ll always see when you start the application from the command line. After you rename the initial form, add two more forms, frmAddEdit (used to add new entries and edit existing entries) and frmConfigure (used to display and manage the list of local and remote sources), by right clicking the project entry in Solution Explorer and choosing Add | New Item from the context menu. You saw the working version of these forms in the previous post.


I’m eliminating a few configuration features from the example for the sake of simplicity. For example, you should include tooltips with your version of the application to make it accessible. When the example is completely finished, you’ll be able to download the source code and see some of these additions for yourself.

Let’s begin with the design of frmMain.
Here’s how frmMain will appear.


This form lets the user type in the location of a piece of wallpaper and use it directly by typing something into Wallpaper Location, selecting a style in Style Selection, and clicking Set Value. The user can also click Random Sources to see a list of available wallpaper sources, which is actually the preferred method of interacting with the application because this technique lets the user save entries for future use. Close will close the form. The user can also use this form to choose whether to use local sources, remote sources, or both. The following table describes the control configuration for this form (add the controls to the form in the order shown to reduce the amount of configuration you must perform).

Control Property Value
Button1 (Name) btnOK
  DialogResult OK
  Location 208,8
  Modifiers Friend
  Size 104,23
  TabIndex 0
  Text Set &Value
Button2 (Name) btnConfigure
  Location 208,40
  Modifiers Friend
  Size 104,23
  TabIndex 1
  Text &Random Sources
Button3 (Name) btnCancel
  DialogResult Cancel
  Location 208,72
  Modifiers Friend
  Size 104,23
  TabIndex 2
  Text &Close
Label1 Location 8,8
  Modifiers Friend
  Size 144,23
  TabIndex 3
  Text &Wallpaper Location:
TextBox1 (Name) txtWallpaperURI
  Location 8,32
  Modifiers Friend
  Size 176,20
  TabIndex 4
GroupBox1 Location 8,64
  Modifiers Friend
  Size 192,100
  TabIndex 5
  Text Style Selection
RadioButton1 (Name) rbStretched
  Location 16,24
  Modifiers Friend
  Size 104,24
  TabIndex 6
  Text &Stretched
RadioButton2 (Name) rbCentered
  Location 16,48
  Modifiers Friend
  Size 104,24
  TabIndex 7
  Text &Centered
RadioButton3 (Name) rbTiled
  Location 16,72
  Modifiers Friend
  Size 104,24
  TabIndex 8
  Text &Centered
CheckBox1 (Name) cbLocal
  Location 8,176
  Modifiers Friend
  Size 184,24
  TabIndex 9
  Text Use Random &Local Sources
CheckBox2 (Name) cbRemote
  Location 8,208
  Modifiers Friend
  Size 184,24
  TabIndex 10
  Text &Use Random Remote Sources

Next time we’ll look at two other forms. The first helps the user manage local and remote sources. The second will provide the means for adding or editing individual entries. Let me know if you have any questions at John@JohnMuellerBooks.com. You can see the next post in this series at Exploring the GrabAPicture Application (Part 3).


Exploring the GrabAPicture Application (Part 1)

Sometimes it’s nice to explore a new kind of application. GrabAPicture is a Visual Basic.NET application I built for myself and have continually improved over a number of years. This application lets me record my favorite picture locations, whether they exist online, on a network, or on my location machine, and display them as wallpaper. Yes, I could have used Windows 7 functionality to create a rotating services of random pictures, but I wanted to create my own application. It does have some advantages over the Windows 7 option:


  • The command line interface makes it possible to add the application to the Startup folder, access the application from a shortcut, or execute the application from a command prompt.
  • The application has the ability to use both network and online resources.
  • I can individually adjust how I want each picture displayed. If one picture looks better tiled and another stretched, I have that option.

Of course, it’s always nice to play around and see what you can do. This example builds on techniques found in .NET Framework Solutions: In Search of the Lost Win32 API and Professional Windows 7 Development Guide. Some of the techniques, such as saving the settings to an XML file in the user’s C:\Users\UserName\AppData\Roaming\GrabAPicture folder are ported from C# applications I created in the past. I’m also trying out a few entirely new techniques in this application. In short, it’s a combination of techniques both old and new.

To achieve my goal, I’m using some exotic techniques, such as PInvoke to change the wallpaper. I’m also creating a serializable class to hold the application data and make it easy to work with. The example uses several forms that are specially configured to ensure GrabAPicture works as expected. As previously mentioned, it includes a fully functional command line interface that lets you adjust everything from the command line if desired. Of course, you can also work with the GUI.

You’ll find a new installment for this series every Friday. I have no idea right now of just how many installments the application will require—that depends on how many reader queries I receive and just how far I have to break the application down in order to make it easy to understand. When I’ve completed this series, you’ll be able to create your own version of GrabAPicture and hopefully improve on it. In fact, I’ll provide some ideas as we go along of things I’d like to add.

Let’s get started with a quick overview of the user interface. When you start GrabAPicture, you’ll see the current application settings as shown here.


If desired, you can type the location of a wallpaper file into the Wallpaper Location field, click Set Value, and have it change the wallpaper on your desktop. However, the more common way to use this application is with a random source, which you access by clicking Random Sources. This dialog box contains local and remote sources as shown here.


I chose to separate local sources (including those on the local network) from remote sources (those on the Internet) so that the application could continue to do its job using just local or just remote sources if desired. Each source has four buttons associated with it. You can add new sources, edit existing sources, delete a source you no longer want, and select a particular source for use immediately.

Selecting or deleting a source doesn’t require much additional work. However, the other two activities require that you obtain additional information from the user. Adding or editing a source displays a customized version of the following dialog box (the customization occurs in code—the application doesn’t actually use multiple forms).


You give each source a name and a location. The entry also includes a style selection that tells Windows how to display it. The example is currently compatible with older versions of Windows, so it includes the Stretched, Centered, and Tiled settings. In a later blog post, I’ll show how to update these settings to something that’s Vista or Windows 7 specific so it will include the Fill and Fit options.

Most command line utilities rely on a text-based help screen. I chose to include a GUI help screen for GrabAPicturejust to be different and to demonstrate how you’d perform this task (since there aren’t any other examples that I can find). When you type GrabAPicture /? and press Enter at the command line, you see the following help screen.


Well, that’s about it from a design perspective. Let me know if you have any questions at John@JohnMuellerBooks.com. You can see the next post in this series at Exploring the GrabAPicture Application (Part 2).


Resources for Rod Stephens’ Visual Basic Programming 24-Hour Trainer

I had previously reviewed Rod Stephens’ Visual Basic Programming 24-Hour Trainer. As part of that review, I had mentioned that Rod provides videos as one of the training methods. These videos add an element not found in most books—a real human talking with you about a particular technique.

For at least some people, the video is probably the most important addition to the learning environment because they actually learn better by watching someone else perform the task. Computer books now have the equivalent of cooking or woodworking shows on television. The popularity of these shows tells just how much people like them and you’ll like Rod’s videos quite a bit.

Fortunately, you don’t have to take my word for it, which is the purpose of this post. Rod has graciously made a number of these videos available online. As long as you can access YouTube, you can see the videos. Here’s the links:


Give these links a try to determine whether the video method works well for you. If you have any questions about the videos, you can direct them to Rod at http://lists.topica.com/lists/VBHelperQA. Make sure you check out Rod’s main Web site at http://www.vb-helper.com/ for additional resources. Normally, I won’t revisit a review, but in this case, I thought the additional information helpful and chose to share it with you.


Adding a Web Reference in Visual Studio 2010

There is a new wrinkle in working with Web services in Visual Studio 2010. In previous versions of the product, you could access a Web service by creating a Web Reference to it. All you did was right click the project and choose to add a Web Reference from the context menu. After entering a few bits of information, the IDE created a class you could use to access the Web Service for you. The process was quite simple because all you needed to do then is create an instance of the class and use the methods it provided to access the Web service as if the information was local to the machine.

The default operation for newer versions of Visual Studio is to create a Service Reference, which works differently from the Web Reference you used in the past. Service references are actually quite flexible, but they require a different technique to use. For example, I show how to use a Service Reference to access a Web Service using LINQ in Chapter 13 (page 278) of LINQ for Dummies. LINQ for Dummies is unique, however. Most of my books, including C# Design and Development, rely on the older Web Reference technique, as do all of my articles. This post provides a quick overview of the differences between the two techniques and examines how to create a Web Reference in Visual Studio 2010 (including all of the Express products).


Even though the source code in this post relies on C#, the same principles hold for Visual Basic.NET. In fact, the process for adding and using the Web Reference is almost precisely the same. If you’d like a Visual Basic.NET version of this example, please contact me at John@JohnMuellerBooks.com and I’ll provide another post.

Let’s examine the Service Reference technique first. For this example, I use an extremely simple Web service that’s free and works equally well using either technique. You can find the description of this Web service at http://www.deeptraining.com/webservices/weather.asmx. To begin, you right click the project entry in Solution Explorer and choose Add Service Reference from the context menu. After you type http://www.deeptraining.com/webservices/weather.asmx?WSDL in the Address field and click Go, you’ll see the information shown here (expanded so you can see everything).


Notice that I’ve also typed WeatherService in the Namespace field to give the Service Reference an easy name to remember. The Web Service offers a single method, GetWeather(), that requires a String containing the name of the city as input. The output is a String containing a single word weather forecast for the city in question. This example also has two textboxes, txtCity (contains the input string) and txtWeather (contains the output string), and a button, btnTest. Given this setup, here’s the code you’ll use to access the Web service using a Service Reference:

private void btnTest_Click(object sender, EventArgs e)
    // Create an instance of the Web service.
    WeatherService.WeatherSoapClient Client =
        new WeatherService.WeatherSoapClient();
    // Query the weather information.
    var Output = from ThisData
                 in Client.GetWeather(txtCity.Text)
                 select ThisData;
    // Clear the current information and output the
    // new information.
    txtWeather.Text = "";
    foreach (Char Letter in Output)
        txtWeather.Text = txtWeather.Text + Letter;

As you can see, the example begins by creating an instance of the Web service client. It uses this instance, Client, in a LINQ statement that calls on GetWeather() to obtain the weather for the city specified by txtCity.Text. The odd thing is that the Output actually receives a Char array that contains the weather information, so you have to use some method for turning that array into a string. The example shows just one method for performing this task.

Now, let’s say you need to use a Web Reference instead of a Service Reference. For example, a lot of people are encountering problems using this approach due to encoding problems. A number of Web sites use ISO-8859-1, which won’t work with this technique unless you build a custom encoder (see the article at WCF: Text Message Encoding and ISO-8859-1 Encoding for a discussion of this solution).

In this case, you start by right clicking the project entry in Solution Explorer and choosing Add Service Reference as you did before. However, this time you click Advanced at the bottom of the dialog box, which displays the Service Reference Settings dialog box shown here.


Notice the Add Web Reference button in the lower left corner. Click this button and you’ll see the Add Web Reference dialog box shown here. (I’ve already included the WSDL file URL and provided the name of the Web reference that you should use if you’re following along.)


Click Add Reference and Visual Studio will add the new reference for you. However, this reference appears in the Web References folder, not the Service References folder, as shown here.


I added another button to the application to test this Web Reference, btnTest2. The code is quite a bit different from the LINQ technique used for a Web Service as shown here:

private void btnTest2_Click(object sender, EventArgs e)
    // Create an instance of the Web service.
    WeatherService2.Weather Client =
        new WeatherService2.Weather();
    // Call the Web service method required to obtain the data.
    txtWeather.Text = Client.GetWeather(txtCity.Text);

Both event handlers produce the same result, using the same essential call, Client.GetWeather(txtCity.Text). The difference is that one uses a Service Reference with LINQ and the other uses a Web Reference with procedural code. So far I haven’t found a decided speed advantage for either approach. In this particular example, you type less by using the second approach, but that might not always be the case, especially as the Web service gains in complexity. The point is that both methods are available.

If you’re working with one of my books, you’ll probably need to use this second approach in most cases because it’s the approach that Visual Studio relied upon until recently. As always, let me know if you have any questions about this post or my books at John@JohnMuellerBooks.com.


An Update About Mono

I’m not one to get stuck on a particular form of a technologyit’s best to use the solution that works, rather than make a favored solution try to fit. Many .NET developers miss opportunities to move their solutions to other platforms or use them in places that normally don’t work well with the .NET Framework by leaving out Mono, the .NET Framework alternative. Most people associate Mono with the Macintosh or Linux, but there is also a Windows form of Mono. When Windows Server 2008 Server Core came out without the .NET Framework, I immediately wrote an article about it entitled, “Mixing Server Core with .NET Applications.” In fact, I’ve promoted Mono to frustrated administrators in my book, “Administering Windows Server 2008 Server Core.” Before Microsoft finally came out with a .NET Framework solution, you could use Mono to do things like run ASP.NET applications on Windows Server 2008 Server Core and my book told you how to do it in Chapter 24.

So, I was more than a little disturbed when I heard that the Mono crew had been laid off by Novell. Fortunately, I discovered later that the layoffs are just a prelude to having Mono supported by a new company named Xamarin. To read more about this company, check out Miguel de Icaza’s blog entry, “Announcing Xamarin.” Not only will the new company continue supporting the open source versions of both Mono and Moonlight (the Silverlight alternative), but it’ll create new commercial products for both Android and iOS. In short, this development is actually good because the new company will focus on this incredibly useful technology.

My main concern for the new company is that they won’t have the funding to do everything they envision doing, but that remains to be seen. Reading Mary Jo Foley’s article gives me hope that they’ll succeed. What are your thoughts about Mono? Is it a technology that you’ve used or at least are willing to try? Let me know at John@JohnMuellerBooks.com.


Inheriting Someone Else’s Code

In my book, “C# Design and Development,” I propose a lot of management techniques that could apply to any language. In fact, I’ve used the techniques in this book successfully for years with Visual Basic, C#, IronPython, C++, and PHP, among many others. The idea behind the concepts in this book is to create code that performs well from the outset and is easy to maintain when changes are required. In today’s environment, you can count on changes at some point. There are many reasons to expect changes:


  • The business is bought by someone else and the code must be integrated into a new system.
  • A change in some other component necessitates a change in the application (such as the changes in Vista and Windows 7 security).
  • The business has grown and now needs new features to support its customers.
  • Modifications in the law mean changes to the way the application interacts with data.
  • Technology changes in such a way that the application no longer works properly.

In fact, the reasons for change are endless. I could probably continue writing reasons for change for hours and not cover even a small number of them. The point is that applications will change at some pointit’s simply a matter of time.

If you’ve written code for any time at all, you know that feeling in the pit of your stomach when you discover the perfectly lucid comments you wrote at the time you created the application are no long lucid now. Worse still, the documentation you put together doesn’t tell you what you did and why. Still, after a few hours of looking at your comments and documentation, you can usually begin to fit the pieces together of an application you created because you can recreate the thought patterns you had when you designed and wrote it in the first place.

However, what happens when you’re part of a team or you’re working on an application that someone else designed? Now there isn’t any way to “get back into the groove” of what you did in the past. The thought processes are foreign, so the comments and documentation have to be as comprehensive as possible. Even with great comments and documentation, it’s going to take a while to get up and running with the code.

The longer it takes you to recreate the ideas that went into creating the code, the longer the project update languishes and the more extreme management’s position becomes. The only thing that management knows is that your application (the one you inherited from someone else) is costing the business money. Not only is there a cost associated with the update, but the delay costs the organization money too. Users who have to fight with an outdated application are always unhappy and aren’t productive. So, what techniques can you use to become productive as quickly as possible? That’s what I’d like to know as well. Obviously, reading the documentation and comments are two pieces to the puzzle, but what other techniques do you use?

I’m currently putting an article together that discusses this topic in detail and I’d like to hear about your techniques. If you’ve ever been frustrated by someone else’s code, write me at John@JohnMuellerBooks.com. I’ll let you know when the article is published on Software Quality Connection


Rod Stephens’ Visual Basic Programming 24-Hour Trainer

Learning a new skill, such as programminglearning it quickly and easily, is much harder than it sounds, but “Rod Stephens’ Visual Basic 24-Hour Trainer” makes the task considerably easier. The book begins with possibly the best Introduction I’ve ever seen. The author tells you precisely how to use his book to learn how to code in Visual Basic in a short interval. Additionally, he makes it clear what the book won’t tell you. This is most definitely a book for the rank beginnersomeone who has never written code before and the author makes it clear that better educated developers need not apply. (More advanced readers will benefit more from another of Rod’s books, “Visual Basic 2010 Programmer’s Reference“.)

The chapters tell you how to perform most basic tasks associated with desktop applications. You won’t find any Web development techniques in this book, but the author makes that clear in the Introduction. The reason for focusing on the desktop is that it provides a controlled environment that works anywhereno Internet connection required, no special setup needed. All you need is your system and Visual Basic.

The first chapter is the usual description of how to get Visual Basic installed on your system. Rod discusses a few different options and doesn’t wimp out by covering only Visual Basic 2010 Express Edition like many beginner books do. Because of the target audience for this book, Visual Basic 2010 Express Edition works fine.

The book takes the usual college approach of starting with a basic application, working through variables and structures, and then moving toward more complex application types. You learn all of the basics of working with classes, printing documents, working with the clipboard, and other usual fare for a book of this type. Rod does provide a couple of nice perks such as working with LINQ and databases using the entity framework. The goal is to learn how to use Visual Basic at a basic level, so these exercises will serve the reader well. Someone looking to start by creating a relatively complex example almost immediately will be disappointed. I know that some readers look for this approach now and Rod’s book definitely won’t serve their needs; this book is more traditional (and proven) in its approach.

Each of the chapters provides several methods to learn the material. You can read about the technique, try it yourself on your machine, work through exercises, and even watch a video. Most readers will find a technique that works well for them. Rod suggests using some or ideally all of the techniques to obtain a better learning experience.

The videos are a nice touch and Rod does them well. The longest video I watched came in at 17 minutes, the shortest at 4 minutes. He has a nice speaking voice and an easy manner of approaching the topic. The reader should definitely feel at ease during the presentation. Rod doesn’t resort to humor to cover up a lack of depth in his book. It’s not that he’s incredibly serioushe’s simply straightforward and businesslike in his approach.

Will you actually get through this book in 24 hours as the title says? I doubt it. I tried out a number of the chapters and found that I averaged about an hour in doing them fully (including the exercises) at a rapid pace. There are 39 chapters in the book for a total of 39 hours of training as a minimum. Even if you attack the book at a rabbit-like pace and skip some of the features, you still won’t get through it in 24 hours and manage to gain anything worthwhile. I’d suggest setting aside at least 40 hours of focused time to get through this tome.

The bottom line is that this is a great book for the rank novice to learn a new skill and discover the joys of programming. It’s not the sort of book that anyone who has written code before will want and it’s also not the sort of book that the impatient reader will find helpful. Rod has done a marvelous job of presenting a complex topic in a manner that will help most people get up to speed working with Visual Basic in a short time.