This is a small collection of code samples, i wrote for my personal use. Later i decided to make it public, like my other pages for Delphi and PHP code. The functions are all free and tested and most of them are written by myself.
If you found something useful, i'm happy to hear about it. Also suggestions to the functions are very welcome, don't hesitate to send me an email to .
Did you already write a small useful function for your library, but then it became difficult to share it, because you needed to deliver translated text resources? Maybe you can borrow the textes from the Windows environment, at least some of the most common textes. To find out which text resources are available, just open the windows library in VisualStudio and switch to the String-Table node.
using System; using System.Runtime.InteropServices; using System.Text; /// <summary> /// Searches for a text resource in a Windows library. Sometimes, using the /// existing Windows resources, you can make your code language independent /// and you don't have to care about translation problems. /// </summary> public class Sto_WindowsString { /// <summary> /// Searches for a text resource in a Windows library. /// </summary> /// <example> /// btnCancel.Text = Sto_WindowsString.Load("user32.dll", 801, "Cancel"); /// btnYes.Text = Sto_WindowsString.Load("user32.dll", 805, "Yes"); /// </example> /// <param name="libraryName">Name of the windows library like "user32.dll" /// or "shell32.dll"</param> /// <param name="ident">Id of the string resource.</param> /// <param name="defaultText">Return this text, if the resource string could /// not be found.</param> /// <returns>Requested string if the resource was found, /// otherwise the <paramref name="defaultText"/></returns> public static string Load(string libraryName, uint ident, string defaultText) { IntPtr libraryHandle = GetModuleHandle(libraryName); if (libraryHandle != IntPtr.Zero) { StringBuilder sb = new StringBuilder(1024); int size = LoadString(libraryHandle, ident, sb, 1024); if (size > 0) return sb.ToString(); } return defaultText; } [DllImport("kernel32.dll", CharSet = CharSet.Auto)] private static extern IntPtr GetModuleHandle(string lpModuleName); [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern int LoadString(IntPtr hInstance, uint uID, StringBuilder lpBuffer, int nBufferMax); }
If you write XML files with indents and wish to validate them against an XSD schema, sometimes you get invalid XML. By default, the XmlDocument writes all tags with an opening and a closing tag. This can result in errors, when the closing tag is written on a new line and therefore is not empty anymore.
<element attribute1="hello" attribute2="world"> </element>
To get valid tags, you can write short tags instead.
<element attribute1="hello" attribute2="world" />
And here comes the code you need to convince the XmlDocument to write short tags:
/// <summary> /// Prepares the XML tree for writing short format tags, instead of opening and /// closing tags. This leads to shorter and particularly to valid XML files. /// </summary> public static void Sto_XmlShortTags(XmlNode node) { // call the function recursive for all children if (node.ChildNodes.Count > 0) { foreach (XmlNode childNode in node.ChildNodes) Sto_XmlShortTags(childNode); } // if the node has no children, use the short format else { if (node is XmlElement) ((XmlElement)node).IsEmpty = true; } }
When you call the sort method of a list or of an array, you will finally use the Array.Sort() function, which implements a fast Quicksort. This can be very handy, but especially when you have to sort a list of objects with multiple attributes (like database records), there are some drawbacks. Because of these drawbacks, and because i couldn't find code for a parallel and stable sorting, i implemented a Mergesort, with following advantages over the built-in sort function:
Implementation of a parallel and stable Mergesort
The standard TreeView component can be used as a virtual tree. You don't need to initialize the tree with all data, instead you can populate the childnodes when the user expands a node. Unfortunately, the TreeView does not display the plus icon on a node without childnodes and therefore the user cannot expand the TreeNode. This is a way to display the plus icon anyway:
using System; using System.Runtime.InteropServices; using System.Windows.Forms; /// <summary> /// Makes the plus icon of a treeview node visible, even if the node doesn't /// has child nodes. This allows to populate a tree on demand. /// </summary> /// <param name="node">Set plus icon for this node</param> public static void Sto_ShowTreeviewPlusIcon(TreeNode node) { const int TVIF_CHILDREN = 0x40; const int TVM_SETITEM = 0x110d; // prevent hiding the icon, when child nodes exist if ((!show) && (node.Nodes.Count > 0)) return; TV_ITEM lParam = new TV_ITEM(); lParam.mask = TVIF_CHILDREN; lParam.hItem = node.Handle; lParam.cChildren = Convert.ToInt32(true); SendMessage( new HandleRef(node.TreeView, node.TreeView.Handle), TVM_SETITEM, 0, ref lParam); } [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern IntPtr SendMessage( HandleRef hWnd, int msg, int wParam, ref TV_ITEM lParam); [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] private struct TV_ITEM { public int mask; public IntPtr hItem; public int state; public int stateMask; public IntPtr pszText; public int cchTextMax; public int iImage; public int iSelectedImage; public int cChildren; public IntPtr lParam; }