C Sharp Snippets
C# Snippets
- Listing the members of an Enum in a combox
- Converting a string to an enum
- Converting a StringCollection to an array of string
- Simply loading and iterating through a portion of an XML document
- Getting the real hard margins of a printer
- Embedding Images resources in a VS project.
- Solving the Image transparency issue in ListView.
Listing the members of an Enum in a combox
- Tutorial on Enums, C# Station (registration req.)
- OpenNetCF tutorial on enums on the Compact Framework
Converting a string to an enum
For the .Net Framework:
For the Compact Framework:
System.ComponentModel.TypeDescriptor.GetConverter(typeof(MyEnum)).ConvertFromString(str) as MyEnum;
Converting a StringCollection to an array of 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.Xml;
using System.Xml.XPath;
public static void RunXPath() {
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"file.xml");
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.Drawing;
using System.Drawing.Printing;
using System.Runtime.InteropServices;
namespace MyApplication {
public class PrinterMargings {
// From WinGDI.h
[DllImport("gdi32.dll")]
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);
e.ReleaseHdc(hDC);
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.
- CodeProject article: Getting the printer's REAL margin bounds in .NET
- Microsoft article KB122037 HOWTO: How to Use a Program to Calculate Print Margins
- GetDeviceCaps() definition from MSDN.
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:
- Using Resources in Visual Studio .NET (in particular the addendum)
- Embedding and Using Resources in C#, on the DevHood website
- Retrieving an Image that is an Embedded Resource on MSDN
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:
Application.DoEvents();
- Application.EnableVisualStyles from the blog of Raghavendra Prabhu, of the .Net development Team
- Strange problems when Application.EnableVisualStyles is used
How can I set margins to 0?...I'm using new margins(0,0,0,0) but I still has some margins as before....