ASP.NET can render in CSS (if you tell it how)

The ASP.NET team have finally released some control adapters for the standard .NET controls to use CSS.
This means we can now use the built in ASP.NET controls for menus and such things with worrying about issues rendering on PDA’s or Text Only browsers or for screen readers. I think its a shame this wasnt how the controls were built to begin with but this is better than nothing.
 
I advise all people using ASP.NET, especially for public websites to use these extenders to help make their sites CSS compliant and accessible. There is no good reason not to anymore.
 
I have web access on my phone and I know it annoys me when I go to sites like national rail and have to spend about (150KB of credit) downloading a simple 3 textbox form.  Using good markup can help cure this pain.
 
At least take a look
http://www.asp.net/CSSAdapters - This is the main page
 
http://www.asp.net/CSSAdapters/Menu.aspx - This is the menu example
 
Go forth and be accessible.

Generic Enum Parsing and bit changing.

I was very frustrated the other day when I wanted to write a couple of generic functions in C# which take an enum generic parameter and turn flags on or off and check if there on etc. Just to tidy up lots of bitwise checking in my code.
Imagine my dissapointment when it got :
error CS0702: Constraint cannot be special class 'System.Enum'

This was quite iritating as I could seem to find out why. What I did find out though was that C++ for CLR did allow this, so I pulled out VC++ 2005 and had a go at doing what I wanted. I am no great C++ programmer so the thing that turned this from about 2 minutes work to 30 minutes was that I had declared the type and the methods without a public: block so I couldnt seem them in C#.

 
All thats fine now so I have got a class with some generics methods for making enum work simpler. I have kept the C++ for this as there are probably going to be other uses, especially with arrays (which get the same error). Heres the code for a C++ class library targeted to the CLR:
namespace BasicHelpers{

	public ref class EnumHelper  sealed : System::Object
	{
		public:
			generic  where T : System::Enum
			static T Parse(System::String ^valueName)
			{
				return Parse(valueName, false);
			}

			generic  where T : System::Enum
			static T Parse(System::String ^valueName, bool ignoreCase)
			{
				return safe_cast(System::Enum::Parse(T::typeid, valueName, ignoreCase));
			}

			generic	 where T : System::Enum
			static System::Boolean IsFlagged(T value, T flag)
			{
				System::Int32 i1 = safe_cast(value );
				System::Int32 i2 = safe_cast(flag );
				return safe_cast((i1 & i2) == i2);
			}

			generic  where T : System::Enum
			static T Flag(T value, T flag, System::Boolean onOrOff)
			{
				System::Int32 i1 = safe_cast(value );
				System::Int32 i2 = safe_cast(flag );
				System::String ^name = System::Enum::GetName(T::typeid,(onOrOff ? (i1 | i2) : (i1 & ~i2)));
				return Parse(name);
			}

	};

};

Mondays are the best days of the week.

If you hate Mondays, then try Mondays. It is a kind of comedy show from the people that do the (also excellent) DotNetRocks. It is pure humour and the only thing I can’t stand about it is that it isn’t out evey Monday. For all the newer Mondays fans though, there is the enitre back catalogue online to keep you going.
 
This is the best podcast in the world ever!!!
 

MouseWheel Not Working

I saw in a newsgroup a problem where mousewheel wasnt working on a panel and the issue is because the form isnt passing the event on to its children. I wrote the code below which when pasted into a form. Makes it pass on the event so controls on the form get notified of the MouseWheel event.
bool sendingMessage = false;
  protected override void WndProc(ref Message m)
  {

   Point mp = MousePosition;
   if (m.Msg == 522)
   {
    if (sendingMessage)
   {
    m.Result = (IntPtr)0;
    return;
   }
    Debug.Print("Mouse Wheel: lparam = {0},WParam = {1}, Result ={2}", m.LParam, m.WParam, m.Result);
    try{
     sendingMessage = true;
    SendMessage(GetChildAtPoint(PointToClient(mp)).Handle,     m.Msg, m.WParam.ToInt32(), m.LParam.ToString());
    sendingMessage = false;
    }catch(Exception exception){
     exception.ToString();
    }
   m.Result = (IntPtr)0;

   }
   base.WndProc(ref m);
  }
  [DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hwnd, [MarshalAs(UnmanagedType.U4)] int Msg, int wParam, [MarshalAsSystem.Runtime.InteropServices.UnmanagedType.LPStr)] string lParam);
Hope it helps someone

Handy Tool: Control Printer

This is one of my more recent controls I wrote. It will grab the screen image of a control/form onto an image and print it. I didn’t even need this but wrote it to help someone in a newgroup. Hope its helpfull to someone else:
public class ControlPrinter
{
	[ThreadStatic()]
	static Image _imgToPrint;
	[ThreadStatic()]
	static Point _locationOnPage;

	public static Image GetControlImage(Control c)
	{
		Graphics mygraphics = c.CreateGraphics();
		Image img = new Bitmap(c.Size.Width, c.Size.Height, mygraphics);

		Graphics memoryGraphics = Graphics.FromImage(img);

		IntPtr sourceHandle = mygraphics.GetHdc();
		IntPtr destinationHandle = memoryGraphics.GetHdc();

		BitBlt(destinationHandle,0,0, c.Width, c.Height, sourceHandle, 0, 0, 13369376);
		mygraphics.ReleaseHdc(sourceHandle);
		memoryGraphics.ReleaseHdc(destinationHandle);

		return img;

	}
	public static void PrintControl(Control c, Point locationOnPage)
	{
		PrintDocument pd = new PrintDocument();
		PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();
		printPreviewDialog.Document = pd;
		_locationOnPage = locationOnPage;
		pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
		Graphics mygraphics = c.CreateGraphics();
		_imgToPrint = GetControlImage(c);

		printPreviewDialog.ShowDialog();
	}

	static void pd_PrintPage(object sender, PrintPageEventArgs e)
	{
		e.Graphics.DrawImage(_imgToPrint, _locationOnPage);
	}

	[DllImport("gdi32.dll")]
	static extern bool BitBlt(IntPtr hdc, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, uint dwRop);
}

Handy little tools

It has occured to me that so far through my career, I have written probably 10 difference line counters, file mergers, encryption tools and all sorts of other small utilities. I recently found an old cd with this stuff burned on to it which I original made to stop me remaking these things. I immediatly lost the disk and therefore had to re-invent these wheels any.
 
Time for change now though, the world has moved on since the CD burner was invented and now I am going to post them here. All my little tools and helper functions and controls are going to be put up here for me and the world to consume. I reckon this could potentially save millions of pounds of lost development hours and therefore make the world a better place.
 
First one comming soon