Sunday, January 13, 2008

.NET interview questions - Windows Forms

  1. I am constantly writing the drawing procedures with System.Drawing.Graphics, but having to use the try and dispose blocks is too time-consuming with Graphics objects. Can I automate this? Yes, the code
    System.Drawing.Graphics canvas = new System.Drawing.Graphics();
    try
    {
    //some code
    }
    finally
    canvas.Dispose();

    is functionally equivalent to

    using (System.Drawing.Graphics canvas = new System.Drawing.Graphics())
    {
    //some code
    } //canvas.Dispose() gets called automatically

  2. How do you trigger the Paint event in System.Drawing?Invalidate the current form, the OS will take care of repainting. The Update method forces the repaint.
  3. With these events, why wouldn’t Microsoft combine Invalidate and Paint, so that you wouldn’t have to tell it to repaint, and then to force it to repaint? Painting is the slowest thing the OS does, so usually telling it to repaint, but not forcing it allows for the process to take place in the background.
  4. How can you assign an RGB color to a System.Drawing.Color object? Call the static method FromArgb of this class and pass it the RGB values.
  5. What class does Icon derive from? Isn’t it just a Bitmap with a wrapper name around it? No, Icon lives in System.Drawing namespace. It’s not a Bitmap by default, and is treated separately by .NET. However, you can use ToBitmap method to get a valid Bitmap object from a valid Icon object.
  6. Before in my VB app I would just load the icons from DLL. How can I load the icons provided by .NET dynamically? By using System.Drawing.SystemIcons class, for example System.Drawing.SystemIcons.Warning produces an Icon with a warning sign in it.
  7. When displaying fonts, what’s the difference between pixels, points and ems? A pixel is the lowest-resolution dot the computer monitor supports. Its size depends on user’s settings and monitor size. A point is always 1/72 of an inch. An em is the number of pixels that it takes to display the letter M.

0 Comments: