Tuesday, May 17, 2005

Make your .NET demos better with a TraceListener

Last night I was writing a sample application for one of my DevTeach sessions and I thought I'd share this little trick. If you've ever tried to fiqure out a speaker's sample code after the conference when you got home you'd appreciate this as well. Usually I just write copious amounts of comments in my code to help people but last night it dawned on me how I could add a very simple TraceListener to my application to easily display the application's sequence of events in a list box. This way the developer could see exactly what was going on at runtime without having to debug the code.

If you're unfamiliar with TraceListeners have a read here.

Trace listeners monitor the output of the Debug and Trace messages in an application. You can create custom listeners and then add them to the Debug or Trace Listeners collection on application startup quite easily:
Imports System.Diagnostics

Public Class Main
 ''' --------------------------------------------------------------
 ''' <summary>
 ''' Main enrty point for the application.
 ''' </summary>
 ''' <remarks>
 ''' </remarks>
 ''' <history>
 '''  [Beth] 5/16/2005 Created
 ''' </history>
 ''' --------------------------------------------------------------
 Shared Sub Main()
  '-- Registers a trace listener 
  Debug.Listeners.Add(New DemoAppTraceListener)

  Application.Run(New MainForm)
 End Sub
End Class
The idea for the DemoAppTraceListener class is very simple. I just want to globally broadcast the trace messages so that they could be displayed by the application anywhere. We can do this by declaring a Shared (static) event:
Imports System.Diagnostics

''' -----------------------------------------------------------------------------
''' Project  : DataCachingStrategies
''' Class  : DemoAppTraceListener
''' 
''' -----------------------------------------------------------------------------
''' <summary>
''' Globally broadcasts the trace messages in this demo which are displayed in the UI.
''' </summary>
''' <remarks>
''' </remarks>
''' <history>
'''  [Beth] 5/16/2005 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Class DemoAppTraceListener
 Inherits TraceListener

 Public Shared Event MessageReceived(ByVal e As MessageEventArgs)

 Public Overloads Overrides Sub Write(ByVal message As String)
  RaiseEvent MessageReceived(New MessageEventArgs(message))
 End Sub

 Public Overloads Overrides Sub WriteLine(ByVal message As String)
  RaiseEvent MessageReceived(New MessageEventArgs(message))
 End Sub

End Class

''' -----------------------------------------------------------------------------
''' Project  : DataCachingStrategies
''' Class  : MessageEventArgs
''' 
''' -----------------------------------------------------------------------------
''' <summary>
''' This EventArg is used to broadcast messages from the DemoAppTraceListener.
''' </summary>
''' <remarks>
''' </remarks>
''' <history>
'''  [Beth] 5/16/2005 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Class MessageEventArgs
 Inherits EventArgs

 Private m_msg As String = String.Empty
 Public ReadOnly Property Message() As String
  Get
   Return m_msg
  End Get
 End Property

 Public Sub New(ByVal msg As String)
  m_msg = msg
 End Sub
End Class
Now all you have to do is handle the event and display the messages. Oh, yea you also have to actually put Trace messages in your code now too!
Public Sub Foo
        Trace.Write("Entering Foo method")
.
.
.
End Sub
To display the messages in my demo I created an MDI parent and put a simple list box on the bottom. Then I just added a handler to the DemoAppTraceListener.MessageReceived event. I added the handler in the constructor because this code is hidden in the form designer region and people won't be distracted away from the demo code.
#Region " Windows Form Designer generated code "

Public Sub New()
 MyBase.New()

 'This call is required by the Windows Form Designer.
 InitializeComponent()

 'Add any initialization after the InitializeComponent() call
 AddHandler DemoAppTraceListener.MessageReceived, AddressOf Me.DisplayMessage
