Developer .NET UPDATE—brought to you by SQL Server Magazine

http://sqlmag.com

This Issue Sponsored By

DEVCONNECTIONS: WIN A HARLEY MOTORCYCLE

http://www.devconnections.com

1. Developer .NET Perspectives

by Marquis Howard, marquish@interknowlogy.com

  • Handling Events that Fire Unexpectedly
  • In "Compact Framework Tips and Tricks" (http://www.winnetmag.com/windows/article/articleid/41243/41243.html) and "More Compact Framework Tips and Tricks" (http://www.winnetmag.com/windows/article/articleid/41513/41513.html), I provided some helpful hints about how to develop Windows .NET Compact Framework applications. In this column, I continue that discussion by pointing out the differences between bound and unbound controls when working with the ComboBox control. However, the concepts I discuss here also apply to Windows .NET Framework applications.

    If you pay close attention, you'll find that the ComboBox's SelectedIndexChanged event fires somewhat unexpectedly. The SelectedIndexChanged event fires every time a user selects a different item in the ComboBox. The collection of items, called the Items collection, has a zero-based index, and each item has an index value. When you choose a different item, you're changing from one index value to another, thus raising or firing the SelectedIndexChanged event.

    When the SelectedIndexChanged event fires, the system ignores the event, unless you've added a handler to your code. However, developers commonly use such handlers to change a portion of an application's display or change other default values based on the current selection. When you use such handlers, you need to account for when the event fires, especially if you've tied this event to a resource-intensive action, such as a database query or a Web service call.

    However, under certain conditions, the SelectedIndexChanged event sometimes fires when you're initializing a ComboBox. So, you need consider how to handle the unexpected firing of this event in your application's code.

    You can add items to the ComboBox's Items collection two ways. You can use the Items.Add approach or the DataSource approach.

    In the Items.Add approach, you use the Items.Add method to add elements. For example, to add three elements (labeled "one," "two," and "three") to the ComboBox's Items collection, you use the code

    this.comboBox1.Items.Add("one");
    this.comboBox1.Items.Add("two");
    this.comboBox1.Items.Add("three");

    In the DataSource approach, you use the DataSource property to bind the control to a DataTable or an Array. To add the same three elements to the ComboBox's Items collection with this approach, you use the code

    string\[\] sArray = new string\[\] \{"one","two","three"\};
    this.comboBox1.DataSource = sArray;

    When you use the DataSource property to load data into the ComboBox, you'll notice that although you haven't changed the selected item, the SelectedIndexChanged event fires.

    Let's create a sample project to better illustrate the Items.Add and DataSource approaches. In Visual Studio .NET, create a Compact Framework project by following the basic steps I describe in "Compact Framework Tips and Tricks." When creating this project, you'll encounter the Smart Device Application Wizard, which produces a screen that asks you two questions: "What platform do you want to target?" and "What project type do you want to create?"

    The platform you select determines the default project settings associated with your display area and device characteristics. Choosing the correct platform is important. For example, a Pocket PC application might not work correctly if you run it on a Windows CE device because the Pocket PC has special control buttons that the Windows CE device doesn't support. Conversely, if you run a Windows CE application on a Pocket PC, the user will see only a part of the UI (the user would have to scroll horizontally to see the rest of it) because the Pocket PC has a smaller display area than the Windows CE device.

    The project type you select determines the template that you'll use for the project. The wizard displays the templates supported by the platform you selected. No matter which platform you select, the template list always includes the Class Library and Windows Application options. I'm not going to cover these options in detail, except to mention that if you use a class library that supports the Compact Framework, you can reuse the class library for a desktop or Web-based application (unless it includes a reference to Microsoft SQL Server 2000 Windows CE Edition).

    After you select your platform and project type, click OK to bring up Visual Studio .NET's Design window. From the Toolbox, drag a ComboBox control onto the form. Double-clicking the ComboBox control causes the form's Code Behind section to appear. Visual Studio .NET automatically generates an event handler method for you called comboBox1_SelectedIndexChanged. For test purposes, place the following line of code in the comboBox1_SelectedIndexChanged method, which will be the only piece of code inside this method. This particular piece of code will open up a message box and display whatever item is selected in the ComboBox:

    MessageBox.Show(comboBox1.Text);

    Next, you need to add initialization code. The place to add this code is in the handler method for the form's Load event. If you don't see the form_load handler method, return to the Design window of Visual Studio .NET. If you double-click the form, Visual Studio .NET will take you to the code for this handler. You can then add the code for the Items.Add approach I showed you previously. This code will add the three elements to the Items collection and place them in the form_load handler method.

    Now you need to run the sample project in the debugger. You should see the form load without opening the message box. The ComboBox's drop-down box will be empty. Whenever you select one of the three elements in the ComboBox, the event will fire and the message box will open.

    At this point, stop the debugger (leaving the emulator open) and return to the code for the form_load handler method. Replace the Items.Add code with the DataSource code that I provided earlier. To see how this new code changes your application's behavior, relaunch your project in the debugger. For starters, you'll likely notice that the message box pops up twice as the form is loading. Next, you might notice that the drop-down box is no longer empty. Instead, it includes the first entry from the data source (i.e., "one"), and this entry is selected by default.

    The problem with the DataSource approach is that although the code is doing exactly what it should do, it might not necessarily be doing what you expect or want. More important, if this handler was making a Web service call each time, the results of this behavior could disrupt performance. The easiest solution is just to add a variable to your form to indicate that the form is loading, but even that solution doesn't address all the problems.

    The extra event firing is just the tip of this binding iceberg. So, in my next column, I'll describe the other differences associated with binding controls and discuss when you might find this technology appropriate. The firing of extra events might not be crucial, but as you examine the various behavior differences of a bound control, you'll understand why there's a right and a wrong time to work with bound controls.


    Sponsor: DEVCONNECTIONS: WIN A HARLEY MOTORCYCLE

    Dev Connections conference and expo will be held April 18 - 21. Back by popular demand are concurrently running events Microsoft ASP.NET Connections, Visual Studio Connections, and SQL Server Magazine Connections. Details about workshops, sessions, and speakers are online, including the exclusive Microsoft Day on "Yukon" and "Whidbey". Save $200, receive access to all three conferences for one price, and get a chance to win a Harley motorcycle. Go online or call 800-438-6720 or 203-268-3204.

    http://www.devconnections.com

    2. Announcements
    (brought to you by SQL Server Magazine)

  • Need to Get Your Hands Wrapped Around SQL Server?

  • Subscribe to SQL Server Magazine and get all the newest information and tools needed for SQL Server. You'll receive 12 print issues, along with full access to the entire online article archive. Bonus--you'll also get the latest Microsoft System Table Map Poster. Subscribe today!

    https://secure.pentontech.com/nt/sql/index.cfm?promocode=psep2142fd

  • 5 Years Worth of SQL Server Content in One Place

  • Subscribe to the SQL Server Magazine Master CD and get high-speed, portable access to all articles, code, tips, tricks, and expertise published in SQL Server Magazine and T-SQL Solutions. Search by keyword, subject, author, or issue and find fast answers to your SQL Server questions. Subscribe today!

    https://secure.pentontech.com/nt/sql/index.cfm?promocode=scep2842md

    3. Resource

  • Featured Thread: Unable to Run DOS Command in a Visual Basic .NET Application
  • Novice forum member bip is trying to run a DOS command from a Visual Basic .NET command-line application. The DOS command includes one parameter and redirects its output to a file. The command bip wants to run is

    c:\program files\ibm\subsystem device driver\datapath.exe
    query device > output.txt

    where "query device" is the command parameter. According to the Help file, he has to include paths in triple quotes ("""). He tried doing so but couldn't get the command to work correctly. If you can help, go to the following URL:
    http://www.winnetmag.com/forums/rd.cfm?cid=55&tid=67227

    4. Events Central
    (A complete Web and live events directory brought to you by Windows & .NET Magazine: http://www.winnetmag.com/events )

  • SQL Server Magazine Connections: Win a Harley
  • The SQL Server Magazine Connections conference will be held April 18-21, 2004, with concurrently running events Microsoft ASP.NET Connections and Visual Studio Connections. Register today and save $200, receive access to all three conferences for one low price, and get a chance to win a Harley. Register online or call 203-268-3204 or 800-438-6720.

    http://www.sqlconnections.com

    5. New and Improved
    by Shauna Rumbaugh, products@sqlmag.com

  • Create Reusable, Robust Objects and Libraries
  • Eiffel Software released EiffelStudio 5.4, a development environment that lets application developers create reusable, robust objects and libraries. Based on the Eiffel OOP language, EiffelStudio includes full-feature browsing, multiple inheritance facilities, multithreading support, built-in metrics, and profiling capabilities. The product's "Design by Contract" methodology helps prevent bugs from occurring and traces and repairs existing bugs. EiffelStudio 5.4 runs on Windows (including Microsoft .NET), UNIX, Linux, Mac OS X, VMS, and embedded systems. Contact Eiffel Software at 805-685-1006 or info@eiffel.com.

    http://www.eiffel.com

    Sponsored Links

    Quest Software, Inc.
    Database Contention Affecting SQL Server Performance? Download White Paper.

    http://www.quest.com/landing/sqlmag_link1.asp

    Innovartis Ltd
    Auditable, Repeatable, Reversible team SQL code? Meet DB Ghost.

    http://www.dbghost.com

    6. Contact Us

  • About Developer .NET Perspectives -- bills@interknowlogy.com
  • About the newsletter -- kbemowski@winnetmag.com
  • About technical questions -- http://www.sqlmag.com/forums
  • About product news -- products@sqlmag.com
  • About your subscription -- Developer_dot_NET_update@winnetmag.com
  • About sponsoring an UPDATE -- contact Kate Silvertooth (ksilvertooth@sqlmag.com)
  • This email newsletter is brought to you by Windows & .NET Magazine, the leading publication for IT professionals deploying Windows and related technologies. Subscribe today.

    http://www.winnetmag.com/sub.cfm?code=wswi201x1z

    View the Windows & .NET Magazine Privacy policy at

    http://www.winnetmag.com/aboutus/index.cfm?action=privacy

    Windows & .NET Magazine a division of Penton Media, Inc.
    221 East 29th Street, Loveland, CO 80538
    Attention: Customer Service Department

    Copyright 2004, Penton Media, Inc. All Rights Reserved.