Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, August 8, 2013 4:04 PM
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.
All replies (3)
Friday, August 9, 2013 2:21 PM âś…Answered
Thanks for the answers!
@Hasani Holder
My user control is embedded in a tab, which in turn is embedded in the main form. I would like that the control is displayed all the time, so showing it in another form is not what I want.
@darnold924
I have tried something similar. In your example the connection between the Form1 and Form2 objects is done with:
var frm = new Form2(this);
I have already tried something similar, in a simpler way: the custom control has the parent Form as a member, which is initialized in the constructor:
GETPower m_parent;
public HistSecuritiesIn(Form parent)
{
m_parent = (GETPower)parent;
}
And then initialize the user control from the parent form:
this.histSecuritiesIn1 = new GET_Power.HistSecuritiesIn(this);
However, the VS designer has an error when I do this (it complains that constructor of the user control cannot accept a Form argument) and as a consequence the user control is not shown in the application. I haven't tried yet, but am not sure if it would work with the interface.
I gave a little more thought and found a simpler solution. Since the user control can access a public method of the parent form, I can call this method from the user control. Then, since the user control is a member of the form, it means the form has direct access to the data contained in the user control. Therefore I can copy all necessary data inside this method:
public void ShowPlots()
{
m_dtFrom = histSecuritiesIn1.DateFrom;
m_dtTo = histSecuritiesIn1.DateTo;
}
...problem solved
Thursday, August 8, 2013 4:30 PM
Where do you display theHistSecuritiesIn control? I'm not exactly sure what your problem is here but one option is to show it as a Dialog then use the properties you expose to update the values from the parent itself:
public partial class GETPower : Form
{
...
void SomeMethod()
{
this.histSecuritiesIn1.ShowDialog();
this.DateFrom = this.histSecuritiesIn1.DateFrom;
this.DateTo = this.histSecuritiesIn1.DateTo;
}
}
Thursday, August 8, 2013 7:48 PM
You can use an Interface between the form and the usercontrol. You would need a constructor on the usercontrol. The you will thie.
this.histSecuritiesIn1 = new GET_Power.HistSecuritiesIn(this);
It will pickup the properties and or methods defined in the Interface, and you are passing the interface to HistSecuritiesIn(). The interface and its objects are being shared between that called class and the calling class in the example code below.
Look at the Interface, see what is on the interface and see how it's being shared by the two forms and passing data between the two forms.
namespace Interfaceobject
{
public interface Interface1
{
TheObject theobj { get; set; }
string Lastname { get; set; }
string GetMiddleName();
}
}
This is just a custom object that's being used in the Interface nothing special.
namespace Interfaceobject
{
public class TheObject
{
public string Name { get; set; }
public void somemethod()
{
//some code
}
public string returnsomedata()
{
return string.Empty;
}
}
}
Here is form1 that implements the Interface look at the code carefully and see how it makes the call to form2 passing the interface
using System;
using System.Linq;
using System.Windows.Forms;
using System.Collections.Generic;
namespace Interfaceobject
{
public partial class Form1 : Form, Interface1
{
public Form1()
{
InitializeComponent();
}
private string middlename = "test1";
// public properties and method on the Interface1
public TheObject theobj { get; set; }
public string Lastname { get; set; }
public string GetMiddleName()
{
return middlename;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
var ints = new List<int>();
ints.Add(1);
ints.Add(1);
ints.Add(2);
var theints = (from a in ints select a).ToList();
for (int i = 0; i < theints.Count(); i++)
{
theints[i] = 4;
}
var obj = new TheObject {Name = "help"};
theobj = obj;
Lastname = "help1";
var frm = new Form2(this);
frm.Show();
var look = Lastname;
}
}
}
Here is form2 that was called from form1 with form1 passing the Interface to form2. Look at what is happening with the interface and how it is being used to share objects between the two classes. Look at the power of using Object Oriented Programming. You can do the same thing.
using System;
using System.Windows.Forms;
namespace Interfaceobject
{
public partial class Form2 : Form
{
private readonly Interface1 mView;
public Form2(Interface1 theview)
{
mView = theview;
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
var name = mView.theobj.Name;
var latname = mView.Lastname;
var middlename = mView.GetMiddleName();
//and on top of that, you can return data to Form1
mView.Lastname = "99999";
this.Dispose();
}
}
}