End Sub
.
.
.
Finally I added the few lines of code to display the message in the list box and select the last message:
Private Sub DisplayMessage(ByVal e As MessageEventArgs)
 Me.lstMsgs.Items.Add(Me.lstMsgs.Items.Count.ToString + ". " + e.Message)
 Me.lstMsgs.SelectedIndex = Me.lstMsgs.Items.Count - 1
End Sub
Another cool thing about using a TraceListener is that the developer can still copy/paste the code in the demo into their own applications without having to remove the Trace messages if they don't want to.

Now if you come to my data caching session at DevTeach you'll get to see the TraceListener in action! ;-)

Saturday, May 14, 2005

A component walks into a bar....

The component asks the bartender via .NET remoting:
"Gimme a beer"
Bartender:
"What kind of beer?"
Component:
"Just give me all of them one by one and I'll drink the one I like."

The component asks the bartender via Web Services:
"Gimme a beer"
Bartender:
"What kind of beer?"
The component thinks a minute.
"I didn't know you'd have more than one. I just want a beer."
Bartender:
"What kind of beer?"
Component:
"A plain one."
Bartender:
"What's plain beer? How about a bud?"
Component:
"Umm.. ok."
The component tries to take the beer but it slips onto the floor and the glass breaks.
Bartender:
"What's wrong with you?"
Component:
"I got a slippery SoapException."

Thursday, May 05, 2005

Refactor, refactor, REFACTOR!

So much for hiding the word "refactor" from VB.NET developers... (oh how I love the fact that it's spelled with a big fat exclamation point!!!!)

Have you taken a look at this new VB 2005 refactoring? I think this refactoring interface blows doors off the C# one. Everything is context sensitive right at your fingertips and all inline with the code editor -- no modal dialogs! And it's just a whole heck of a lot smarter about what you would want to do with the selected code. Any developer will be able to pick this up in two minutes. Thank you Microsoft for making such an awesome deal with Developer Express to provide us all with this free tool. (I think there's a C# one you can buy for $99.)

Thursday, April 21, 2005

Custom Exceptions and Remoting

I just thought I'd put up a reminder post about enabling the throwing of custom exceptions from your remote objects since this was a change from 1.0 to 1.1. It recently bit me in the butt when I was setting up a new system. If you need to throw your own exceptions from your remote objects that are hosted in IIS, you need to make sure that customErrors="Off" in your Web.config file. By default it's set to "RemoteOnly" which means it will work on your development machine, but not when clents requests are coming from remote machines.

Wednesday, April 13, 2005

Combobox Databinding Woes

UPDATE: This post is for Visual Studio 2003. For Visual Studio 2005 content please see this post.

I've been noticing a lot of questions on the newsgroups related to Winforms databinding and the combobox and I thought I'd post something up here to help people out. There are a couple very common scenarios in which people use the combobox:

1. To display information from a lookup table and send the selected value into another table's field.

2. To display a list of parent table's records and use that as a filter to display related child records. For instance, as the user selects a record in the combobox, you want to display all the related child records in a grid.

As usual, the trick is setting up the data binding properly and using the currency managers. In the first case it's not necessary to set up a data relation in your dataset between the lookup table and the table you're editing, but it doesn't hurt. In the second case it is necessary to create a relation between your parent and child tables. Let's take an example from our beloved Northwind:

Private Const SQL_CONNECTION_STRING As String = _
 "Data Source=localhost;" & _
 "Initial Catalog=Northwind;" & _
 "Integrated Security=SSPI"

Try
 Dim ds As New DataSet
 Dim cnn As New SqlConnection(SQL_CONNECTION_STRING)

 Dim da As New SqlDataAdapter("SELECT * FROM Region", cnn)
 da.Fill(ds, "Region")

 da = New SqlDataAdapter("SELECT * FROM Territories", cnn)
 da.Fill(ds, "Territories")

 ds.Relations.Add("Region_Territories", _
  ds.Tables("Region").Columns("RegionID"), _
  ds.Tables("Territories").Columns("RegionID"))

 ds.DataSetName = "RegionTerritories"

Catch Exp As Exception
 MessageBox.Show(Exp.Message)
End Try
In the first scenario we want to select a Region from the combobox and have that value populated into the Territorries record. In this case you'll need to set up the following properties on your Combobox:
Me.ComboBox1.DataSource = ds.Tables("Region")
Me.ComboBox1.DisplayMember = "RegionDescription"
Me.ComboBox1.ValueMember = "RegionID"
These properties control what items are displayed in the combobox and what value is used when the user makes a selection. Now to get that value into the Territories table, you'll need to set up a data binding:
Me.ComboBox1.DataBindings.Add("SelectedValue", ds, "Territories.RegionID")
Okay we're all set, right? Well... not exactly. You'll also need to call EndCurrentEdit on the territories currency manager at some point in order to write the value back to the dataset. Depending on the style of your form you could do this from an "Update" button (similarly you could call CancelCurrentEdit from a Cancel button). However, when working with datasets I find it much easier to use the dataset methods for Accepting/Rejecting row changes. So 99.99% of the time I just call EndCurrentEdit from the SelectedIndexChanged event handler of the combobox itself:
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, _
  ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
 '-- This forces the comboxbox's value to be written to the dataset.
 Dim cm As CurrencyManager = DirectCast(Me.BindingContext(ds, "Territories"), CurrencyManager)
 cm.EndCurrentEdit()
End Sub
The cool thing (or anoying thing depending on how you look at it) about EndCurrentEdit/CancelCurrentEdit on the currency managers is that they cancel or commit only the fields in which they have bindings for where as the dataset rows' AcceptChanges/RejectChanges works on the whole row regardless of the data bindings. (It would be *really* nice if the currency manager had a property for this like "AlwaysCommitChanges" so we wouldn't have to call EndCurrentEdit all over the place.)

