Showing posts with label .Net Basics. Show all posts
Showing posts with label .Net Basics. Show all posts

Wednesday, October 3, 2012

Change Cursor to Wait Cursor



This example demonstrates how to show Wait Cursor in C# for any lengthy operation.

Mouse cursor displayed over any control in windows forms application is determined by Control.Cursor property. If you want to change the mouse cursor at application level use static property Current of Cursor class. To show hourglass cursor assign value Cursors.WaitCur­sor. To return back the default behavior (to display control specific cursor) assign back value Cursors.Default to the Cursor.Current property. See example below.


Cursor.Current = Cursors.WaitCursor;
 // Your operations!  
Cursor.Current = Cursors.Default;

How does it work at windows level:

Windows sends the window that contains the mouse cursor the WM_SETCURSOR message, giving it an opportunity to change the cursor shape. A control like TextBox takes advantage of that, changing the cursor into a I-bar. The Control.Cursor property determines what shape will be used.

The Current.Cursor property changes the shape directly, without waiting for a WM_SETCURSOR response. In most cases, that shape is unlikely to survive for long. As soon as the user moves the mouse, WM_SETCURSOR changes it back to Control.Cursor.

The UseWaitCursor property was added in .NET 2.0 to make it easier to display an hourglass. Unfortunately, it doesn't work very well. It requires a WM_SETCURSOR message to change the shape and that won't happen when you set the property to true and then do something that takes a while.





Wednesday, September 5, 2012

Why can't create object of an abstract class?



An abstract type is defined largely as one that can't be created. You can create subtypes of it, but not of that type itself. The CLI will not let you do this.

An abstract class has a protected constructor (by default) allowing derived types to initialize it.

Further, if we go with object creation process by CLR, an object of class can only be created if its FULL memory requirement is known as compilation time. considering anstract class is a "Template" or "Incomplete" class, compiler is not able to get its full memory requirement hence you can't proceed.

Abstract classes have the following features:

  • An abstract class cannot be instantiated.
  • An abstract class may contain abstract methods and accessors.
  • It is not possible to modify an abstract class with the sealed modifier, which means that the class cannot be inherited.
  • A non-abstract class derived from an abstract class must include actual implementations of all inherited abstract methods and accessors.

Why struct can not have parameterless constructor


Although the CLR allows it, C# does not allow structs to have a default parameterless constructor. The reason is that, for a value type, compilers by default neither generate a default constructor, nor do they generate a call to the default constructor. So, even if you happened to define a default constructor, it will not be called and that will only confuse you. To avoid such problems, the C# compiler disallows definition of a default constructor by the user. And because it doesn't generate a default constructor, you can't initialize fields when defining them.


For instance, an array of value types will be initialized to the initial values of its members—i.e., zero for number type primitive members, null for reference types, and so forth—and not to the values provided in a default constructor. This feature makes structs perform better; because, constructor code need not be called.
So, requiring that a constructor contain a minimum of one parameter reduces the possibility that a constructor will be defined which is expected to be called every time the struct type is built.

For more information about Struct: Please visit http://msdn.microsoft.com/en-us/library/ah19swz4(v=vs.71).aspx