6 Replies - 1680 Views - Last Post: 29 November 2012 - 04:35 AM Rate Topic: -----

#1 ravva  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 20
  • Joined: 28-November 12

how combine 2 DateTimePickers with automatically update 2nd DTpicker

Posted 28 November 2012 - 11:44 PM

im new to C# i want to create form tht, user can select any date, month,year from DateTimePicker1 after selecting it, automatically updated DateTimePicker2 to last day of month that user selected in Datetimepicker1,Any one have code for it.I attached picture,Thank you.

:helpsmilie:/>Attached Image

Is This A Good Question/Topic? 0
  • +

Replies To: how combine 2 DateTimePickers with automatically update 2nd DTpicker

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: how combine 2 DateTimePickers with automatically update 2nd DTpicker

Posted 29 November 2012 - 12:38 AM

Get DateTime from picker, create new Datetime by setting day to 1 and month to +1 from the DateTime you previously got. Add -1 to date. You now have the last day of the month.

All this can be done at once :)
Was This Post Helpful? 3
  • +
  • -

#3 ravva  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 20
  • Joined: 28-November 12

Re: how combine 2 DateTimePickers with automatically update 2nd DTpicker

Posted 29 November 2012 - 02:11 AM

I coded it given as below Mr Momerath, but some months has 31,30,28days,
do you have correct code for it??
can you kind enough to post it,thank you

string theyd = dateTimePicker1.Value.ToString("dd");
int num2 = int.Parse(theyd);
int rest = mdays - num2;
var endDate = default(DateTime);
endDate = dateTimePicker1.Value.AddDays(rest);
dateTimePicker2.Value = endDate;
Was This Post Helpful? 0
  • +
  • -

#4 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: how combine 2 DateTimePickers with automatically update 2nd DTpicker

Posted 29 November 2012 - 02:28 AM

DateTime dtp1 = dateTimePicker1.Value;
DateTime dtp2 = dtp1.AddMonths(1).AddDays(dtp1.Days * -1);
dateTimePicker2.Value = dtp2;


First line we get the value of the first DTP.
Second line we advance to the next month (AddMonths) then subtract the number of days we are currently in this month. This results in a day of zero (0) which will make the DateTime value adjust to the last day of the previous month.
Third line assigns it to the second DTP.

All this could have been done on one line, but why push things :)
Was This Post Helpful? 3
  • +
  • -

#5 nilkanthnaik5  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 16
  • Joined: 22-October 12

Re: how combine 2 DateTimePickers with automatically update 2nd DTpicker

Posted 29 November 2012 - 02:56 AM

Try this..

Instruction:-
Take 2 DateTimePicker controls
DateTimePicker1 and DateTimePicker2
Write following code in ValueChanged event of DateTimePicker1
And run the code.



private void DateTimePicker1_ValueChanged(System.Object sender, System.EventArgs e)
{
	DateTime selectedDate = DateTimePicker1.Value;
	DateTime lastDayOfMonth = new DateTime(selectedDate.Year, selectedDate.Month, DateTime.DaysInMonth(selectedDate.Year, selectedDate.Month));
	DateTimePicker2.Value = lastDayOfMonth;
}


Was This Post Helpful? 1
  • +
  • -

#6 nilkanthnaik5  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 16
  • Joined: 22-October 12

Re: how combine 2 DateTimePickers with automatically update 2nd DTpicker

Posted 29 November 2012 - 03:19 AM

@Momerath


DateTime dtp1 = dateTimePicker1.Value;
DateTime dtp2 = dtp1.AddMonths(1).AddDays(dtp1.Days * -1);
dateTimePicker2.Value = dtp2;


There is small error in above code on line no 2.

use dtp1.Day instead of dtp1.Days


DateTime dtp2 = dtp1.AddMonths(1).AddDays(dtp1.Day * -1);


Was This Post Helpful? 1
  • +
  • -

#7 ravva  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 20
  • Joined: 28-November 12

Re: how combine 2 DateTimePickers with automatically update 2nd DTpicker

Posted 29 November 2012 - 04:35 AM

:rockon:

Thakx brothers...........
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1