I have an application that uses multiple forms with one form constantly open. I need to be able to refresh a control of the main form when one of the other forms closes. Is there a way to generate an Invalidated event on a control in the main form when I close the other?
Invalidated Event On Form Close
Collapse
X
-
-
You need to have a custom event handler.
Your Main form listens to the custom event when it is triggered from your other forms, in your case maybe in the closing event of the form...
So whenever the form closes, the event is fired where the control in your main form gets refreshed...
If you don't get what i mean, try reading C# Snippet Tutorial - Custom Event Handlers | Switch on the Code
Cheers
-
Thank you Vinci,
I found that during a google search for this, but I think it's a bit more complicated than I need. All I need is a simple way to let the main form know that it needs to redraw a listbox when the other form closes ( or when the OK button the the other form is clicked.)
Is there no way of doing this without creating a complete new class?Comment
-
Actually it is not that complicated. Just attach a handler to the FormClosing event for each child form that you create, and then update your list in the handler.
Do this for each child form you create:
Code:// create your child form ChildForm childForm = new ChildForm(); // attach the event handler to the FormClosing event childForm.FormClosing += new FormClosingEventHandler(childForm_FormClosing);
Code:/// <summary> /// Handles the FormClosing event of the sender control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.Windows.Forms.FormClosingEventArgs"/> instance containing the event data.</param> private void childForm_FormClosing(object sender, FormClosingEventArgs e) { // detach the event handler Form form = (Form)sender; form.FormClosing -= new FormClosingEventHandler(childForm_FormClosing); // update your list here // ... }
Comment
-
Sorry vekipeki, I don't understand what you're trying to tell me.
After further thought, I like the idea of a new class. That way I can update the ListBox whenever I need without closing the form. Looking at what vinci gave me, I created a small two form app to give it a try. Here's what I have:
Form1:
Code:namespace EventTest { public partial class Form1 : Form { Car car = new Car(); public Form1() { InitializeComponent(); car.OwnerChanged += new System.EventHandler(car_OwnerChanged); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { Form2 ui = new Form2(); DialogResult dr = ui.ShowDialog(); } void car_OwnerChanged(object sender, EventArgs e) { MessageBox.Show("Form1", "Error", MessageBoxButtons.OK); textBox1.Text = "Well?"; } } }
Code:namespace EventTest { public partial class Form2 : Form { Car car = new Car(); public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { car.OwnerChanged += new EventHandler(car_OwnerChanged); } private void button1_Click(object sender, EventArgs e) { car.CarOwner = "The Reddest"; } void car_OwnerChanged(object sender, EventArgs e) { MessageBox.Show("Form2", "Error", MessageBoxButtons.OK); } } }
Comment
-
I found exactly what I need here..
C# calling parent functions from child form - CodeCall Programming Forum
I just need to declare the listbox as public, and pass the form1 instance to form2 when I create it. Now in form2, I just put this in the button1_click function..
m_parent.textBo x1.Text = "SWEET!!";
Whala! Works like I need it to.
vinci, I'd still like to know what's wrong with my attempt at your solution though. It may come in handy later on..
Thanks being here guys..Comment
-
You have two different instances of your Car class, one in Form1, and one in Form2.
In Form2, you are changing Form2.car.CarOw ner, so you cannot expect to have the event triggered for Form1.car. If you want to fire an event from Form2, you need to have an event there (in Form2), not in Car class, in order to write something like:
Code:// create new Form2 instance Form2 form2 = new Form2(); // attach the handler for your custom event form2.SomethingHappened += new EventHandler(form2_SomethingHappened); // show the form after attaching form2.Show();
By firing an event from Form2, you show that Form2 does not care who will respond to that event, or how it will be handled, meaning that you can maybe reuse your Form2 in an application that doesn't have a Form1 class.Comment
-
Now I understand.
In order to catch an event from a child form, the event has to be attached prior to showing the child form. Also, the object triggering the event needs to be public. In this case car has to be defined as public within the child form.
This code works as should, triggering the event in both forms:
Form1:
Code:using System; using System.Windows.Forms; namespace EventTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { Form2 form2 = new Form2(); form2.car.OwnerChanged += new EventHandler(car_OwnerChanged); DialogResult dr = form2.ShowDialog(); } void car_OwnerChanged(object sender, EventArgs e) { MessageBox.Show("Form1", "Event", MessageBoxButtons.OK); textBox1.Text = "Well?"; } } }
Code:using System; using System.Windows.Forms; namespace EventTest { public partial class Form2 : Form { public Car car = new Car(); public Form2() { InitializeComponent(); car.OwnerChanged += new EventHandler(car_OwnerChanged); } private void Form2_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { car.CarOwner = "The Reddest"; } void car_OwnerChanged(object sender, EventArgs e) { MessageBox.Show("Form2", "Event", MessageBoxButtons.OK); } } }
There is no real deadline for this project. I've volunteered to write a database application for the local veterans office. I've also taken this opportunity to learn C#.
I'll take your advice and use events rather than public controls. I think it will make the whole application run smoother.
Thank you for your help.Comment
Comment