Tuesday, 14 July 2009

DateTime in c#

How to get the number of working days per month (except the legal holidays for each country)?

This is a question that i had to answer for the timing app. Here are some great resources on DateTime class and also on System.Globalization.

And here is my implementation on how to get this number:
private void nrZileLucratoare(int luna)
{
string numeLuna = "";
switch (luna)
{
case 1: numeLuna = "Jan"; break;
case 2: numeLuna = "Feb"; break;
case 3: numeLuna = "Mar"; break;
case 4: numeLuna = "Apr"; break;
case 5: numeLuna = "May"; break;
case 6: numeLuna = "Jun"; break;
case 7: numeLuna = "Jul"; break;
case 8: numeLuna = "Aug"; break;
case 9: numeLuna = "Sep"; break;
case 10: numeLuna = "Oct"; break;
case 11: numeLuna = "Nov"; break;
case 12: numeLuna = "Dec"; break;
}
int totalZile = DateTime.DaysInMonth(DateTime.Now.Year, luna);

int contor=0;
for (int i = 1; i <= totalZile; i++)
{
string Duminica = "Sun "+i+" "+numeLuna+" 9:30 AM 2009";
string Sambata = "Sat "+i+" "+numeLuna+" 9:30 AM 2009";

string format = "ddd d MMM h:mm tt yyyy";
DateTime dateTime;

if (DateTime.TryParseExact(Duminica, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime))
{
contor += 1;
}
if (DateTime.TryParseExact(Sambata, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime))
{
contor += 1;
}
}
MessageBox.Show((totalZile - contor).ToString()); // this is the actual number
}

Friday, 3 July 2009

Update

A short update on what has been going on.. well the app is running, and the guys at work are using it :).. i am working on finishing the Admin part to set up some usage statistics with the data collected from all the guys to complete the monthly metrics. It is going good, hope this weekend i will get things done and i will release the Admin part of the app and that will be the full package and on to a new project :p.
I realize that the app is restrictive to our department, and to that kind of reporting, it's not a general app that you can use to time any activity..

Wednesday, 1 July 2009

Drag and Drop - complete

I managed to implement it, here it goes:
private void listView1_DragDrop(object sender, DragEventArgs e)
{

string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

foreach(string nume in files)
{
Stream fisier = new FileStream(nume, FileMode.Open, FileAccess.Read);
BinaryFormatter formater = new BinaryFormatter();
int contor = (int)formater.Deserialize(fisier);
for (int i = 0; i < contor; i++)
{
Code.Inregistrare inreg = (Code.Inregistrare)formater.Deserialize(fisier);
//MessageBox.Show(inreg.ToString());
ListViewItem item = new ListViewItem(inreg.Data.ToString());
item.SubItems.Add(inreg.Request.ToString());
item.SubItems.Add(inreg.Description);
item.SubItems.Add(inreg.Tara);
item.SubItems.Add(inreg.Requester);
item.SubItems.Add(inreg.Person);
item.SubItems.Add(inreg.OraInceput.ToString());
item.SubItems.Add(inreg.OraSfarsit.ToString());
item.SubItems.Add(inreg.Durata.ToString());
listView1.Items.Add(item);
}
}

}

private void listView1_DragEnter(object sender, DragEventArgs e)
{
//imi copiaza ce ii dau drag
e.Effect = DragDropEffects.Copy;

}

Drag and Drop

I found a great resource on Drag & Drop with step by step instructions and pics:
here

Version 1.0

Finally it's here. I have finished it (as v1.0) and today was the first day it was used. I got some feedback at the end of the day regarding it :
  • To Implement drag and drop files from Explorer into a ListView ( this sounds a little bit tricky)
  • Make so accelerators and just "polish" a little the controls for smother access.
I have stared already reading about the drag & drop functions and how to implement them so, by my next post i will try to post what i came up with.

Till then,
here
is the app ( the Visual Studio 2008 project, or just go in the BIN directory to find the .exe file ;)
Enjoy.

PS: Also a pic