Posts

MFC getting the current date and time

I've been updating some older MFC/C++ applications and I found I needed to get the current system time. There's a way in the Win32 API but it's clunky. MFC gives you a simple way. CTime t = CTime :: GetCurrentTime (); CString s = t . Format ( "%m%d%Y" ); The Format method can take a wide variety of parameters. See this MSDN link

ASP.NET dynamically load AJAX toolkit accordion panes

I love the Accordion pane from the AJAX toolkit, but I've found myself needing to dynamically add accordion panes to it. To do this you need to some work on the back end. The following code loops through a DataSet and dynamically creates a Label control for the header and content. It then adds those controls to a new AccordionPane and then add it to your Accordion. for ( var i = 0; i < ds.Tables[10].Rows.Count; i++ ) { Label lblContent = new Label(); lblContent.ID = Guid.NewGuid().ToString(); Label lblTitle = new Label(); lblTitle.ID = Guid.NewGuid().ToString(); lblTitle.Text = ds.Tables[2].Rows[i + 1][1].ToString(); lblContent.Text = ds.Tables[10].Rows[i][1].ToString(); AjaxControlToolkit.AccordionPane pane = new AjaxControlToolkit.AccordionPane(); pane.ID = Guid.NewGuid().ToString(); pane.HeaderContainer.Controls.Add( lblTitle ); pane...

C# Read File one line at a time

This might seem simple, but often times you find yourself needing to read a file one line at a time no matter what language you do it in. C# makes this very easy with a StreamReader. You simply create the stream reader passing in the file name to the constructor and then call ReadLine on it. See the code below: string line = null; // always use a using with a stream, this ensures it gets disposed of properly using(StreamReader sr = new StreamReader(fileName)) { // we use a while loop until line == null which means end of file while( (line = sr.ReadLine() ) != null ) { // Do something here } }

Three simple tricks for better C# code

Here are 3 tricks that once you start using them can save you a lot of typing when dealing with C#. 1. The Null Coalescing Operator (??) This is a short-cut for the ternary operator (?:) checking against a null: string name = value; if (value == null) { name = string.Empty; } Can now be condensed into one line: string name = value ?? string.Empty; 2. Auto properties There is no need to define a variable for simple backing fields for most properties in C#. In fact most things like int, strings, double i just let the compiler handle it. public class MyPoint { public int X { get; set; } public int Y { get; set; } } 3. Is vs As You should try and avoid the is operator when you can do an as cast. The is requires two casts where the as does just one. Even with the added null check it's always going to be faster to do an as vs is. var sq = Square as Shape; if ( sq != null ) { volume = sq.Calcul...

ASP.NET User Control with Template Content

Asp.net user controls are great.  I use them all the time when working on .net websites.  I was working on a site using the asp.net ajax extender toolkit.  I created a panel that could be expanded collapsed with a button click.  I inserted a few into the site and liked them so much I made some more.  Well after the 7th panel it started getting tedious.  What a perfect place for a user control.  The problem was I needed to be able to place whatever custom content I wanted inside the panel.  After much searching around I wasn't finding what I wanted.  The answer it turns out is an asp.net user control with custom templates. Create a new user control for your asp.net site.  Mine is called CollapsiblePanel.  In the code behind for the page you need to add a new class MessageContainer that inherits from Control and implements the INamingContainer Interface .   Then in the user controls OnInit method you need to check for cont...

C++ convert char * to to wide string wstring

Often times when working with older code you find yourself staring at some old char* variables. I often find myself having to convert them to wide strings for unicode support (wstring). I created a handy function that makes this simple. wstring charToWideString( const char* src ) { return std::wstring( src, src + strlen(src) ); } Now simply call thus: wstring thus = charToWideString( "thus" ); Drop this function in a static class or header file and use it anywhere.

ASP.NET Create Javascript Alert on Server Side

Many times in ASP.net you find yourself needing to create an alert on the client side but you're processing on the server side. The trick is to register a Javascript to the client from the server. I have found a nifty way of creating a class that will allow you to do this. public static class ClientMessageBox { public static void Show(string message, Control owner) { Page page = (owner as Page) ?? owner.Page; if (page == null) { return; } page.ClientScript.RegisterStartupScript(owner.GetType(), "ShowMessage", string.Format("<script type="text/javascript">;alert('{0}')</script>", message)); } } // Example of using class and method protected void Page_Load(object sender, EventArgs e) { ClientMessageBox.Show("Hello World", this); } You can now call ClientMessageBox.Show('text', this ); anywhere server side and get a ...