Hello everyone!
I am trying to make this work and so far every answer I found on the net did not work for me. So here is the problem: I have a Winform in which I embed a user control. The user control contains some datetime pickers and several buttons (among other controls). I would like to fire an event on one of the buttons on the user control, which should send some data to the parent form. This is the relevant piece of code:
namespace GET_Power { public partial class GETPower : Form { private DateTime m_dtFrom; private DateTime m_dtTo; public DateTime DateFrom { get { return m_dtFrom; } set { m_dtFrom = DateFrom; } } public DateTime DateTo { get { return m_dtTo; } set { m_dtTo = DateTo; } } public GETPower() { InitializeComponent(); } public void PrintMessage() { Console.WriteLine("Hello world!"); Console.WriteLine("date from: " + m_dtFrom.ToShortDateString()); Console.WriteLine("date to: " + m_dtTo.ToShortDateString()); } private HistSecuritiesOut histSecuritiesOut1; private void InitializeComponent() { //... this.histSecuritiesIn1 = new GET_Power.HistSecuritiesIn(); } } }
and:
namespace GET_Power { public partial class HistSecuritiesIn : UserControl { private DateTime m_dtFrom; private DateTime m_dtTo; public DateTime DateFrom { get { return m_dtFrom; } } public DateTime DateTo { get { return m_dtTo; } } public HistSecuritiesIn() { InitializeComponent(); dateTimePickerFrom.Value = DateTime.Today; dateTimePickerTo.Value = DateTime.Today; } private void buttonPlot_Click(object sender, EventArgs e) { GETPower parent = (GETPower)this.ParentForm; parent.DateFrom = m_dtFrom; parent.DateTo = m_dtTo; parent.PrintMessage(); } } }
After pressing the button, the PrintMessage() function from the parent Form is being called and I can see the output on the screen, however, the two DateTime members are not updated.
I have also tried with
GETPower parent = (GETPower)this.ParentForm;
when accessing the parent Form, but the result is the same. I don't quite understand what am I missing. Any help is appreciated.