Paging, sorting and filtering are common features in websites. Microsoft has written a tutorial how to implement these features in ASP.NET Core MVC with Entity Framework Core. The described solution works but I found it a bit too primitive. It triggered me to create a more powerful solution. Before you can start using my solution you should first read this tutorial, it explains how you can add Entity Framework Core to an ASP.NET Core MVC application.
I have just received mail from Pearson VUE informing me that have passed the Beta Exam 70-528 TS: Microsoft .NET Framework 2.0 - Web-Based Client Development (called 71-528 while it's in Beta). I also received a free Voucher for a next exam, thanks guys.
On Friday I will try the Beta Exam 70-551 UPGRADE: MCAD Skills to MCPD Web Developer by Using the Microsoft .NET Framework. One week later I will try the Beta Exam70-552 UPGRADE: MCAD Skills to MCPD Windows Developer by Using the Microsoft .NET Framework . Wish me luck.
For a long time I thought that I only had to set the Cursor.Current to a WaitCursor before a long running operation, the .NET runtime would reset it back to the Default cursor. Turns out that this is only true when the mouse is moved. Bummer.
Cursor.Current = Cursors.WaitCursor;
The solution for this problem is very easy. I created a helper class called WaitCursor which set the Cursor.Current and restores it to the original value when it it disposed.
public
sealed
class WaitCursor : IDisposable {        
 private Cursor _prev;
 public WaitCursor(){
   _prev= Cursor.Current;
   Cursor.Current = Cursors.WaitCursor;
 }
 publicvoid Dispose(){
   Cursor.Current =_prev;
 }
}
I create the instance of the WairCursor class inside a using statement. This will automatically call the Dispose() method when it goes out of scope.
There is a difference in disposing Modal and Non-Modal forms. In a training I gave last week I noticed that even experienced developers didn't know that Modal Dialogs don't dispose automatically, Non-Modal do.
private
void
buttonShowNonModalForm
(
object
sender
, System.EventArgs e){
 new TestForm().Show();
}
privatevoidbuttonShowModalDialog(objectsender, System.EventArgs e){
 new Form2().ShowDialog();
}
The solution to this problem is very simple by creating the Form instance within a using block. This will dispose the Form when it is closed.
private
void
buttonShowModalDialog
(
object
sender
, System.EventArgs e){
 using(TestForm f =new TestForm()){
   f.ShowDialog();
 }
}
An alternative solution uses a try/finnaly. Personally I prefer the previous, it is easier to read and write.
I have written an article two years ago with the title 'WinForm DualList Component'. In this article I mentioned the desire to extend the DualList component with Drag & Drop support. Finally it's done. Not by extending the original DualList component but by creating an new DualListDragDrop component (reason: cohesion).
Programming Drag & Drop (D&D) between listboxes is quite difficult. It requires at least 60 lines of code for every two listboxes. Therefore not many application support D&D. This component eliminates need of writing the code. You only have to set properties on it. Making it easy to support D&D in your applications.
I have found a solution for a problem which I had for a long time. I was unable to set the ToolboxBitmap attribute for a component using the (type, string) constructor. I found the solution for this problem (using an internal ResFinder class) on www.bobpowell.net. This site has also some other Windows Forms Tips and Tricks. Great!
This table gives you an overview of all collections in the System.Collections and System.Collections.Specialized namespaces.
Class |
Key |
Value |
Remarks |
System.Array |
|
? |
Predefined size, used for passing data around. |
ArrayList |
|
Object |
Easy to work with, provides basic collection functionality and can easily be
converted to an Array. |
BitArray |
|
Boolean |
Manages a compact array of bit values, which are represented
as Booleans
|
CollectionBase |
|
? |
Abstract base for building your own collection classes. |
DictionaryBase |
Object |
? |
Abstract base for building your own collection classes using a key-value pair. |
HashTable |
Object |
Object |
Provides high performance access to items in the key-value pair collection, by
a specific key. |
Queue |
|
Object |
Implements the FIFO mechanism. |
ReadOnlyCollectionBase |
|
Object |
Abstract base for building your own read-only collection classes. |
SortedList |
Object (sorted) |
Object |
Provides access to items in the collection by a specific key or an index
(GetByIndex(int)). |
Stack |
|
Object |
Implements the LIFO mechanism. |
Specialized.HybridDictionary
|
Object |
Object |
Uses internally a ListDictionary class when the collection is small, and
switches automatically to a Hashtable class when the collection gets large. |
Specialized.ListDictionary |
Object |
Object |
Is designed to be used for collections with 10 or less items, while the
Hashtable is designed to contain more items. |
Specialized.NameObjectCollectionBase |
String |
? |
Abstract base for building your own collection classes using a key-value pair. |
Specialized.NameValueCollection |
String (sorted) |
String |
NameValueCollection class is the strong type equivalent of the SortedList
class, for the String class. |
Specialized.StringCollection |
|
Object |
Strongly typed ArrayList for the String class. |
Specialized.StringDictionary |
String |
String |
Strongly typed Hashtable for the String class, not sorted. |
Read also Jan Tielens article on the MSDN Belux website.
I have used the EventsHelper class to fire events asynchronical. I came across this class in the TechEd 2003 presentation C# Best Practices from Eric Gunnerson and Juval Löwy. Today I noticed a bug. The FireAsync() method uses the 'Fire and Forget' pattern incorrectly.
The 'Fire and Forget' pattern is used when the return value and returned parameters of the call are not required, and when there is no need to synchronize with the asynchronously executing method. In this case, the caller simply needs to call BeginInvoke, passing any normal and ref parameters, and null for the callback and asyncState.
This has been a commonly used pattern. However, there has been a documentation change in version 1.1 of the .NET Framework that has big ramifications for this technique of making async calls. The documentation now states that EndInvoke must be called for a corresponding BeginInvoke--otherwise Microsoft say they may now, or in the future, leak resources. It appears that no resources are leaked under version 1.0 of the framework; however, with this type of warning in place, it is recommended that a call to EndInvoke be made even if the return values of an async call are not required.
It is relatively straightforward to create a helper class that handles this for you--one example I found here.
public
class AsyncHelper {
 delegatevoid DynamicInvokeShimProc(Delegate d,object[]args);
 static DynamicInvokeShimProc dynamicInvokeShim=new
   DynamicInvokeShimProc(DynamicInvokeShim);
 static AsyncCallback dynamicInvokeDone=new
   AsyncCallback(DynamicInvokeDone);
 publicstaticvoid FireAndForget(Delegate d,paramsobject[]args){
   dynamicInvokeShim.BeginInvoke(d,args,dynamicInvokeDone,null);
 }
 staticvoid DynamicInvokeShim(Delegate d,object[]args){
   d.DynamicInvoke(args);
 }
 staticvoid DynamicInvokeDone(IAsyncResult ar){
   dynamicInvokeShim.EndInvoke(ar);
 }
}
I have written the following DateDiff() function in C#. VB.NET users already had it using the Micrsoft.VisualBasic.dll assembly. Now you can use it without referencing this 'ugly' extra assembly.
using System;
namespace ReflectionIT.System {
 publicenum DateInterval {
   Year,
   Month,
   Weekday,
   Day,
   Hour,
   Minute,
   Second
 }
 publicclass DateTimeUtil {
   publicstaticlong DateDiff(DateInterval interval, DateTime date1, DateTime date2){
     TimeSpan ts=date2-date1;
     switch(interval){
       case DateInterval.Year:
         returndate2.Year -date1.Year;
       case DateInterval.Month:
         return(date2.Month -date1.Month)+(12*(date2.Year -date1.Year));
       case DateInterval.Weekday:
         return Fix(ts.TotalDays)/7;
       case DateInterval.Day:
         return Fix(ts.TotalDays);
       case DateInterval.Hour:
         return Fix(ts.TotalHours);
       case DateInterval.Minute:
         return Fix(ts.TotalMinutes);
       default:
         return Fix(ts.TotalSeconds);
     }
   }
   privatestaticlong Fix(double Number){
     if(Number >=0){
       return(long)Math.Floor(Number);
     }
     return(long)Math.Ceiling(Number);
   }
 }
}