I am dealing with 2 forms. A child form and a parent form.
I want to enable a textbox on a parent form immediately when a child form closes.
Below are codes and error messages I got. Please any idea would be appreciated.
public class ChildForm: Form
{
public delegate SomeEventHandler(object sender, EventArgs e);
public event SomeEventHandler SomeEvent;
}
The above code is for child form
and again below codes are for parent form:
public class ParentForm : Form
{
frmCaptureFingerPrint child = new frmCaptureFingerPrint();
child.SomeEvent += new EventHandler(this.HandleSomeEvent);
public void HandleSomeEvent(object sender, EventArgs e)
{
this.textBox1.enabled=true;
}
}
Please need your help
I get a error message that:
Cannot implicitly convert type 'System.EventHandler to CaisseSec....SomeEventHandler
Loading
Hemant SrivastavaPosted Nov 28, 2012, 11:06 AM
Thanks,
Hemant
Hemant SrivastavaPosted Nov 28, 2012, 11:03 AM
public partial class Parent : Form
{
ChildForm childForm = null;
public Parent()
{
InitializeComponent();
childForm = new ChildForm();
childForm.SomeEvent += new SomeEventHandler(childForm_SomeEvent);
textBox1.Text = "";
}
void childForm_SomeEvent(object sender, EventArgs e)
{
textBox1.Text = "Child Form Closed";
}
private void button1_Click(object sender, EventArgs e)
{
childForm.Show();
}
}
and childForm like this:
// delegate declration is defined outside of the class
public delegate void SomeEventHandler(object sender, EventArgs e);
public partial class ChildForm : Form
{
//event is defined here
public event SomeEventHandler SomeEvent;
public ChildForm()
{
InitializeComponent();
}
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
SomeEvent(sender, e);
}
}
When button is clicked on Parent form, child form is opend. See below:
and when child form is closed, a text box is loaded with some message on Parent Form through an event. See below:
Hope it would definetly help.. If you have still any issue let me know..
VulpesPosted Nov 21, 2012, 6:23 AM
Edward MunyaPosted Nov 21, 2012, 2:05 AM
Vulpes
Thanks for yr response but still it does not work.
I agree with you that it would be better to handle the predefined FormClosed event rather than defining my own.
I tried to use the codes you suggested
Now where exactly should I place these codes may be that is the reason it does not work and remember that textbox should be enabled
based on the event fired from another form.
Please your assistance shall be appreciated
VulpesPosted Nov 20, 2012, 9:29 AM
child.SomeEvent += new EventHandler(this.HandleSomeEvent);
should be:
child.SomeEvent += new ChildForm.SomeEventHandler(this.HandleSomeEvent);
However, if you're wanting to enable the textbox when the child form closes it would be better to handle the predefined FormClosed event rather than defining your own.
You could then do:
public class ParentForm : Form
{
frmCaptureFingerPrint child = new frmCaptureFingerPrint();
child.FormClosed += new FormClosedEventHandler(this.ChildForm_Closed);
public void ChildForm_Closed(object sender, FormClosedEventArgs e)
{
this.textBox1.Enabled=true;
}
}
and no additional code would then be needed in the child form class.