c#
Archived Posts from this Category
Archived Posts from this Category
comments off Friday 17 Aug 2007 | Guy Mahieu | .net(t) , c#(t) , msbuild(t) , regex(t)
I’ve had to create md5 hashes in php before, which can be done by simply using the md5() function. Because I always try to avoid reinventing the wheel, I googled for an example on how to compute these kinds of hashes from c#. It directed me to the Microsoft knowledgebase, article KB307020, but that didn’t work as expected.
That particular article shows how you can compute the MD5 hash of a string and then display the resulting byte array as a new string containing the hexadeximal representation of each byte. Since I needed to compare the hash to a string that was generated by a script on a linux machine, I was especially interested in the ByteArrayToString method from the knowledge base article. I have pasted it below, see if you can spot the error:
static string ByteArrayToString(byte[] arrInput) { int i; StringBuilder sOutput = new StringBuilder(arrInput.Length); for (i=0;i < arrInput.Length -1; i++) { sOutput.Append(arrInput[i].ToString("X2")); } return sOutput.ToString(); }
0 comments Friday 09 Mar 2007 | Guy Mahieu | c#(t)
Some Windows Forms controls come with copy/paste functionality out of the box. This is great when you just want the standard copy/paste behaviour, but what if you want to do some transformation of the data on the clipboard prior to displaying it in the Windows Forms control?
In my case I needed to convert a selection copied from Excel to a comma-delimited list of values when they were pasted in a TextBox. Since there is no OnPaste() method on the TextBox to override, I had to dig deeper to get the job done.
Usually something like this can be done by creating a subclass of the component and overriding the WndProc method to capture the right message. So I created a class called MyTextBox which extends TextBox and I trapped the windows Paste message, like this:
private const int WM_PASTE = 0x302; protected override void WndProc(ref Message m) { if (m.Msg == WM_PASTE) { // trigger our custom paste logic OnPaste(); } else { base.WndProc(ref m); } } protected virtual void OnPaste() { // put custom logic here }
There you go, now my own protected virtual OnPaste() method is called when the control receives a PASTE message, and the message is not passed to the base class to prevent the default paste behavior from being executed.
All that is left to do now is get the text data from the clipboard, do my own transformations and change the Text property of the TextBox accordingly.
3 comments Saturday 03 Mar 2007 | Guy Mahieu | .net(t) , c#(t) , tips&tricks(t) , winforms(t)