Now let's take our second scenario where we want to use the combobox as a row filter. In this case we have to have a relation set up between our parent and our child; in the example this is Region_Territories. The combobox properties can be set up just like the first example:
Me.ComboBox1.DataSource = ds.Tables("Region")
Me.ComboBox1.DisplayMember = "RegionDescription"
Me.ComboBox1.ValueMember = "RegionID"
Technically we don't need to specify the ValueMember property this time because we're not writing it anywhere, but it doesn't hurt to specify it. Next you'll need to set up the Datasource and DataMember properties of the DataGrid using the relation path. It is very important to get the path right otherwise the datagrid will not filter automatically as we move the position in the parent:
Me.DataGrid1.DataSource = ds
Me.DataGrid1.DataMember = "Region.Region_Territories"
Okay we're all set, right? Well... not exactly (I knew you were going to say that <g>). Unfortunately a combo box won't move the CurrencyManager's position for you like list controls do (Grids, Listboxes). So the trick is to get a hold of the parent currency manager and move the position manually by handling the combobox's SelectedIndexChanged event:
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, _
  ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
 Dim cmParent As CurrencyManager = DirectCast(Me.BindingContext(ds, "Region"), CurrencyManager)
 cmParent.Position = Me.ComboBox1.SelectedIndex
End Sub
Because you set up the datagrid to display the related territories by specifying the relation path Region.Region_Territories as the DataMember, the grid will automatically filter it's rows based on the selected parent row in the combobox.

The currency managers are your friends. You can obtain currency managers for any table/path in your dataset even if there are no control bindings set. You can also use the currency managers to disable controls when the position moves to -1 (no records) Here's an example. The currency managers maintain dataviews so you can easily access the current DataView as well as the current DataRowView:
Dim dv As DataView = DirectCast(cmParent.List, DataView)
Dim dvr As DataRowView = DirectCast(cmParent.Current, DataRowView)
Complex winforms databinding can take some practice, but once you get the hang of it you can create some very cool forms. Have fun!

Stop the mailing list insanity!

