Space Ostrich Space Ostrich - 11 months ago 49
C# Question

How do I call a function when a form is focused on?

I'm working in Visual Studio using Windows Form applications.

I've got a Car Rental application that allows the user to update booking information from a SSMS database. The main form displays all bookings after a certain date. When the user updates a booking, I have to change the date being used in order to get the app to refill the list, showing the updated information.

The simplest way to have the app do this automatically is to have it refill the list every time the form is focused on. That is, when it becomes the active form.

How do I do this?

Answer Source

add the event handler in designer.cs file. You can either use Activated or Shown event as per your design.

this.Activated += Form1_Activated;

Do the required stuff here

void Form1_Activated(object sender, System.EventArgs e)