-3

In C# I want get the output when I click in the button(Calculate Age ) in the first form give me the output in second form in the label1 how ?? I tried label.Text but it gives me an error

First Form

public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();
    }

    public void Form1_Load(object sender, EventArgs e)
    {

    }

    public void dateTimePicker1_ValueChanged(object sender, EventArgs e)
    {
        DateTime startTime = Convert.ToDateTime(dateTimePicker1.Value);
        DateTime endTime = DateTime.Today;
        TimeSpan span = endTime.Subtract(startTime);
        var totalDays = span.TotalDays;
        var totalYears = Math.Truncate(totalDays / 365);
        var totalMonths = Math.Truncate((totalDays % 365) / 30);
        var remainingDays = Math.Truncate((totalDays % 365) % 30);
        lable1.Text = string.Format("{0} year(s), {1} month(s) and {2} day(s)", totalYears, totalMonths, remainingDays);
    }

    public void button1_Click(object sender, EventArgs e)
    {
        Form2 frm = new Form2();
        frm.Show();
    }
}
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • 1
    You're basically asking how to move data between forms. Once you understand the principles, the data and the forms don't matter as it's all the same. Check [this](http://jmcilhinney.blogspot.com/2012/04/managing-data-amongst-multiple-forms.html) out. – jmcilhinney Nov 16 '19 at 15:06

1 Answers1

0

You need to begin by defining/exposing a method in Form2 which updates the Label Control.

public void UpdateLabel(string message)
{
    lable1.Text = message;
}

The second step would involve of two sub steps.

a) Ensure instance of Form2 is a member variable

private Form2 frm;
public void button1_Click(object sender, EventArgs e)
{  
    if(frm==null)
    {  
       frm = new Form2();
    }
    frm.Show();
}

b) Call the newly defined method using the instance of Form2.

public void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
    DateTime startTime = Convert.ToDateTime(dateTimePicker1.Value);
    DateTime endTime = DateTime.Today;
    TimeSpan span = endTime.Subtract(startTime);
    var totalDays = span.TotalDays;
    var totalYears = Math.Truncate(totalDays / 365);
    var totalMonths = Math.Truncate((totalDays % 365) / 30);
    var remainingDays = Math.Truncate((totalDays % 365) % 30);

    if(frm==null)  //Updated based on comment
    {  
       frm = new Form2();
    }
    frm.UpdateLabel(string.Format("{0} year(s), {1} month(s) and {2} day(s)", totalYears, totalMonths, remainingDays));

}
Anu Viswan
  • 17,797
  • 2
  • 22
  • 51
  • its told me there are erro "System.NullReferenceException: " in this frm frm.UpdateLabel(string.Format("{0} year(s), {1} month(s) and {2} day(s)", totalYears, totalMonths, remainingDays)); – Hussain ALRomaih Nov 16 '19 at 18:23
  • Has frm been initialized ? It looks like dateTimePicker1_ValueChanged was called before button1_Click, which meant, frm2 is not initialized or displayed. – Anu Viswan Nov 17 '19 at 01:20
  • its after the dateTimePicker1_ValueChagned when i click on the button its still says there are in erro "System.NullReferenceException: " – Hussain ALRomaih Nov 17 '19 at 20:56
  • @HussainALRomaih that explains the error. In that case, you need to initialize the form before the update call. Please check the answer. I have updated the answer. – Anu Viswan Nov 17 '19 at 21:32