Looks like I'm not the only one frustrated with unwanted email that won't unsubscribe! (Make sure you scroll to the diagram on page 3. WARNING: This may offend you.)

Friday, April 08, 2005

Sunday, April 03, 2005

I'd like to thank the academy...

This was totally unexpected and I just want to extend a personal thank you to Microsoft, GiftRAP, and especially the community!

Friday, April 01, 2005

Spoof someone for April fools!

http://www.msnsearchspoof.com/index.aspx

MSN Search celebrates April Fool's Day by introducing Spoof, a tool to let you create funny search results about a friend, family member, or co-worker. When you're done, you can send the page to the target or anyone else you think might get a laugh out of it.

Tuesday, March 29, 2005

Finally a hybrid that doesn't suck?

It's not like I hate the environment or anything (please, all I do in the summer is camp and mountian bike), it's just that hybrid cars are, well, sloooooow. I'm a car enthusiast and I've tweaked my Subaru WRX wagon to put it over the 350hp mark. It's a 4-cylinder turbo wagon that goes 0-60 in 4.9 seconds. That's respectible. Gas milage is 21mpg so that kinda sucks, but it isn't horrible. It's better than most SUV's on the market today (and I still have 4WD). I love my car. I just love cars.

I never thought these hybrid cars would really take off until two things happened. #1 - they were really fast. #2 - you can modify them. I admit #2 is not as important to most people, but I don't know many people who don't like power in their vehicle.

I came across the Lexus GS 450h today and it looks like Lexus has finally answered my #1 complaint about hybrids.

This new hybrid powertrain is unique to the GS 450h. It features a 3.5-liter V6 engine teamed with a high-output electric motor that, when combined, will produce "substantially" more than 300 horsepower, according to Lexus vice president of marketing Mark Templin. Lexus claims that this hybrid will be quicker than the new V8-powered GS 430, yet it should achieve fuel economy similar to that of a compact sedan with a 4-cylinder engine.

Only time and test driving will tell, but I think car manufacturers are moving in the right direction with these hybrids.

Sunday, March 27, 2005

Happy Easter!

Growing up Catholic (and 12 years of Catholic school!) I remember Easter as being a pretty big religious holiday. When me and my sister were kids, my mom used to make us cute little Easter outfits to wear to church. My sister was a little more into the cute dresses than I was but I do remember loving to wear those cool white bonnets with the long ribbons down the back. They also flew like frisbies if you threw them hard enough.

After Mass we used to go over to my Nona and Nono's (mom's parents) house and we'd have an Easter egg hunt. Nona would fill those plastic colored eggs with money and hide them in the yard. And I'm not talking dimes and nickels here, I'm talking paper money... $5, $10, even sometimes $20! The entire family used to participate, not just the kids (who wouldn't!). Though I do vaugely remember the kids got a head start.

One year, we all come back onto the patio and while opening our eggs and counting our money we see Nona hovering around us like she's looking for something. My mom asks her what she's looking for and she says that there's still a $20 egg out there somewhere. Now let me give you a little background on Nona's yard. Nona is one of the best gardeners I know (even today). She used to have beautiful tropical plants everywhere. She lived in Palos Verdies, CA overlooking the ocean and Catalina island. Her backyard looked like the Italian island of Iscia where she was from (off the Almafi coast). The yard was big. Real big. Real lush. Nona didn't make a treasure map. Whoops. We all looked for an hour and no money egg anywhere (though I do remember recruiting some more snails for my snail army). A couple years later while gardening, Nona finds the egg and the semi-decomposed $20 bill. But even the liquor store wouldn't take it when she tried to buy her lottery tickets. Great memories.

