Can’t be bothered extending the picturebox control to allow scrolling?…me either! Dump a panel on a form and set it’s ‘AutoScroll’ property to true, now place a picturebox on this panel and set it’s SizeMode to ‘AutoSize’. And there you have it, a scrollable picturebox without writing a single line of code!…just the way I like it! 🙂
The following link explains how to perform check digit calculations for the validation of shipping container codes.
Below is some code based on the calculation steps shown in the above wikipedia page, it validates the check digit, so the complete container number (including check digit) should be entered in the textbox.
Just drop a button and textbox on a form and wire up the event handlers:
public Dictionary<char, int> AlphabetCodes = new Dictionary<char, int>();
public List<int> PowerOfMultipliers = new List<int>();
private void Form1_Load(object sender, EventArgs e)
int step = 10;
//...create a dictionary entry for all letters of the alphabet using their Ascii value to identify them.
//if you subtract their ascii value by the value of the first alpha ascii character (in this case 65 for
//uppercase 'A'), it will give you it's position in the alphabet, Add 10 to this and skip over all multiples
//of 11 to give you ISO Owner Code numbers for each letter.
for (int i = 65; i < 91; i++)
char c = (char)i;
int pos = i - 65 + step;
if (c == 'A' || c == 'K' || c == 'U') //omit multiples of 11.
step += 1;
AlphabetCodes.Add(c, pos); //add to dictionary
//create a list of 10, 2^x numbers for calculation. List should contain 1, 2, 4, 8, 16, 32, 64 etc..
for (int i = 0; i < 10; i++)
int result = (int)Math.Pow(2, i); //power of 2 calculation.
PowerOfMultipliers.Add(result); //add to list.
private void button1_Click(object sender, EventArgs e)
int total = 0;
if (textBox1.Text.Length == 11) //container numbers must be 11 characters long.
for (int i = 0; i < 10; i++) //loop through the first 10 characters (the 11th is the check digit!).
if (AlphabetCodes.ContainsKey(textBox1.Text[i])) //if the current character is in the dictionary.
total += (AlphabetCodes[textBox1.Text[i]] * PowerOfMultipliers[i]); //add it's value to the total.
int serialNumber = (int)textBox1.Text[i] - 48; //it must be a number, so get the number from the char ascii value.
total += (serialNumber * PowerOfMultipliers[i]); //and add it to the total.
int checkDigit = (int)total % 11; //this should give you the check digit
//The check digit shouldn't equal 10 according to ISO best practice - BUT there are containers out there that do, so we'll
//double check and set the check digit to 0...again according to ISO best practice.
if (checkDigit == 10)
checkDigit = 0;
if (checkDigit != (int)textBox1.Text - 48) //check digit should equal the last character in the textbox.
MessageBox.Show("Container Number NOT Valid");
MessageBox.Show("Container Number Valid");
MessageBox.Show("Container Number must be 11 characters in length");
You would be forgiven for thinking that clicking on the ‘X’ in the top right of your form would close it, as per all standard Windows applications…but be warned this is NOT the case with Pocket PC applications! I’m not sure what the wisdom is behind this decision, I guess it doesn’t really matter, I just wish it were more obvious!
Anyway, clicking the cross does nothing more than minimise your form, in order to fully close it, you must set it’s ‘MinimizeBox’ property to false. This will in turn display an ‘ok’ button instead of a ‘X’ at the top of your form, and it is clicking this ‘ok’ button that fires the form closing events!…obvious when you think about it!…hmmmmm.
So there’s an hour of my life I’ll never get back! Thanks Microsoft.
A useful article demonstrating how to share code between mobile and desktop applications.
An awesome printer helper class for sending raw data to a printer. I have found this very useful for sending control codes to our Datamax label printers at work for barcode printing etc.
I added an additional method to the helper class (bodged together from the existing ones!) that accepts a memory stream instead of a file or string.
public static bool SendStreamToPrinter(string szPrinterName, MemoryStream ms, string DocName)
// Open the file.
//FileStream fs = new FileStream(szFileName, FileMode.Open);
// Create a BinaryReader on the file.
BinaryReader br = new BinaryReader(ms);
// Dim an array of bytes big enough to hold the file's contents.
Byte bytes = new Byte[ms.Length];
bool bSuccess = false;
// Your unmanaged pointer.
IntPtr pUnmanagedBytes = new IntPtr(0);
nLength = Convert.ToInt32(ms.Length);
// Read the contents of the file into the array.
bytes = br.ReadBytes(nLength);
// Allocate some unmanaged memory for those bytes.
pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
// Copy the managed byte array into the unmanaged array.
Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);
// Send the unmanaged bytes to the printer.
bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength, DocName);
// Free the unmanaged memory that you allocated earlier.
I have recently been playing around with SharpZipLib, an open source compression library for C#.
Below is some code demonstrating how to recursively zip folders and subfolders. If an exception is thrown (i.e if a file is open when you try and zip it), it copies the file to a temp directory and zips it from there.
public static void ZipFolder(string Root, string CurrentFolder, ZipOutputStream ZipStream)
string SubFolders = Directory.GetDirectories(CurrentFolder);
foreach (string Folder in SubFolders)
ZipFolder(Root, Folder, ZipStream);
string path = CurrentFolder.Substring(Root.Length) + "/";
if (path.Length > 1)
zEntry = new ZipEntry(path);
zEntry.DateTime = DateTime.Now;
foreach (string file in Directory.GetFiles(CurrentFolder))
ZipFile(ZipStream, path, file);
private static void ZipFile(ZipOutputStream ZipStream, string path, string file)
byte buffer = new byte;
string filePath = (path.Length > 1 ? path : string.Empty) + Path.GetFileName(file);
ZipEntry zEntry = new ZipEntry(filePath);
zEntry.DateTime = DateTime.Now;
using (FileStream fs = File.OpenRead(file))
sourceBytes = fs.Read(buffer, 0, buffer.Length);
ZipStream.Write(buffer, 0, sourceBytes);
while (sourceBytes > 0);
File.Copy(file, @"C:\TEMP\" + Path.GetFileName(file), true);
ZipFile(ZipStream, path, @"C:\TEMP\" + Path.GetFileName(file));
.NET allows you to build colours from their ARGB values, so it is possible to use this value to save and retrieve a colour to/from a database as an integer. The code snippet below uses the ToARGB and FromARGB methods to demonstrate this.
int myInt = colorDialog1.Color.ToArgb();
Color myColor = Color.FromArgb(myInt);
pictureBox1.Backcolor = myColor;
This is pretty straight forward, in the sample code below it shows how to add a TreeView node with an image into a ListBox. You’ll need an ImageList loaded with images to link both the TreeView and ListBox to and set the ListView to a view that allows images, i.e ‘Tile’.
private TreeNode sourceNode;
private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
sourceNode = (TreeNode)e.Item;
if (sourceNode.Parent != null)
private void DragEnter_Event(object sender, DragEventArgs e)
if (e.Data.GetData(typeof(TreeNode)) != null)
e.Effect = DragDropEffects.Move;
e.Effect = DragDropEffects.None;
private void listView1_DragDrop(object sender, DragEventArgs e)
Just add the DragEnter event to the _DragEnter event of the controls you want to allow the DragDropEffects.Move to show on.
A quick snippet to show how to minimize an application to the Windows System Tray using a NotifyIcon. Set the NotifyIcon text to be whatever you want displayed when you hover over it in the System Tray and be sure to set the Icon property too, else nothing will get displayed! Then add the following in the form_resize event:
private void wfMain_Resize(object sender, EventArgs e)
if (FormWindowState.Minimized == WindowState)
And to restore it to it’s former state. simply add the following in the DoubleClick event of your NotifyIcon:
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
WindowState = FormWindowState.Normal;