# Saturday, 29 May 2010

This weekend I decided to update my VS 2008 Express with MVC2.  You can do this with the Web Platform Installer (WPI). In order to install MVC2 you do need to be on SP1 for VS 2008 Express.  Your version needs to be "9.0.30729.1 SP".

Installing with WPI is a breeze but in my case WPI stated it installed MVC2 successfully but I didn’t have the MVC2 Project as a choice in my project list.  I tried it a few times and I always received a successful install message but still only had the MVC1 project choice.

The fix for me was pretty simple.  I just downloaded and installed MVC2 directly from the download center.

Happy Coding!

Posted 05.29.2010  #    Comments [0]  | 
# Friday, 07 May 2010

A few posts back I wrote about adding character counting to the .Net AJAX HTML Editor.  Recently, I received a question asking how to hide and show the character count element for the .Net AJAX HTML Editor when the editor goes in and out of focus.  The short and sweet answer is to wire up a function on the focus and blur events of the editor control and call the hide / show methods in jQuery on your count element.

$(GetEditor()).focus(function() {
  $(Editor1InfoArea).show();
}).blur(function() {
  $(Editor1InfoArea).hide();
});

Just add the above code in the $(document).ready function and away you go.  The GetEditor function and Editor1InfoArea variable are referenced in code in the original post.

However, jQuery has a lot to offer and I wanted to show some of these offerings.  I took advantage of method chaining to save myself having to call $(GetEditor()) twice as shown below.

$(GetEditor()).focus(function() { $(Editor1InfoArea).show();  });
$(GetEditor()).blur(function() { $(Editor1InfoArea).hide(); });

If your new to jQuery chaining may throw you a bit but it works great for saving keystrokes and once you get used to the syntax can be easy to follow. 

Beyond the show and hide methods you could call on many other methods to add some panache (Come on, give this one to me,  I am French after all).

You could replace show/hide with some of these combinations:

Nothing like making your app a little more appealing with some animation.  But wait! There’s more.

You can also change the style with the css method:

$(Editor1InfoArea).css("display", "block");
$(Editor1InfoArea).css("display", "none");
Or
$(Editor1InfoArea).css("visibility", "");
$(Editor1InfoArea).css("visibility", "hidden");

Of course, you are using classes to make easier management of your styling right?  You can use the addClass/removeClass methods:

$(Editor1InfoArea).addClass("showCounter");
$(Editor1InfoArea).removeClass("showCounter");

* What about the toggle method? Ah, I’m glad you asked. In this case since we are tracking the focus and blur events you may end up with the toggle working the opposite as you expect.  This is because toggle does the opposite of what the targeted element’s visible value is when called.  You can fix this by calling the show or hide on your element after the focus/bind methods are setup.  Toggle is really designed for the click event but can be used in this situation.

So there you have it.  More ways than you probably care to know and I wouldn’t be surprised if there were more. 

A parting thought.  Earlier this week I read a great post on the performance of show/hide vs. toggle, css and addClass/removeClass.  Check it out if you want to read more on it but the basic answer is calling the css method is the best.  However, there is one more way that’s even faster but you’ll just have to read the article to find out what it is.

Happy Coding! :-)

Posted 05.07.2010  #    Comments [0]  | 
# Thursday, 22 April 2010

I recently needed to make a change to some code that had a routine for getting the week currently in.  I needed to add the ability to get the previous / last week relative to this week.  No problem, I would just work off of the existing routine.

Private Sub SetDatesToThisWeek()

  Dim dateStart As String
  Dim dateEnd As String

  Dim dateNow As Date
  dateNow = Now

  dateStart = (Weekday(dateNow, Microsoft.VisualBasic.FirstDayOfWeek.Sunday) - 1).ToString
  dateStart = DateAdd(DateInterval.Day, -CType(dateStart, Double), dateNow).ToString
  dateStart = Format(System.Convert.ToDateTime(dateStart), "MM/dd/yyyy")

  dateEnd = DateAdd(DateInterval.Day, 6, System.Convert.ToDateTime(dateStart)).ToString
  dateEnd = Format(System.Convert.ToDateTime(dateEnd), "MM/dd/yyyy")
 
  Me.StartDate.Text = dateStart
  Me.EndDate.Text = dateEnd
End Sub

It took me a little bit of time to digest the routine and realized I needed to simplify the routine and even though the code is in VB I wanted to remove the dependence on the Microsoft.VisualBasic library as well.  This means getting rid of Weekday, DateAdd, etc and replace with the framework standard.  I love not reinventing the wheel and happened to find a nice pointer on stack overflow.

Private Sub SetDatesToThisWeek()

  Dim thisSunday As Date = DateTime.Now.AddDays(-(DateTime.Now.DayOfWeek - DayOfWeek.Sunday))
  Me.StartDate.Text = Format(thisSunday, "MM/dd/yyyy")
  Me.EndDate.Text = Format(thisSunday.AddDays(6), "MM/dd/yyyy")

End Sub

11 lines of code and three variables down to three lines and one variable with the added benefit of being easier to read, understand and the reliance on Microsoft.VisualBasic gone (at least for this code). Since I needed to create another routine that returns last week I decided as suggested in one of the stack overflow answers to create an extension on the date type.

Imports System.Runtime.CompilerServices

Module DateExtensions

  <Extension()> _
  Public Function StartOfWeek(ByVal aDate As Date, ByVal dayToStartWeek As DayOfWeek) As Date
    Return aDate.AddDays(-(aDate.DayOfWeek - dayToStartWeek))
  End Function
End Module

Now I can use the extension like so:

Private Sub SetDatesToLastWeek()

  Dim lastSunday As Date = DateTime.Now.StartOfWeek(DayOfWeek.Sunday).AddDays(-7)
  Me.StartDate.Text = Format(lastSunday, "MM/dd/yyyy")
  Me.EndDate.Text = Format(lastSunday.AddDays(6), "MM/dd/yyyy")
End Sub

Private Sub SetDatesToThisWeek()

  Dim thisSunday As Date = DateTime.Now.StartOfWeek(DayOfWeek.Sunday)
  Me.StartDate.Text = Format(thisSunday, "MM/dd/yyyy")
  Me.EndDate.Text = Format(thisSunday.AddDays(6), "MM/dd/yyyy")
End Sub
The C# version of the extension is a little different than VB:
namespace Extensions
{
    public static class DateExtension
    {
        public static Date StartOfWeek(this Date aDate, DayOfWeek dayToStartWeek)
        {
            return aDate.AddDays(-(aDate.DayOfWeek - dayToStartWeek));
        }
    }   
}

Leaving code better off than when you started is always a great feeling.

Happy Coding!

P.S. – I also replaced the date formatting strings with a constant. :-)

C# | Programming | VB
Posted 04.22.2010  #    Comments [0]  |