I have two forms and i have retrive controls from one form to another.

In form 1
Two controls
1 datetimepicker1
2 datetimepicker2

in form 2
1 Datetimepicker1

now i retrive form1 controls on form2 and i want to compare form1 datetimepicker with form2 datetimepicker.

here is code:


Form2 fr2 = new Form2("", "");
            DateTime cdt = dateTimePicker1.Value;
            if ((fr2.dateTimePicker1.Value<=cdt) && (fr2.dateTimePicker2.Value>=cdt))
              MessageBox.Show("This Date Is Not Valid In Current Accounting Year", "Correct Date");

 MessageBox.Show("You Can Save");

but this code not giving me the proper output. it always goes in else condition.

either the form2 datetimepicker1 value not properly change.

but can you solve this problem.

Edited by virusisfound: n/a

5 Years
Discussion Span
Last Post by virusisfound

When you open form2, pass a parameter of dateTimePicker (a value of it) in constructor to form2. So now you have both values on forms. You only get a value of dateTimePicker from form2 and compare them.


Form 2 is always display on the top of the screen and all these forms are run under the form2 because it is MDI Form.


In line 1 of your sample code you create a new Form2 that is not displayed and then you pull the date from it. If you already have a Form2 displayed, then you don't need that line at all.


Yes, you create an object of that form, but it has no relationship to the other form2 that you are showing. It is a different object. You have to get a reference to the original form2 or your code will not work.


For this kind of problems, i hav a solution. Instead of Creating the Object for Form2 in Form1, u create that Object Globally in Program.cs and Use every where Program.<object>.<Controll Name>, which will work fine.

Hope it will Help U.


I am sorry momerath I realy dont have so much of knowledge about this but I want to learn it. but thank you for reply my post.

Thank you akshintlakalyan for telling me another solution I tried it but i hope you will tell me how to do it.

I am totaly new in the C# with this concept.

I have create an object in program.cs
here is the code

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsApplication15
    static class Program
        /// <summary>
        /// The main entry point for the application.
        /// </summary>

        static void Main()
            Application.Run(new Form1());
            Form2 fr2 = new Form2();

is it is correct.


This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.