5 Replies - 439 Views - Last Post: 04 December 2012 - 08:03 AM Rate Topic: -----

#1 ravva  Icon User is offline

  • New D.I.C Head

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

Validate 2 Datetimepickers

Posted 29 November 2012 - 09:27 PM

i code this for DTPicker2 validation, Both DateTimePickers should be in same month,and DTPicker1 cannot has younger date than TDPicker2 eg(2012/2/10- 2012/2/27)

i coded to validate first rule(Both DateTimePickers should be in same month),but it ll never end.code as below,n picture attached,Thank you all.

  if (dateTimePicker1.Value.ToString("MM") != dateTimePicker2.Value.ToString("MM"))
            {
                MessageBox.Show("Cannot search for different months!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);

            }
            else
            {
              

            }

Attached image(s)

  • Attached Image

This post has been edited by Atli: 29 November 2012 - 09:29 PM
Reason for edit:: Please use [code] tags when posting code.


Is This A Good Question/Topic? 0
  • +

Replies To: Validate 2 Datetimepickers

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: Validate 2 Datetimepickers

Posted 29 November 2012 - 10:03 PM

First there is no need to convert the month to a string to compare it, just compare the month values directly.

Second, how are you calling this validation routine?

Third, just set the max date on the 2nd control after they pick the date on the first control and no validation is needed (set min date too).

This post has been edited by Momerath: 29 November 2012 - 10:05 PM

Was This Post Helpful? 2
  • +
  • -

#3 ravva  Icon User is offline

  • New D.I.C Head

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

Re: Validate 2 Datetimepickers

Posted 29 November 2012 - 10:41 PM

I put it on DateTimePicker2,

private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {
            if (dateTimePicker1.Value.ToString("MM") != dateTimePicker2.Value.ToString("MM"))
            {
                MessageBox.Show("Cannot search for different months!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
            }
        }





No it cant solve by setting Max date because DTP1 need to has privileges to select any month,after user select a day from DTP1 Automatically set last day of that month,
but still user can change DTP2 to different month, I want to pop errormsg when user select another month from DTPicker2

thank you Mr. Momerath
Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Validate 2 Datetimepickers

Posted 30 November 2012 - 06:40 AM

Why would you allow the user to selected a different month for DTP2 when you are just going to show them an error message if they do? Normally you would do this type of validation when you can't control what the user is going to enter(like typing a date into a textbox), but using the date pickers, you can control. No need to do validation if you can limit the entry.
Was This Post Helpful? 1
  • +
  • -

#5 ravva  Icon User is offline

  • New D.I.C Head

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

Re: Validate 2 Datetimepickers

Posted 02 December 2012 - 11:40 PM

No user can select Days within one month,if user selected another month by DTPicker2,user have to see Error-message,
i validated it but problem is ErrorMsg is reapeating as picture given, I really dont hv idea to do it,if you can provide me a code,tht ll be really help for me..thank you :blink:
Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Validate 2 Datetimepickers

Posted 04 December 2012 - 08:03 AM

Nothing in the few lines of code that you have shown will repeatedly show the error message. So that issue must be somewhere else. Show us more code.

And with your "custom" validation(instead of using the DateTimePicker properties to properly limit the dates), I am allowed to chose a date in different years, as long as the month is the same. Is that what you are going for? I can choose Dec. 4, 2012 and Dec. 12, 2087, and that is valid by your validation.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1