Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a method in a UserControl after it is shown?

Tags:

c#

winforms

I have a multi-paned form, in the left pane is a tree and in the right pane is a Panel. Tree selections result in specific UserControls being loaded on the Panel. In this case, since the parent form is always loaded, I cannot hook into the parent form's Shown event.

Most of my UC's have an unbound DataGridView on them which needs to be populated when the UC is created. There can be 50,000 rows added up front at times, from an SQL Server database.

Up until now I have populated the grids during the UC constructor, but since we started working with 50,000 rows things have changed. I have found that populating the grid with 50,000 rows from the constructor takes at least 15 minutes for some crazy reason. However, if I wait to populate the grid until I click a button or from the Load event, it takes 15 seconds. This is a mystery. So I am trying to move the loading of the grid elsewhere.

When I try populating the grid from the Load event, however, there are visual inconsistencies. What happens, is you see a small version of my UC appear, it takes 15 seconds to load the DGV rows, and THEN the UC expands to fill the Panel (the UC Dock property is set to Fill). So I don't like this option either.

The Shown event would be a perfect place to load my grid, if it existed for UC's. Does anyone know of another way to do this?

like image 219
Dave Ludwig Avatar asked Jan 12 '11 18:01

Dave Ludwig


People also ask

How to call user control method in parent page in c#?

In the user control I have placed a textbox and a button. On the click event of the Button I am calling the Parent Page method we discussed above using Reflection and passing the value of the textbox to the method and then the method is invoked and the message is displayed on the screen. It works fine for me.

What is method in user control in C#?

C# user control is defined as an implementation in programming language of C# to provide an empty control and this control can be leveraged to create other controls. This implementation provides additional flexibility to re-use controls in a large-scale web project.


1 Answers

My answer to a similar problem was to populate the DGV when the grid first became visible. This is the essence of lazy-loading; get the information at the last possible second.

Hook into VisibleChanged, which will fire when your control has Show() or Hide() called, or the Visible property manually set. If the field is currently visible, AND the control is not in the process of closing or being disposed (because for some freaky reason controls can become visible in this case), perform your grid population.

Here's the necessary handler, very simple:

protected override void OnVisibleChanged(EventArgs e)
{
    base.OnVisibleChanged(e);

    if (Visible && !Disposing) PopulateGridView(); //<-- your population logic
}
like image 58
KeithS Avatar answered Sep 22 '22 05:09

KeithS