Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

What's New in the Visual Studio 2005 Toolbox for Windows Applications? : Page 6

The toolbox for Windows Forms applications was already pretty rich in Visual Studio .NET 2003, but it will be even richer when the next version ships. In this article, I'll take you on a whistle-stop tour of the new controls slated for Windows Forms 2.0 to make writing code more productive and pleasant than ever.


advertisement
Miscellaneous Controls
To top off this article entirely devoted to a tour of the new controls in the .NET Framework 2.0, I want to briefly review a bunch of controls that provide miscellaneous functions not available in .NET Framework 1.x and loudly requested by the community of developers. Let me introduce you to the MaskedTextBox, SoundPlayer, and WebBrowser controls.

The MaskedTextBox Control
MaskedTextBox is a variant to the classic TextBox control and provides for masked input. The principal extension to the base control is the Mask property.

// Configure the control to accept only // valid social security numbers maskedTextBox1.Mask = "000-00-0000";

The Mask property indicates the mask to use at runtime when typing into the control. The mask must be a string composed of one or more masking elements as defined in Table 2.

Table 2: Masking elements supported by the MaskedTextBox control.

Element



Description

0

Accept any single digit between 0 and 9. The digit is required.

9

Accept a digit or space. The entry is optional.

#

Optional entry, accepts digits or spaces. If this position is left blank, it will be rendered as a space in the text properties of the control. Plus (+) and minus (-) signs are allowed.

L

Required entry, restricts input to the ASCII letters a-z and A-Z.

?

Like "L" but optional.

&

Required entry, accepts a character. If the AsciiOnly property on the control is set to true, this element behaves like the "L" element.

C

Like "&" but optional. If the AsciiOnly property on the control is set to true, this element behaves like the "?" element.

A

Optional entry that accepts any alphanumeric characters.

.

Decimal placeholder. The actual display character is determined by the control's Culture property.

,

Thousands placeholder. The actual display character is determined by the control's Culture property.

:

Time separator. The actual display character is determined by the control's Culture property.

/

Date separator. The actual display character is determined by the control's Culture property.

$

Currency symbol. The actual display character is determined by the control's Culture property.

<

Converts all characters that follow to lowercase. (Shift down)

>

Converts all characters that follow to uppercase. (Shift up)

|

Disable a previous shift up or shift down.

\

Escape. Escapes a mask character, turning it into a literal. "\\" is the escape sequence for a backslash.


The MaskedTextBox control features three text properties: Text, InputText, and OutputText. The first property gets and sets the text as it is currently displayed to the user. Looking at Figure 11, Text is 123-45-67 with two final blanks. InputText simply returns or sets the raw sequence of input characters (1234567 in the example). OutputText represents the real input buffer of the control and returns the formatted text including any placeholder and embedded symbols. Unlike the other two, this property is read-only.

Playing Sounds
 
Figure 11: Entering preformatted input in a MaskedTextBox control.
The SoundPlayer control governs the playback of .wav files from a local path, a stream, or a URL. The playback is articulated in two steps-loading and playing-and can occur both synchronously and asynchronously. In the latter case, an event-driven interface lets you detect the end of the playback.

// Instantiate the control SoundPlayer player = new SoundPlayer(); // Set the location of the file to play player.SoundLocation = wavPath;

You first set the location for the .wav file and then load and play the file. Methods in Table 3 show how to accomplish it. You store the path to the sound file in the SoundLocation property.

Table 3: Method to load and play .wav files.

Method

Description

Load

Loads the specified sound into an internal buffer. The operation occurs synchronously.

LoadAsync

Same as above except that operation occurs asynchronously on a new thread.

Play

Plays the .wav file asynchronously using a new thread. If the no method Load was previously called it first loads the file and then plays it.

PlayLooping

Same as Play except that it loops until the method Stop is called.

PlaySync

Optionally loads and plays the specified .wav file on the synchronously on the primary UI thread.

Stop

Stops playback of the sound.


After your application successfully loads a .wav file in memory, future calls to playback methods will not need to reload the file. If you change the path stored in the SoundLocation property, this action resets an internal flag that causes the sound to be reloaded before the next playback.

If the name of the .wav file has not been specified, or if the file fails to load, the playback methods will play the default beep sound.

A WebBrowser Wrapper
Windows Forms 1.x really needed a control with browsing capabilities, although embedding the WebBrowser ActiveX control isn't a hard task at all. Luckily Microsoft will ship a ready-to-use browsing .NET component in Windows Forms 2.0.

The WebBrowser control lets you host Web pages and other browser-enabled documents in Windows applications. You use the Url property to set the URL of the page to display or you can call the Navigate method.

webBrowser1.Navigate(TextBoxAddress.Text);

Methods like GoBack and GoForward let you move between pages. Overall, the programming interface of the control mimics that of the WebBrowser ActiveX component of which the WebBrowser control is a wrapper. One significant enhancement is the possibility the control offers to display content that you create in your application or retrieve from a database or resource file.

You use the DocumentText or DocumentStream property to get or set the contents of the current document as a string or data stream.

Your application can manipulate the contents of the currently displayed Web page through the Document property. The property returns an HtmlDocument object that provides access to the Dynamic HTML (DHTML) object model for the page.

You can make the communication between the hosted page and the surrounding application bi-directional if you set the ObjectForScripting property (see Listing 3). The object you assign to the ObjectForScripting property is associated with the window.external object in the DHTML object model and can be invoked from within any script code you use, or inject, in the hosted page. The code in Listing 3 shows how to build and display a page dynamically using DocumentText. The dynamically created page contains some script code that uses an external object defined on the host-the window.external object. In the sample code, this external object is the form class itself. As a result, your HTML script can invoke any public method on a .NET class.

Many useful controls and required functionalities didn't find their way into Windows Forms 1.x. Some of them made it through in the next version, currently available in Beta. In this article, I've examined the new stuff in the Visual Studio 2005 toolbox.

In brief, you have new controls in the data binding area, new system components, and a bunch of new user interface elements. In my humble opinion, the real added value to programming power is in the layout controls—a pretty hard feature to build manually. Strip controls, data connectors, and various system components are mostly excellent wrappers around existing Win32 functions or public domain components (like the SerialPort control).

However, in no way does this mean that there's nothing new, or nothing really interesting, in Windows Forms 2.0. Quite the opposite! The improvement in productivity is huge and the design-time support is really great.

For me, the biggest things in Visual Studio 2005 are the designers both for ASP.NET and Windows Forms applications, for data binding and UI-related tasks. Try it out!



Dino Esposito is a mentor at Solid Quality Mentors where he manages the ASP.NET, workflow, and AJAX courseware. A speaker at many industry events including Microsoft TechEd, Basta, DevWeek, and DevConnections, Dino is the author of two volumes of Programming Microsoft ASP.NET 2.0 Applications, for Microsoft Press. You can find late breaking news at http://weblogs.asp.net/despos.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap