Friday, July 24, 2009

Getting the day of the week the first day of the month is.

A co-worker of mine is learning to program and decided to write a calendar program. He needed a way to find out what day of the week the first day of the month would be. In his explaining of how he was doing it we came up with the following code that would work for his needs.


   17 static void Main(string[] args)
   18 {
   19 
   20     string date = "1-1-09";
   21     string dofw = GetDayOfWeekFromString(date);
   22 
   23 }
   24 
   25 private static string GetDayOfWeekFromString(string date)
   26 {
   27     DateTime desiredDay = new DateTime();
   28     DateTime.TryParse(date, out desiredDay);
   29     return desiredDay.DayOfWeek.ToString();
   30 }


After I gave him that code I decided to just pack it into an extension function. Like so.


   36 public static string GetFirstDayOfMonthDayOfWeek(this DateTime dt)
   37 { 
   38     DateTime firstOfMonth = new DateTime();
   39     if (DateTime.TryParse(string.Format("{0}/1/{1}", dt.Month, dt.Year), out firstOfMonth))
   40     {
   41         return firstOfMonth.DayOfWeek.ToString();
   42     }
   43     return string.Empty;
   44 }


Which can be called like this.

   21 string dofw = DateTime.Now.GetFirstDayOfMonthDayOfWeek();


Hope someone else finds it of use.

Thursday, July 23, 2009

My python hours worked calc.

I use this horrid little piece of code to tell me how long i work each day. It isn't all that grate but it gets the job done.

ClockInHoure = 7
ClockInMinuet = 10
ClockOutHoure = 5
ClockOutMinuet = 0
lunchstartHoure = 11
lunchstartMinuet = 28
lunchEndHour = 12
lunchEndMInuets = 05

#caculate hours worked
mHours = 12 - ClockInHoure
aHours = ClockOutHoure
#get theminuets
mMinuets = mHours * 60 - ClockInMinuet
aMinuets = aHours * 60 + ClockOutMinuet

tMins = mMinuets + aMinuets

Lhours = lunchEndHour - lunchstartHoure
lmins = lunchstartMinuet - lunchEndMInuets

bMin = Lhours * 60 - lmins


tMins -= bMin

tHour = tMins / 60
tMinuets = tMins % 60
if(tMinuets < 10):
pMin = "0"+ str(tMinuets)
else:
pMin = str(tMinuets)
print "%d:%s" %(tHour,pMin )

Monday, July 20, 2009

Setting the selected treeItem in a treeview on right click

So over the past several weeks I have been working with a lot of TreeViews in WPF I use a HierarchicalDataTemplate with a custom ItemContainerStyle for my nodes in the tree. I have been having a rather hard time finding a way to easily set the right clicked on node before the context menu is opened on it. My HierarchicalDataTemplate consists of a stackpanel with a context menu and a image and textblock.

In the ContextMenu_Opened function i put the following code.

   93 var cMenu = sender as ContextMenu;
   94             var tbase = (cMenu.PlacementTarget as StackPanel).DataContext as BasicTreeViewBase;
   95             tbase.IsSelected = true;


Which will set my selected item to true. The BasicTreeViewBase is a ViewModdle object baised off of the code found at TreeViewWithViewModel and is rather simple.