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");
            }

            else
{
 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.

Recommended Answers

All 10 Replies

You never show Form2 so how does the date get set?

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.

Form2 fr2 = new Form2("", "");

This line is for creating an object of that form because we can't access that controls directly we have to create the object.

http://www.daniweb.com/forums/thread298003-2.html

this coding i use to access forms controls.

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.

how to do that please tell me.

Since you've posted no code nor explained anything about where your method is, no.

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>
        [STAThread]

        
      
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
           
            Form2 fr2 = new Form2();
        }
    }
}

is it is correct.

thanks....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.