Login | Register   
LinkedIn
Google+
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
 

.Finalize() - Summer Travel, Blueberries, and Data Binding


advertisement
nother summer, another Tech·Ed. This is, of course, the 10th anniversary of Microsoft's annual geekfest, and I've had the honor of being at all of them besides one somewhere in the middle of the run. I managed to grab a speaking gig at Tech·Ed in both Dallas and Barcelona this year, and these trips clogged up a travel schedule that was already too full for the summer. Let's review how the speaking and travel have gone since we last spoke. There was the week in Dallas, for Tech·Ed. How much beef can one person consume in one week? I went vegetarian for the remainder of the summer, for the most part.

While I was at a conference in Las Vegas, I bet three quarters and a nickel and won $10.00—living on the edge! I spent two days in Redmond, then four in New York. I enjoyed four Broadway plays in three days. Another two days in Redmond, then four days in Barcelona. If you haven't been to Barcelona, go. It's an amazingly beautiful city with friendly people, great food, an easy transit system, and fascinating architecture. Just don't go in August (luckily, we didn't). It's hot. Apparently, it's currently 106°F as I write this in mid-August. I'll skip that. In July, it was crisp, warm, and a pleasure. Oh, and the conference was fun, too. They really know how to throw a party at Tech·Ed Europe.

Then there was the college roommate reunion over July 4th weekend in Monterey, followed by a week spent recording more training content for AppDev in Minneapolis. My friend, ex-manager, current speaker coordinator at various conferences, editor for this publication, and biking buddy Erik Ruthruff spent the afternoon with me on a beautiful spin around a few lakes in Minneapolis. It's amazing that there's an apparent inverse relationship between the number of days the public can actually spend cycling with the number of miles of usable bike trails to be had in said municipality. Minneapolis has many miles of trails, yet only around 10 days per year that aren't cold. Los Angeles has 360 days per year of perfect riding weather, and perhaps 10 miles of usable trails (the remainder are in gang territory—only the brave and the very fast ride there). Anyway, we biked 17.4 miles. He did fine. My legs barely worked for a few days.

In Minneapolis, I had the fortune of being in town for the yearly Aquatennial Celebration (a yearly festival devoted to—I'm not kidding—water sports, which one local justified in that there's so little time in the year to enjoy the outdoors, they have to find made-up excuses to celebrate, but I'm sure there's a better explanation than that; maybe it has something to do with the statement on the license plates: 10,000 Lakes). In Minneapolis, I had the misfortune of staying at a hotel that was one of many hotels hosting some youth soccer tournament, bringing in pre-teens and their families from around the country. I found that the amount of noise a hotel full of 12-year-olds can make far outweighs the capacity of a good set of earplugs. I fully expected to see the kids staying upstairs from me turn out to be the size of small Mack trucks. I only have the one good ear—it makes sleeping in noisy hotels easier—but it was still an experience I wouldn't wish on anyone. If you were there with your kids at that time, shame on you. There is no excuse for that kind of behavior. Gosh, I feel like the grumpy old neighbor from next door when you were a kid.

One more trip to New York, and then my final trip of this summer marathon—to Houston to visit my family for EatFest 2003 (that's not an official event, we just tend to eat out a lot when I'm in town, and there's a lot of good food to be had in Houston) in the sweltering "it's only 102 out but it feels like 112" heat. Finally, I'm back in lovely Los Angeles.

My favorite part of summer (besides the fact that summer isn't usually hot here—its September and October that will get you) is the fruit at CostCo. You have a CostCo where you are, right? This is one of my criterion for finding a place to "settle" (you don't settle in Los Angeles, you generally pass through) when I grow up—I need to be within an hour of CostCo. Among the palettes of lawn-care products, batteries, film, and life-time supplies of canned tuna, they sell the most amazing fruits and vegetables there, and I eat a lot of peaches and blueberries in the summer. Maybe the rogue strawberry, as well. CostCo packages their fruit in mass quantities, and I just go for it. My friend, business partner, and co-author Andy Baron and I were shopping at CostCo once, and we decided to split a case of blueberries. We were discussing the recommended serving size for a portion of blueberries, and Andy hit the nail on the head when he suggested that the correct service size is "the container they're in." Andy's full of similar deep thoughts. On the other hand, I can't help thinking of Willy Wonka, with the little girl who ate the forbidden gum and turned blue and berry-shaped. I'm on my way. Of course, blueberries are both good and good for you. Everyone tells me they're excellent anti-oxidants. I'm not sure what oxidants are or why I should be against them, but I'm glad blueberries take care of them for me.

Speaking of Andy, he recently solved an apparent .NET programming mystery for me, and I wanted to share his solution. Let me provide a little setup: I wanted to provide a simple Windows-based data entry form in which users could enter their phone numbers, selecting from various types of phone numbers (Home, Mobile, Work, and so on). I created an array containing the phone types, and bound four ComboBox controls to the array. That is, the form contained the following code (simplified for display here):

private String[] PhoneTypes = new String[] {"Home", "Mobile", "Work"}; private void Form1_Load(object sender, System.EventArgs e) { comboBox1.DataSource = PhoneTypes; comboBox2.DataSource = PhoneTypes; comboBox3.DataSource = PhoneTypes; }

I load the form, select a phone type from one of the combo boxes, and the result is certainly not what I intended. Try it yourself—it will just take a minute. I'll wait.

As you saw, selecting a phone type from one of the combo boxes selects the same phone type in all the combo boxes. You may wonder what's going on. At first I thought that one solution would be to create three copies of the PhoneTypes array. I could assign each copy of the array as the data source of one of the combo boxes, but that would be an inefficient way to go about it. The problem is that when you bind controls to data sources on a Windows form, as I've done here, the form creates a single BindingContext object to manage the data binding, and adds a single object that inherits from the BindingManagerBase class (here, a CurrencyManager object) for each data source. Because there's only a single BindingContext, and only a single CurrencyManager object, there's only a single current row within the data source, and each combo box always displays the same value.

Andy came to the rescue here with what turns out to be a perfectly obvious solution, once you know it. That is, you aren't required to use the form's single BindingContext for all your data binding. You can create your own BindingContext object, which will then manage individual CurrencyManagers for each instance. In other words, rewriting the code so that it looks like this solves the problem:

private String[] PhoneTypes = new String[] {"Home", "Mobile", "Work"}; private void Form1_Load(object sender, System.EventArgs e) { // Use the form's default BindingContext. comboBox1.DataSource = PhoneTypes; comboBox2.BindingContext = new BindingContext(); comboBox2.DataSource = PhoneTypes; comboBox3.BindingContext = new BindingContext(); comboBox3.DataSource = PhoneTypes; }

ComboBox1 can use the form's default BindingContext, but the other two controls need to set up their own individual contexts for binding (supplied by their individual BindingContext objects). That's all it takes to solve this problem—that is, adding two lines of code, creating a unique BindingContext object for all but the first ComboBox control, made it possible to use a single data source for multiple controls.

The workings of the BindingContext and CurrencyManager objects can seem confusing at first, but once you understand how they're working under the covers to provide data binding for Windows forms, you'll be amazed at the elegance and the simplicity of the design. And thanks to Andy for the correspondingly elegant and simple solution to my little problem. And speaking of elegant and simple: I'm heading out for more blueberries. The season's just about over, and once they're gone, they're gone until next summer.



   
Ken Getz is a regular columnist for CoDe Magazine.
Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date