Methods that affect a Windows Forms control can be executed only on the thread that created the control. .NET does not permit directly manipulating controls across threads. The Visual Studio compiler under .NET 2.0 will mark these attempts as errors. .NET 1.1 will allow them, but these will often result in unexpected behavior like incorrectly-painted controls.
It’s important to note that an enumerator does not have exclusive, thread-safe access to its collection. Even when a collection is synchronized, other threads can still modify the collection. Therefore, a collection’s contents can change while enumerating through it, which will cause the enumerator to throw an exception. So there are three key ways to safely enumerate a collection:
Multi-threaded code is challenging to get right and even harder to debug once it’s gone wrong. This is especially true when attempting to collect data from multiple threads. To make this easier, many .NET collection classes include the SyncRoot property to maintain proper synchronization with other threads that might be simultaneously modifying the collection. But then Microsoft changed its mind in .NET 2.0 and decided to let the developer decide how to manage synchronization, and so none of the new generic collections have a SyncRoot property.
NotifyIcon is .NET’s version of the system tray icon, those little icons that appear next to the clock in the Windows Start bar. .NET 2.0 added the ability to display a pop-up balloon tip pointing at a tray icon. However, this capability doesn’t always work as you would expect.
Handling keyboard shortcuts in your application can sometimes get a bit tricky. Consider the standard Ctrl+C shortcut, which every application should support as a “Copy” to clipboard command. When you users type in textboxes in your application’s form, they will expect that Ctrl+C will copy the selected text. But this feature is not supported by default; you have to explicitly write some code.
The DataGridView is a terrific control built into .NET that provides a customizable table for entering and displaying data. If you provide the DataGridView in your software as a means for the user to enter multiple rows of data, you may wish to redefine the default behavior of the Enter key. Read the rest of this entry »
You probably knew that you can use the String.Trim method to remove whitespace from the start and end of a C# string. Unfortunately, the Trim method does not remove whitespace from the middle of a string.
Of course you know that XML denotes element names with greater-than and less-than symbols, such as:
<name>value</name>
Therefore, to avoid confusing the XML parser, the greater/less symbols (and the ampersand, an HTML special character) must be encoded.
C# object equality is one of those topics that seems easy on the surface but can get a little complicated when you dig into it.