Although I'm not very religious now, Easter Sunday is still a day to spend with family eating and drinking (and drinking and drinking). This weekend has been really fun so far. My honey, Alan, is Jewish so he was very facinated with the whole Easter egg decorating. Friday was his first time. See, I usually go for an art deco look which basically involes throwing the eggs in as many colors as possible and then putting as many hideous stickers on them as I possibly can. Quantity, not quality, baby. Alan, on the other hand, took so much care in designing his eggs (all two of them). I do have to say that his were definate masterpieces. Of course, I had to break the news to him that my dad was just going to crack them open and eat them. But it was fun.

Later this afternoon we're heading over to mom's. Typically my mother makes enough gormet food to put Martha Stewart to shame. If 8 people are coming, she'll make food to feed at least 25. Now when we were kids we usually had some kind of roast or ham on Easter. When we got older the tradition changed to going out to a nice big brunch. This year Nona wanted to take it back home and cook. This year we're making rabbit. Yes, we're cooking up cute little easter bunnies and eating them. And if that's not enough, were also roasting a cute little lamb to go with the easter bunnies. My mom would never let Nona make this dish on Easter when we were kids because of the obvious freak-out factor, but we used to raise rabbits and I LOVE LOVE LOVE rabbit. And the way my Nona makes it is amazing. I can't wait!

I know my family is not normal, but we all love eachother very much and that's what these religous holidays mean to me nowadays... getting together with the people you love most in this world and having a good time. HAPPY EASTER EVERYONE!

Sunday, March 20, 2005

Podcasting to Fame

Carl's in the news about podcasting! If you haven't seen the DNR or Monday's shows you're definately missing out on some awesome podcasts. Carl's now setting up a customizable podcast called The Daily Commute. I can't wait to try this out.

Friday, March 18, 2005

Come to DevTeach 2005 in Montreal!

I'll be speaking at DevTeach 2005 this year in beautiful Montreal June 18-22. If you missed out last year, you should definitely consider it this year. I had a blast and those wonderful Canadians are so hospitable. I'm very excited to be coming back. Check out my sessions topics here and here. I'm doing a two parter with Nick Landry (aka Mobile Man) on designing distributed apps for multiple user interfaces. These sessions are going to be very "how to" focusing on a lot of different issues developing usable enterprise information systems. I can't wait!

DataSet Debugger Visualizer for VS 2003!

I don't know how I missed this one. This is probably my most favorite VS 2003 add-in to date. Anyone doing database development in .NET is probably using the DSWatch add-in. This add-in lets you select a dataset variable while in the debugger and presents a form with an XML representation of the dataset's data (as well as a datagrid).

Well say goodbye to DSWatch and hello to the XML Visualizer! This add-in rocks! Among the cool features it lets you view the schema of your dataset as well as the row versions. It also works on DataViews or any XML serializable type! Who needs to wait for Whidbey? Get this now!

Thursday, March 17, 2005

My Singleton Answer

In my previous post I asked a question about using the singleton pattern for application-wide services that were stored in an object repository. With the help of the commenters, I decided not to implement the singleton pattern in this situation because since the services were only accessed via shared members on the object repository there is no need to force the service classes into the singleton pattern. This also makes them easier to extend (inherit).

That said, the discussion led to the question about how to implement lazy initialization of shared members in a thread-safe manner. I mentioned I was using double-checked locking, but someone pointed me to this interestering article and this discussion. I decided to test out double-checked locking versus just locking before the read in my distributed app (basically just created a remoted unit test) and I observed no difference in performance really. The test was just the "give me a warm and fuzzy feeling" test so I didn't write any performance counters or anything. But just measuring speed of the calls with 40 concurrent threads calling the components 100 times I only saw between a 1 and 10 milisecond average difference in speed. This is really nothing to me so I'm going to go with the easier, safer pattern of locking the whole shee-bang:
Private Shared m_service1 As ExtendedService
Private Shared _syncRoot As New Object

Public Shared ReadOnly Property Service1() As ExtendedService
  Get
    SyncLock (_syncRoot)
      If (m_service1 Is Nothing) Then
        m_service1 = Factory.CreateExtendedService()
      End If
    End SyncLock
    Return m_service1
  End Get
