6 Replies - 12185 Views - Last Post: 17 December 2008 - 03:52 AM Rate Topic: -----

#1 xDementiax  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 16-December 08

Retrieving Time from DateTimePicker

Posted 16 December 2008 - 02:58 AM

Hi I'm new here and new to C#

I'm trying to write a program where you can enter in 2 times into 2 DateTimePickers and it will tell you how long you have been doing something.

private void btnAdd_Click(object sender, EventArgs e)
		{
			DateTime start = dtpStart.Value;
			DateTime end = dtpEnd.Value;

			TimeSpan gameplay = end - start;

			lblGameplay.Text = "Total Daily Gameplay Hours: " + gameplay.TotalHours.ToString() + " Hrs";
			prgGameplay.Value = int.Parse(gameplay.TotalHours.ToString());
		}


That is the code I have now, the only problem I have now is every time I add a duration, I want it to add onto the progress bar and my label so I imagine I need some form of loop.

Much help would be appreciated :)

This post has been edited by xDementiax: 16 December 2008 - 04:14 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Retrieving Time from DateTimePicker

#2 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 170
  • View blog
  • Posts: 637
  • Joined: 07-October 08

Re: Retrieving Time from DateTimePicker

Posted 16 December 2008 - 04:56 AM

Hi xDementiax,
From what I can gather is you want to record the time when they start playing, and then record the time when they finish playing, and display it in the form o both a label and a progress bar?

What I would do is create a seperate class, maybe called player info:

// class for the players info
public class PlayerInfo
{
	// the start time for this player
	DateTime startTime;

	// default constructor
	public PlayerInfo()
	{
		// get the start time on creation
		this.startTime = DateTime.Now;
	}
	
	// method to return play time
	public TimeSpan ReturnMyPlayTime()
	{
		// return a timepsan based on the time now -starttime
		return DateTime.Now - this.startTime;
	}

}



Then to use class it would be something like this:

// create a new player - this takes care of start time in constructor
PlayerInfo player1 = new PlayerInfo();
// then when you want to get played time use this:
player1.ReturnMyPlayTime();



Hopefully this is useful :)
Was This Post Helpful? 0
  • +
  • -

#3 xDementiax  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 16-December 08

Re: Retrieving Time from DateTimePicker

Posted 16 December 2008 - 04:57 AM

but I also have another problem, I'm using a combo box that has a list of names, is there a way anytime I change to a different name, that when I go to add another duration, it adds a completely new duration to my label and progress bar.

edit: Thanks for the help ben, but I am actually using 2 DateTimePickers, so the user can select the 2 dates. I have succesfully managed to subtract the 2 dates and display them on my label and progress bar but I also need it, so everytime I click the button, it adds to the duration on my label and progress bar.

This post has been edited by xDementiax: 16 December 2008 - 05:00 AM

Was This Post Helpful? 0
  • +
  • -

#4 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 170
  • View blog
  • Posts: 637
  • Joined: 07-October 08

Re: Retrieving Time from DateTimePicker

Posted 16 December 2008 - 05:21 AM

Hmmm I think I see what your getting at... so to check this is right, you want to select a name, and then set the progress bar and label to the value of the timepsan between that names start and end times?

Sorry I didn't notice the two date time pickers there :) so they pick both times then?

Also, I noticed that this bit of code you used:

prgGameplay.Value = int.Parse(gameplay.TotalHours.ToString());



may cause you some problems... try this instead:

prgGameplay.Value = (Int32)gameplay.TotalHours;



As this saves casting to a string and then parsing to an int.

As for the thing with the 2 datetimepickers I'll have a stab although I'm not sure I get it.

I would have a class for each player, this stores the times they pick and all the other info you want, including a field for name. This name is then what you have in your combobox and when selected you set the progress bar and label from the returned value for your player - this would also require the class to have a method similar to the one I provided below. Then you could just set the values from the value returned from whatever player you select.

Sorry, that bottom bit may have been a bit confusing there!

What I should have said is have a class, lets call it Player with the follwing fields:

- Name (String)
- TimeA (DateTime)
- TimeB (DateTime)

and methods:

- ReturnDuration (TimeSpan)

then pick the player from his name which is in the combo box, and call the ReturnDuration method to provide you with a timespan to set you progress bar and labels from.

:)
Was This Post Helpful? 0
  • +
  • -

#5 xDementiax  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 16-December 08

Re: Retrieving Time from DateTimePicker

Posted 16 December 2008 - 05:30 AM

I posted a zip to show you the solution file, if you can kinda get what I'm trying to do.

At the moment the program works semi-decent anyway. I never wanted to store the duration permanently, just display it on the screen temporarily and move on :)

The only thing I'm really missing is accumulating the hours (eg. if you play 2 hours in the morning, 3 hours in the afternoon etc. in the same day) as the program displays the duration of played time for 1 day.

And that when I select to a different person in the combo box, it resets the duration so then you can set new times for that person.

Attached File(s)

  • Attached File  CA3b.zip (40.71K)
    Number of downloads: 135

Was This Post Helpful? 0
  • +
  • -

#6 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 170
  • View blog
  • Posts: 637
  • Joined: 07-October 08

Re: Retrieving Time from DateTimePicker

Posted 16 December 2008 - 06:03 AM

Cool I get it now it's so you could monitor how much gameage you've been getting!

Ok I modded the program a slight bit - I changed the datetimepicker ShowUpDown property to true to allow you to choose a time, then I have made some small mods to the way the label and the progress bar get their values... take a look! However I have added no error checking or any thing so don't let it go above 24 hrs!

Here is the modded code, you will need to change the property for the datetimepicker yourself, or use the zipped solution I've attached :)

private void btnAdd_Click(object sender, EventArgs e)
{
	DateTime start = dtpStart.Value; // variable used to store start time
	DateTime end = dtpEnd.Value; // variable used to store end time

	TimeSpan gameplay = end - start; // variable to subtract both times
									 // TimeSpan object must be used instead of DateTime

	// set existing time from progress bar value
	TimeSpan existingTime = new TimeSpan(prgGameplay.Value, 00, 00);

	lblGameplay.Text = "Total Daily Gameplay Hours: " + (existingTime.TotalHours + gameplay.TotalHours).ToString() + " Hrs"; // displays the current duration in hours to the nearest 2 decimals
	prgGameplay.Value += (Int32)gameplay.TotalHours; // displays the current duration on a 24 hour progress bar
}



Hope this works for you :)

Attached File(s)

  • Attached File  CA3b.zip (44.53K)
    Number of downloads: 122

Was This Post Helpful? 1
  • +
  • -

#7 xDementiax  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 16-December 08

Re: Retrieving Time from DateTimePicker

Posted 17 December 2008 - 03:52 AM

Thanks for that mate, that's exactly what I wanted.

I finally got the screen to clear as well by figuring out all I need to do was make a method for SelectedIndex change on the combo box to clear the data :)

I think I made it sound alot more complicated then it actually was :P
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1