Persisting Colors and Other Settings with Visual Basic

WEBINAR:On-Demand

Introduction

Persisting information means to save information (of any type) and to retrieve it from the location to which it has been saved. With today's article, I will show you two ways to save colors. Now, the first question that pops into your head might be: Why? Well, the answer is simple. People like saving settings such as the location and size of a particular form, but they also like to save colours. This just adds a little professional touch in rounding off your application.

Persistence

As I mentioned earlier, persistence means to save and retrieve information. Where can you save information? Anywhere on the computer… No, that is a sick joke—because that is obviously not what you meant. You can save information in the system Registry, or, you can use the built-in Application Settings of your application. Obviously, there may be more, but this article serves only as an introduction, or a nudge in the right direction. For more information about persisting information, have a read through this article.

Registry

The Registry stores lots of application settings. This is usually small pieces of text (actually any data, but up to a point) that would be senseless dedicating a whole file to it. For more information regarding the Registry, have a look through this (very old) article of mine.

Our Project

The purpose of today's project is simply to store a color value into either the Registry or Application.Settings. There isn't much of a design. For interest's sake, design your Form in your Windows Forms application to resemble Figure 1.

Figure 1: Our Design

Add a ColorDialog to your project as well.

Before I continue to the code, I must first show you how to set up your Applications.Settings. Don't worry; it is really not complicated at all. There are two ways:

Through the use of the Properties Window

Through Project properties

Properties Window

To add a custom setting, such as a color, follow these steps:

Select the form.

Open the Properties Window, if not already open, by pressing F4.

Expand the ApplicationSettings item on top of the Properties list.

Click the ellipses button next to PropertyBinding. This will show the following screen:Figure 2: Property Binding

Select BackColor from this list, and click the blue New link that is displayed.

Give it a name, scope, and a default value, such as shown in Figure 3.Figure 3: Closeting

Click OK.

Notice the BackColor Property inside the Properties Window. This indicates that it is a custom application setting, and that the color you see in design time might not be the color you will see when the app starts because it relies on the color specified by the user.Figure 4: BackColor property

Project Properties

To Add a setting through the Project Properties, follow these steps:

Click Project.

Click Project Name Properties—where Project name is your project's name.

Click Settings.

Fill in a Name, Type, and the setting as shown in Figure 5:Figure 5: Project Properties

Code

Let's get down to business!

Although due to the fact that you have already set the BackColor property in Application.Settings, it will be saved automatically, but sometimes you will need to load the saved setting, especially when a user is involved. Add the next code behind the Button1 Click event:

This saves the actual color value into the Registry. This is not the name; this is the physical color value. Now, don't let the preceding code fool you. Loading it from the Registry is not as easy, as in the next code segment:

This code basically reads the color value. Then, it breaks the string apart with the use of the following two functions (which is just a simpler way of breaking strings apart with SubString):

Public Shared Function strStringRight(strInput As String, _
intStart As Integer) As String
Dim result As String = strInput.Substring(intStart)
Return result
End Function
Public Shared Function strPartRight(strInput As String, _
intStart As Integer, intLength As Integer) As String
Dim result As String = _
strInput.Substring(intStart, intLength)
Return result
End Function

As I mentioned, the original string gets broken up. It basically gets the Alpha, Red, Green, and Blue values. Once all those values have been read, it is easy to assemble the physical color again via the use of the built-in Convert.ToInt16 methods.

I have attached a working sample.

Conclusion

I hope you have enjoyed today's article. Until next time, cheers!

About the Author

Hannes du Preez is a Microsoft MVP for Visual Basic for the seventh year in a row.