End Property

Wednesday, March 16, 2005

Singleton Question

I've been going back and forth trying to decide whether or not to use a Singleton pattern for a specific situation and I've decided I need community input. In my application I have a global "object repository" which is basically just a single point of entry for application-wide services. Some objects are expensive to create and only used in certian situations but what they all have in common is that there only needs to be one instance of each service for my entire application since they are all thread safe (I hope! :P). The object repository itself contains only shared (static) readonly properties used to access the application services and has no instance methods itself. The services need to have an inheritable class hierarchy to support different application's needs.

My question is then, since I only access these objects from the ObjectRepository, do I gain anything by creating the base services as extendable (inheritable) singletons or should I just access them as shared members directly? Now what gets a little trickier is that the application is a server-side application and objects are activated as single-call objects, so depending on the load on the server the application service objects may not be around and may need to be created on the next round if the aspnet worker process is recycled, right? Anyways, I've got the system scaling really well not using the singleton pattern and just accessing the services as shared members of the ObjectRepository, but I'm just curious if using the singleton pattern in this situation will be more performant. Of course, since I need to have the services inheritable, if I go with the singletn pattern I will have to maintain the pattern on all the sub-classes so code maintenance has a weigh-in factor here as well.

So let me show you what I mean. Should I do this (what I'm doing now):
Public Class MyBaseService1

 Public Sub New()
  Initialize()
 End Sub

 Private Sub Initialize()
  'do initialization stuff
 End Sub

 Public Sub TemplateMethod()
  HookMethod1()
  HookMethod2()
 End Sub

 Protected Overridable Sub HookMethod1()
  'do default stuff
 End Sub

 Protected Overridable Sub HookMethod2()
  'do more default stuff
 End Sub
End Class


Public Class MyExtendedService1
 Inherits MyBaseService1

 Public Sub New()
  MyBase.New()
 End Sub

 Protected Overrides Sub HookMethod1()
  'do different stuff
 End Sub

 Protected Overrides Sub HookMethod2()
  'do more different stuff
 End Sub
End Class


Public Class ObjectRepository1

 Private Shared m_service1 As MyExtendedService1
 Private Shared _lock As New Object

 'Use lazy initialization because this 
 ' class is rarely used and expensive to create.
 Public Shared ReadOnly Property Service1() As MyExtendedService1
  Get
   If m_service1 Is Nothing Then
    SyncLock (_lock)
     If m_service1 Is Nothing Then
      m_service1 = New MyExtendedService1
     End If
    End SyncLock
   End If
   Return m_service1
  End Get
 End Property

End Clas
Or would it be better to implement this pattern?
Public Class MyBaseService
 Protected Shared _Instance As MyBaseService
 Protected Shared _Lock As New Object

 Protected Sub New()
  Initialize()
 End Sub

 Public Shared Function GetInstance() As MyBaseService
  If _Instance Is Nothing Then
   SyncLock (_Lock)
    If _Instance Is Nothing Then
     _Instance = New MyBaseService
    End If
   End SyncLock
  End If
  Return _Instance
 End Function

 Private Sub Initialize()
  'do initialization stuff
 End Sub

 Public Sub TemplateMethod()
  HookMethod1()
  HookMethod2()
 End Sub

 Protected Overridable Sub HookMethod1()
  'do default stuff
 End Sub

 Protected Overridable Sub HookMethod2()
  'do more default stuff
 End Sub
End Class


Public Class MyExtendedService
 Inherits MyBaseService

 Public Shared Shadows Function GetInstance() As MyExtendedService
  If _Instance Is Nothing Then
   SyncLock (_Lock)
    If _Instance Is Nothing Then
     _Instance = New MyExtendedService
    End If
   End SyncLock
  End If
  Return DirectCast(_Instance, MyExtendedService)
 End Function

 Protected Overrides Sub HookMethod1()
  'do different stuff
 End Sub

 Protected Overrides Sub HookMethod2()
  'do more different stuff
 End Sub
End Class

Public Class ObjectRepository
 Private Shared m_service1 As MyExtendedService

 'Use lazy initialization because this 
 ' class is rarely used and expensive to create.
 Public Shared ReadOnly Property Service1() As MyExtendedService
  Get
   If m_service1 Is Nothing Then
    m_service1 = MyExtendedService.GetInstance()
   End If
   Return m_service1
  End Get
 End Property

End Class
I mean, I really don't see the advantage of this pattern for my situation but maybe I'm missing something? Thanks in advance!

Monday, March 14, 2005

Can't we all just get along?

Here's a great post from Rick commenting on the absurd .NET language "war" that seems to be going on. It's not really a war, it's just a bunch of Microsoft platform developers bickering and wasting time. This is more ridiculous than the religious war of Java versus .NET. At least the argument with Java is an argument about an entire architecture - OS, Languages, and Tools. Who cares if you wrote your code in C# or VB.NET? It runs on the same damn framework! Like Rick, I come from a VFP background (talk about being neglected) so I'm not really bothered when someone tells me that VB.NET is not a "real" language because I know it is and they are probably just saying that because they don't have a date for Saturday night. Unlike Rick, however, I'm more comfortable with VB.NET because it just seems closer to the Fox programming style I've been used for so long. I think VB-only people are being too sensitive and C#-only people shouldn't knock a language just because it lets you do lose-typing (actually one of the things I like about VB, but that's another post), or isn't "pure" or whatever. Gimme a break. A business isn't going to care what language you program in, they care if the project is done on time, to spec, and works!

Things that make you go "hmmmm...."

I'm not sure if I like this car or not. My friends would either think I'm totally cool or a total weirdo... wait, they already think that.

Thursday, March 10, 2005

GOTCHA: Cancelling a control's validating event in MDI apps

Okay, this is the first time I ever noticed this one and I think it's either a bug or pretty damn retarded. Alright, say you have an MDI application with two child forms call 'em Form1 and Form2, each with a text box on them. You open the forms from your MDI container form like normal (probably from a Main Menu's click event):
Dim frm as New Form1
frm.MdiParent = Me
frm.Show()
Now on Form1, you are handling the TextBox1's validating event and are cancelling the event (probably based on some condition):
Private Sub TextBox1_Validating(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating

   'If some condition blah blah blah
   e.Cancel = True

End Sub
When you run your application, open Form1 and trigger the event, the focus remains in the TextBox1 as expected... until.....

Now you want to open Form2 while Form1 is still open (a perfectly valid use case for an MDI application). BUT, once Form2 opens, even though the form is activated, no control can receive focus on Form2. It basically renders Form2 useless and in a user's eyes it looks like the form is not responding. WHAT!? Why in the world is the Textbox1's Validating event being raised at all on Form1 once Form2 is active? This has got to be a bug or it is just plain retarded. I fail to see why you would ever want this behavior. This is not a problem if the application is not an MDI application. In that case the event will not be raised if the form is not active.

I can't figure out how to prevent the Validating event from being raised, however, I did find a work-around. You can use this check in the handler to avoid running your handler code:
Private Sub TextBox1_Validating(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating

  If Me.MdiParent.ActiveMdiChild Is Me Then
   'If some condition blah blah blah
    e.Cancel = True
  End If

End Sub
This is moderately painful because I have to search my app for "_Validating" and add this line to all the handlers. If anyone's got a better idea I'm all ears.

The VB MVPs are pissed...

I happen to agree with Carl on this one, but I also don't have million line legacy applications to maintain either. If I did I'd probably kill myself. Fact is, all the things I love about programming come directly from learning something new and cool. I prefer to solve my business problems with the best solutions and right now that's VB.NET. Sorry.