Search Site:








Edit - To Do - AllRecentChanges

Recent Changes Printable View Page History Edit Page

C# Snippets

Listing the members of an Enum in a combox

comboBox.SelectedIndex = 0;

Converting a string to an enum

For the .Net Framework:

MyEnum myEnum = (MyEnum)Enum.Parse(typeof(MyEnum),str);

For the Compact Framework:

MyEnum myEnum =
System.ComponentModel.TypeDescriptor.GetConverter(typeof(MyEnum)).ConvertFromString(str) as MyEnum;

Converting a StringCollection to an array of string

string[] arr = (string[])new ArrayList(stringcollection).ToArray(typeof(string));

Simply loading and iterating through a portion of an XML document

Loading the file.xml and iterating through the child nodes of the tag <Consumable InternalCode="21">

using System;
using System.Xml;
using System.Xml.XPath;

public static void RunXPath() {
  XmlDocument xmlDoc = new XmlDocument();
  XmlNodeList nodes = xmlDoc.SelectNodes("//Consumable[@InternalCode="21"]/*");
  foreach(XmlNode node in nodes) {
    // Use node.Name and node.InnerText to access node name and value

Getting the real hard margins of a printer

The .Net framework has no facility to get the real page limits imposed by the hardware of a printer. Here is one way to correct that issue:

using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Runtime.InteropServices;

namespace MyApplication {
 public class PrinterMargings {
  // From WinGDI.h
  private static extern Int32 GetDeviceCaps(IntPtr hdc, Int32 capindex);
  private const int HORZRES = 8;
  private const int VERTRES = 10;
  private const int LOGPIXELSX = 88;
  private const int LOGPIXELSY = 90;
  private const int PHYSICALWIDTH = 110;
  private const int PHYSICALHEIGHT = 111;
  private const int PHYSICALOFFSETX = 112;
  private const int PHYSICALOFFSETY = 113;

  public Margins GetHardMargins(PrinterSettings ps, bool isLandscape) {
   Margins m;
   Graphics e = ps.CreateMeasurementGraphics();
   float dpiX = e.DpiX;
   float dpiY = e.DpiY;
   int offsetx, offsety;
   int horzres, vertres;
   int physicalwidth, physicalheight;

   IntPtr hDC = e.GetHdc();
   offsetx = GetDeviceCaps(hDC , PHYSICALOFFSETX);
   offsety = GetDeviceCaps(hDC , PHYSICALOFFSETY);
   horzres = GetDeviceCaps(hDC , HORZRES);
   vertres = GetDeviceCaps(hDC , VERTRES);
   physicalwidth = GetDeviceCaps(hDC , PHYSICALWIDTH);
   physicalheight= GetDeviceCaps(hDC , PHYSICALHEIGHT);

   int left   = (int)Math.Ceiling((float)offsetx * 100.0F / dpiX);
   int top    = (int)Math.Ceiling((float)offsety * 100.0F / dpiY);
   int right  = (int)Math.Ceiling((float)
                            (physicalwidth  - horzres - offsetx) * 100.0F / dpiY);
   int bottom = (int)Math.Ceiling((float)
                            (physicalheight - vertres - offsety) * 100.0F / dpiY);

   if (isLandscape && ps.LandscapeAngle == 90) {
      m = new System.Drawing.Printing.Margins(bottom, top, left, right);
   } else if (isLandscape && ps.LandscapeAngle == 270) {
      m = new System.Drawing.Printing.Margins(top, bottom, right, left);
   } else {
      m = new System.Drawing.Printing.Margins(left, right, top, bottom);
   return m;

Notice that the margins are obtained from GetDeviceCaps() for a portrait orientation and have been adjusted to the actual page orientation to be meaningful.

Embedding Images resources in a VS project.

Instead of loading common images used in an application from the filesystem, it's usually better to have them sealed into the application itself and use them as embedded resources.

In Visual Studio, simply add the images to your project using the Solution Explorer: right-click on your project then choose Add->Add Existing Items... to add your bitmap images:

Next declare that image as Embedded Resources so it is compiled into the resulting application appropriately.

Now to use the images from your code, just instanciate them using this syntax:

Image ImageOK = new Bitmap (GetType(), "OK.png");
Image ImageNotOK = new Bitmap (GetType(), "NotOK.png");

Solving the Image transparency issue in ListView

I had an issue with the ListView in .Net: images with transparency like PNG images with an alpha-channel would display the content of the transparent channel as black:

After searching for a while, it appears that the issue comes from the ImageLists used in .Net and the fact that on Windows XP, they do not work properly with 32bpp alpha-channel images.

The solution is to use VisualStyles in your application to force it to use Windows XP theming. This will also enable your application to use Windows XP theming, albeit with some limits as not all controls seem to support it.
In any case, it fixes our issue with the ListView:

Just add, before Application.Run(...) in your Main Form:

MeTuesday 18 July 2006, at 15:41 GMT+8 [X]
How can I set margins to 0?...I'm using new margins(0,0,0,0) but I still has some margins as before....
Enter your comment (no links allowed): Author:

Edit Page - Page History - Printable View - Recent Changes - WikiHelp - Search - RSS -
Page last modified on Wednesday 23 November 2005, at 12:00 GMT+8 - Viewed 3004 times